Monday, April 6, 2026

Productionize a Django website

 Recommended minimal permissions of files in a Django site:

# Database
sudo chown www-data:www-data db.sqlite3
sudo chmod 664 db.sqlite3

# DB directory
sudo chown www-data:www-data .
sudo chmod 775 .

# Static/media (if needed)
sudo chown -R www-data:www-data static media 

Add SSL certificate to ubuntu server

 

Here’s a clear, practical way to add SSL (HTTPS) to a server on Ubuntu. The most common and easiest method is using Certbot with Let's Encrypt (free SSL certificates).


🔒 Method 1: Use Let’s Encrypt (Recommended)

1. Update your system

sudo apt update
sudo apt upgrade -y

2. Install Certbot

For Nginx:

sudo apt install certbot python3-certbot-nginx -y

For Apache:

sudo apt install certbot python3-certbot-apache -y

3. Get SSL certificate

Nginx:

sudo certbot --nginx

Apache:

sudo certbot --apache

You’ll be prompted to:

  • Enter your email
  • Agree to terms
  • Choose your domain
  • Optionally redirect HTTP → HTTPS (choose YES)

4. Verify SSL

Open your site:

https://yourdomain.com

5. Auto-renew SSL

Let’s Encrypt certs expire every 90 days, but auto-renew is usually set up automatically.

Test it:

sudo certbot renew --dry-run

⚙️ Method 2: Manual (Advanced / Custom setups)

If you're not using Apache/Nginx automation:

Generate certificate:

sudo certbot certonly --standalone -d yourdomain.com

Certificates will be stored in:

/etc/letsencrypt/live/yourdomain.com/

Then configure your server manually with:

  • fullchain.pem
  • privkey.pem

📌 Common Requirements

Make sure:

  • Your domain points to your server (DNS configured)
  • Ports are open:
sudo ufw allow 80
sudo ufw allow 443

🧠 Quick Troubleshooting

  • If Certbot fails → check DNS propagation
  • If port 80 is busy → stop conflicting services:
sudo systemctl stop nginx
 
 
 
 
Notes:
 
$ apt install apache2 openssl
$ a2enmod ssl
$ systemctl restart apache2
$ apt install certbot python3-certbot-apache
$ certbot --apache 

Host php and django on same apache server

To host both PHP and Django sites on the same Apache server, you just need to configure Apache to handle each type of application differently.

Here’s how it works in practice:


🔧 Key Idea

Apache can serve:

  • PHP via mod_php or PHP-FPM
  • Django via a WSGI interface (usually mod_wsgi)

Each site gets its own virtual host configuration.


🧱 Typical Setup

1. PHP Site

Handled normally with PHP enabled:

<VirtualHost *:80>
ServerName php-site.com
DocumentRoot /var/www/php-site

<Directory /var/www/php-site>
AllowOverride All
Require all granted
</Directory>
</VirtualHost>

2. Django Site

Uses mod_wsgi to run the Django app:

<VirtualHost *:80>
ServerName django-site.com

WSGIDaemonProcess django_site python-home=/var/www/django-site/venv python-path=/var/www/django-site
WSGIProcessGroup django_site
WSGIScriptAlias / /var/www/django-site/project/wsgi.py

<Directory /var/www/django-site/project>
<Files wsgi.py>
Require all granted
</Files>
</Directory>

Alias /static /var/www/django-site/static
<Directory /var/www/django-site/static>
Require all granted
</Directory>
</VirtualHost>

⚠️ Things to Watch Out For

1. Port / Domain Separation

  • Use different domains (recommended), e.g.:
    • example.com → PHP
    • app.example.com → Django
  • Or different ports (less common)

2. Python Environment

  • Always use a virtual environment for Django
  • Keep dependencies isolated from system Python

3. Performance Considerations

  • PHP runs inside Apache (or via PHP-FPM)
  • Django runs as a separate WSGI process
    ➡️ They won’t conflict, but you need enough memory for both

4. Static Files

  • Apache can serve static files for both apps
  • Django static files should be collected (collectstatic)

✅ Alternative (Often Better)

Many modern setups use:

  • Apache or Nginx as a reverse proxy
  • Django served via:
    • gunicorn or uwsgi
  • PHP via PHP-FPM

This gives better scalability and separation.


🧠 Bottom Line

Yes—you can run both on the same Apache server by:

  • Using VirtualHosts
  • Enabling both PHP support and mod_wsgi
 
 
 


 

Host Php/Django On Ubuntu

On Ubuntu this is very straightforward. I’ll walk you through a clean, practical setup where Apache hosts both a PHP site and a Django app.


🧰 1. Install Required Packages

sudo apt update
sudo apt install apache2 php libapache2-mod-php python3 python3-venv python3-pip libapache2-mod-wsgi-py3

Enable Apache modules:

sudo a2enmod wsgi
sudo a2enmod rewrite
sudo systemctl restart apache2

🐘 2. Set Up Your PHP Site

Create a directory:

sudo mkdir -p /var/www/php-site
sudo chown -R $USER:$USER /var/www/php-site

Test PHP:

echo "<?php phpinfo(); ?>" > /var/www/php-site/index.php

Create config:

sudo nano /etc/apache2/sites-available/php-site.conf
<VirtualHost *:80>
ServerName php.local
DocumentRoot /var/www/php-site

<Directory /var/www/php-site>
AllowOverride All
Require all granted
</Directory>
</VirtualHost>

Enable it:

sudo a2ensite php-site

🐍 3. Set Up Your Django App

Create project

mkdir -p /var/www/django-site
cd /var/www/django-site

python3 -m venv venv
source venv/bin/activate

pip install django
django-admin startproject project .

Test locally:

python manage.py runserver

Configure WSGI + Apache

sudo nano /etc/apache2/sites-available/django-site.conf
<VirtualHost *:80>
ServerName django.local

WSGIDaemonProcess django_site python-home=/var/www/django-site/venv python-path=/var/www/django-site
WSGIProcessGroup django_site
WSGIScriptAlias / /var/www/django-site/project/wsgi.py

<Directory /var/www/django-site/project>
<Files wsgi.py>
Require all granted
</Files>
</Directory>

Alias /static /var/www/django-site/static
<Directory /var/www/django-site/static>
Require all granted
</Directory>
</VirtualHost>

📦 4. Prepare Django for Production

Edit settings:

ALLOWED_HOSTS = ["django.local"]
STATIC_ROOT = "/var/www/django-site/static"

Then:

python manage.py collectstatic

🌐 5. Enable the Django Site

sudo a2ensite django-site
sudo systemctl reload apache2

🧪 6. Local Testing (Important)

Edit your hosts file:

sudo nano /etc/hosts

Add:

127.0.0.1 php.local
127.0.0.1 django.local

⚠️ Common Gotchas

🔐 Permissions

sudo chown -R www-data:www-data /var/www/django-site

📁 Static files not loading?

Make sure:

  • collectstatic was run
  • Apache Alias /static is correct

🧠 mod_wsgi vs modern setup

This works well, but many production setups prefer:

  • Apache → reverse proxy
  • Django → Gunicorn
  • PHP → PHP-FPM

More scalable, but slightly more complex.


✅ Final Result

You’ll have:

  • http://php.local → PHP site
  • http://django.local → Django app

Running on the same Apache server without conflict.

 

Tuesday, February 24, 2026

Install Keras on Mac

 To install Keras on a Mac, install TensorFlow (which includes Keras) using  

pip install tensorflow 

or  

pip install tensorflow-macos for Apple Silicon (M1/M2/M3) chips. 

For best performance on M1/M2/M3 chips, use conda to install tensorflow-deps

Verify with python -c "import keras; print(keras.__version__)"

 

== Use PIP (Standard & Apple Silicon)

# Upgrade pip
pip3 install --upgrade pip

# Install TensorFlow (includes Keras)
pip3 install tensorflow
 
Note: TensorFlow works with Python 3.9-2.12. 

The recommended way to install and manage multiple Python versions on a Mac is by using a version manager like pyenv, which prevents conflicts with the system's default Python installation.

== Use pyenv (Recommended for Developers)

brew install pyenv 

You may also want the pyenv-virtualenv plugin for creating isolated project environments: 

bash  brew install pyenv-virtualenv

Configure your shell: You need to initialize pyenv in your shell (e.g., Zsh, the default for modern macOS). Add the necessary lines to your shell's configuration file (commonly ~/.zshrc or ~/.bashrc) and restart your terminal as per the pyenv documentation instructions.

pyenv install 3.12.0

Switch between versions:

  • Globally: Set a global default version for all your shell sessions:
    bash
    pyenv global 3.12.0
    
  • Per Project: Navigate to a project directory and set the local Python version, which creates a .python-version file:
    bash
    pyenv local 3.12.0
    
  • Create a Virtual Environment: Use pyenv-virtualenv to create an isolated environment for a project with a specific Python version:
    bash
    pyenv virtualenv 3.12.0 myprojectenv
    cd myprojectdir
    pyenv local myprojectenv
    

 

Create local env:

# Create a virtual environment (e.g., named 'tf-env')
python -m venv tf-env

# Activate it (Linux/macOS)
source tf-env/bin/activate
 
Then install:
pip install tensorflow
 
Verify:
import tensorflow as tf
print(tf.keras.__version__)
print(tf.__version__) 
  


Monday, February 16, 2026

Run apache and nginx on same server

AI Overview

Common Configuration Methods
 
You have several options to run both servers simultaneously:
  • Different Ports: This is the simplest method. Configure one server (e.g., Apache) to listen on a non-standard port like 8080 or 81, while the other (e.g., Nginx) uses the default 80 (HTTP) and 443 (HTTPS) ports. Users would access the second server by appending the port number to the URL (e.g., http://yourdomain.com:8080).
  • Different IP Addresses: If your server has multiple IP addresses, you can bind each web server to a unique IP address on the same port (80/443).
  • Nginx as a Reverse Proxy (Recommended): This leverages the strengths of both servers. Nginx (known for speed and efficiency in static content and high concurrency) listens on the public ports 80 and 443. It serves static files directly and proxies requests for dynamic content (like PHP applications that rely on .htaccess files) to Apache, which is configured to listen on an internal, loopback IP and port (e.g., 127.0.0.1:8080). This setup improves overall performance and resource usage.
Steps for a Reverse Proxy Setup (Nginx in front of Apache)
 
A typical configuration involves these general steps (specific file paths may vary by operating system):
  1. Configure Apache to use a different port: Edit the Apache configuration files (ports.conf and the default virtual host file) to change the listening ports from 80/443 to an internal port like 8080.
  2. Configure Nginx as a reverse proxy: Set up Nginx to listen on the public ports 80/443. For dynamic requests, add proxy directives within the Nginx virtual host configuration to forward traffic to the Apache port (e.g., proxy_pass http://localhost:8080;).
  3. Install mod_rpaf (or mod_remoteip) on Apache: This module ensures Apache logs the client's real IP address instead of Nginx's localhost IP, which is useful for statistics and authentication.
  4. Restart services: After configuration changes, restart both Nginx and Apache to apply the new settings.
This combined approach allows you to benefit from Nginx's performance for static content while still using Apache for applications that require its specific features.

How To Deploy a React Application with Nginx on Ubuntu

 

Learn how to deploy a React app with Nginx on Ubuntu. Step-by-step guide covering build, server config, reverse proxy, SSL, and production optimization.

To host a React application on an Ubuntu server, you need to
install Node.js and npm, build your application, and then use a web server like Nginx to serve the static files.
 
Prerequisites
  • An Ubuntu server (virtual machine or physical).
  • SSH access to your server (use a client like PuTTY on Windows).
  • A React application built on your local machine.
Step-by-Step Deployment Guide
  1. Connect to Your Server
    Use SSH to connect to your Ubuntu server using its IP address and your credentials.
  2. Install Node.js and npm
    React requires Node.js and npm to build the application.
    • Update your server's package list: sudo apt update
    • Install Node.js and npm: sudo apt install nodejs npm
    • Verify the installation: node -v and npm -v
  3. Transfer Your React App Files
    Build your React application on your local machine using the command: npm run build. This creates a production-optimized build directory. Transfer the contents of this build directory to your Ubuntu server using tools like scp, rsync, or by cloning your repository from GitHub on the server itself.
  4. Install and Configure Nginx
    Nginx is a high-performance web server that will serve your application's static files.
    • Install Nginx: sudo apt install nginx
    • Start and enable the Nginx service: sudo systemctl start nginx and sudo systemctl enable nginx
    • Create a new Nginx configuration file for your site in the /etc/nginx/sites-available/ directory. You can use a text editor like nano (e.g., sudo nano /etc/nginx/sites-available/your_domain):
      nginx
      server {
          listen 80;
          server_name your_domain www.your_domain;
      
          root /var/www/your_domain/html;
          index index.html index.htm;
      
          location / {
              try_files $uri $uri/ /index.html;
          }
      }
      
      (Replace your_domain with your actual domain name and adjust the root path to where you placed your build files, for example, /var/www/your_domain/html).
  5. Enable Your Nginx Site
    • Create a symbolic link from your configuration file in sites-available to the sites-enabled directory:
      sudo ln -s /etc/nginx/sites-available/your_domain /etc/nginx/sites-enabled/
    • Test your Nginx configuration for syntax errors: sudo nginx -t
    • Restart Nginx to apply the changes: sudo systemctl restart nginx
Your React application should now be accessible in a web browser via your server's IP address or domain name. For a secure connection, you should also set up SSL certificates using a tool like Certbot.

How To Deploy a React Application with apache on Ubuntu

 

To deploy a React application with Apache on Ubuntu, you need to
build your application for production, configure Apache to serve the static files, and ensure proper handling of client-side routing using an .htaccess file.
 
Prerequisites
  • An Ubuntu server with Apache installed.
  • Node.js and npm installed on your local machine or server.
  • A React application ready for deployment.
Step-by-Step Guide
 
 
1. Build the React Application
On your local machine, navigate to your project's root directory in the terminal and run the build command to generate production-ready static files:
bash
npm run build
This command creates an optimized build (or dist if using Vite) folder containing your static assets.
2. Prepare the Ubuntu Server and Upload Files
 
Connect to your Ubuntu server via SSH and prepare a directory to host your application.
  • Create a directory for your site (replace yourwebsite.com with your domain or desired folder name):
    bash
    sudo mkdir -p /var/www/yourwebsite.com
    
  • Set ownership of the directory:
    bash
    sudo chown -R $USER:$USER /var/www/yourwebsite.com
    
  • Upload the contents of the local build folder to the server's directory (e.g., /var/www/yourwebsite.com) using an FTP client like FileZilla or scp.
 
3. Configure Apache for React Routing
 
React applications use client-side routing, which means direct access to sub-routes (e.g., ://yourwebsite.com) will result in a 404 error without proper server configuration. Apache needs to be instructed to redirect all requests to the index.html file.
  • Enable the mod_rewrite module:
    bash
    sudo a2enmod rewrite
    
  • Create a .htaccess file: Inside your application's root directory on the server (e.g., /var/www/yourwebsite.com), create a file named .htaccess and add the following snippet:
    apache
    Options -MultiViews
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.html [QSA,L]
    
    This snippet tells Apache that if a requested file does not exist, it should load index.html.
  • Ensure AllowOverride All is set: You might need to edit your Apache configuration to allow .htaccess files to override settings.
    • Open your virtual host file or the main apache2.conf file (e.g., /etc/apache2/sites-available/000-default.conf or /etc/apache2/apache2.conf).
    • Find the <Directory /var/www/html> block (or your custom directory block) and ensure AllowOverride is set to All:
    apache
    <Directory /var/www/yourwebsite.com>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>
    
4. Restart Apache
 
After making configuration changes, restart Apache for them to take effect:
bash
sudo systemctl restart apache2
Your React application should now be deployed and accessible via your server's IP address or domain name.