Last active
May 13, 2022 12:19
-
-
Save egor83/b53870c0cd50483a3b39013daa7b0fa4 to your computer and use it in GitHub Desktop.
Deploying Django on Apache server with mod_wsgi
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Deploying Django on Apache server with mod_wsgi | |
Per https://docs.djangoproject.com/en/2.1/howto/deployment/wsgi/modwsgi/ | |
https://modwsgi.readthedocs.io/en/develop/installation.html | |
Some things are not immediately obvious, so I'm writing down the detailed list of steps that worked for me. | |
I started with bare VPS, using Ubuntu 18.04.4. Python 3 (3.6.9) was available out of the box, but not Python 2. | |
Used the latest mod_wsgi version available (mod_wsgi-4.7.1) | |
Some steps might not be necessary for you (eg installing vim, creating a separate user). | |
I'm installing Ubuntu packages as root, and trying to do the rest as a separate user where possible. | |
-------------------- | |
(as root) | |
sudo apt-get update | |
sudo apt-get upgrade | |
adduser user | |
usermod -aG sudo user | |
sudo apt-get install vim | |
sudo apt install apache2 | |
# test Apache status | |
sudo systemctl status apache2 | |
# test if Apache has installed correctly by accessing your server's IP in browser - default Apache page should be shown | |
# from installation guide: "On Linux systems, if Apache has been installed from a package repository, | |
# you must have installed the corresponding Apache “dev” package as well." | |
sudo apt-get install apache2-dev | |
sudo apt install curl | |
sudo apt-get install python3-distutils | |
sudo apt-get install python3-dev | |
mkdir install | |
cd install/ | |
# download, unpack mod-wsgi - NB you might need to change version number | |
# from https://github.com/GrahamDumpleton/mod_wsgi/releases | |
curl https://codeload.github.com/GrahamDumpleton/mod_wsgi/tar.gz/4.7.1 -o mod_wsgi-4.7.1.tar.gz | |
tar xvfz mod_wsgi-4.7.1.tar.gz | |
cd mod_wsgi-4.7.1/ | |
# configure, build mod-wsgi | |
# had to point to python3 explicitly since 'python' was not available | |
./configure --with-python=/usr/bin/python3 | |
make | |
make install | |
# from the guide: "Loading Module Into Apache ... In the simplest case, all that is required is to | |
# add a line of the form: | |
# LoadModule wsgi_module modules/mod_wsgi.so | |
# into the main Apache “httpd.conf” configuration file at the same point that other Apache modules | |
# are being loaded. The last option to the directive should either be an absolute path to where | |
# the mod_wsgi module file is located, or a path expressed relative to the root of your Apache | |
# installation. If you used “make” to install the package, see where it copied the file to work out | |
# what to set this value to." | |
# In my case, the output of make contained the following path: /usr/lib/apache2/modules/mod_wsgi.so, | |
# so I edited Apache config (at /etc/apache2/apache2.conf in my case) by adding | |
LoadModule wsgi_module /usr/lib/apache2/modules/mod_wsgi.so | |
# after the following lines: | |
# IncludeOptional mods-enabled/*.load | |
# IncludeOptional mods-enabled/*.conf | |
# Creating a *.load file in mods-enabled/ directory might be the more correct option though. | |
# reload and see if Apache works: | |
sudo systemctl reload apache2 | |
sudo systemctl status apache2 | |
make clean | |
# install pip, venv for Python3 (was not installed out of the box for me) | |
# in ~/install: | |
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py | |
python3 get-pip.py | |
apt-get install python3-venv | |
### the following steps are done as the user, not as root | |
# note that I'm using user's home directory here, and this is against the best practices: | |
# https://modwsgi.readthedocs.io/en/develop/user-guides/quick-configuration-guide.html#mounting-the-wsgi-application | |
mkdir projects | |
cd projects/ | |
# created virtual environment myenv | |
python3 -m venv myenv | |
source myenv/bin/activate | |
pip install --upgrade pip | |
pip install django | |
# here I created a barebones Django project called dj_base just for test purposes | |
# the following steps are from Django deployment tutorial: | |
# https://docs.djangoproject.com/en/2.1/howto/deployment/wsgi/modwsgi/ | |
# editing Apache config (at /etc/apache2/apache2.conf for me) | |
# added the following to the end of the file (not including the dashes): | |
-------------------- | |
WSGIScriptAlias / /home/user/projects/dj_base/dj_base/wsgi.py | |
WSGIPythonHome /home/user/projects/myenv | |
WSGIPythonPath /home/user/projects/dj_base | |
<Directory /home/user/projects/dj_base/dj_base> | |
<Files wsgi.py> | |
Require all granted | |
</Files> | |
</Directory> | |
-------------------- | |
# reload Apache, make sure it works: | |
sudo systemctl reload apache2 | |
sudo systemctl status apache2 | |
# added server IP to ALLOWED_HOSTS in project settings | |
# That's it! | |
# misc NBs: | |
# layout of Apache for Ubuntu (and other distros): | |
https://cwiki.apache.org/confluence/display/httpd/DistrosDefaultLayout#DistrosDefaultLayout-Debian,Ubuntu(Apachehttpd2.x): | |
# Apache logs at: | |
/var/log/apache2/error.log | |
/var/log/apache2/access.log |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment