Skip to content

Instantly share code, notes, and snippets.

@chhantyal
Last active March 31, 2023 08:36
Show Gist options
  • Save chhantyal/57cdf5b360f04f8cb3f6e32a7dc32d87 to your computer and use it in GitHub Desktop.
Save chhantyal/57cdf5b360f04f8cb3f6e32a7dc32d87 to your computer and use it in GitHub Desktop.
Easiest way to host and deploy Python web applications (Django) using Apache and Mod_WSGI (Mod_WSGI-express)

Apache virtual host to proxy requests to another port

Name it project_name.conf and symlink to /etc/apache2/sites-enabled/

<VirtualHost *:80>
       ServerName example.com
       ProxyPass / http://localhost:8000/
       ProxyPassReverse / http://localhost:8000/
       RequestHeader set X-Forwarded-Port 80
</VirtualHost>

Systemd project.service script to run it in background and start when machine reboots.

Name it project_name.service and put/symlink it to /etc/systemd/system/

[Unit]
Description=Porject_name application daemon

[Service]
Type=simple
WorkingDirectory=/var/www/roject_dir
ExecStart=/var/www/project_dir/env/bin/mod_wsgi-express start-server --url-alias /static /var/www/project_dir/static wsgi.py
Group=www-data
User=www-data
Restart=always

Finally, run sudo systemctl daemon-reload and sudo systemctl start project_name

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment