HomeTom - CS

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 

Sunday, April 5, 2026

Bluehost VPS Setup

Docs 

https://www.bluehost.com/blog/how-to-set-up-a-vps/

https://www.bluehost.com/help/article/dns-records-explained


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.

Blog Archive

Followers