In this article I am going to explain how I installed Django and Apache2 onto Ubuntu 16, I spent the best part of a week researching and compiling my notes from serveral different sources. I hope this will help some of you. My setup consists of a virtual machine running a fresh install of Ubuntu 16 and Apache2 and a Django project created in Python version 3.6.6. Their is no guarantee that this guide will work for everyone, but if you do need help please get in contact and I shall do my best to assit.
Ubuntu 16.04 does not come with the version of Python that we need, so we need to add a new repository in order to install Python 3.6.8.
1 |
sudo add-apt-repository ppa:jonathonf/python-3.6 |
Once we have added the PPA as shown above we can now install the software that we need, issue the below commands one at a time.
1 2 3 4 |
sudo apt update sudo apt install python3.6 sudo apt install python3.6-dev sudo apt install python3.6-venv |
The next part is to install pip version 3.6, for this we issue the following commands one at a time.
1 2 |
wget https://bootstrap.pypa.io/get-pip.py sudo python3.6 get-pip.py |
we will now create some symbolic links so that when we issue python3 it will point to our python3.6 installation, you may get an error saying that they exists, this is ok.
1 2 |
sudo ln -s /usr/bin/python3.6 /usr/local/bin/python3 sudo ln -s /usr/local/bin/pip /usr/local/bin/pip3 |
Now we need to install mod-wsgi for Apache2 this will allow our Python code to work with Apache2, issue the below commands
1 2 |
sudo apt-get install apache2-dev sudo pip install mod_wsgi |
Uploading Project Files to Server
we have now installed the software needed, we now need to upload our project files to the server. I am on a Windows pc so I used WinScp to upload my project files to my Linux server within my home directory. Once you have uploaded your project files we need to give permisisons to Apache2 on some of the files, replace MyBlog with your project name.
1 2 3 4 5 6 |
sudo chown :www-data MyBlog/db.sqlite3 sudo chmod 664 MyBlog/db.sqlite3 sudo chown :www-data MyBlog/ sudo chown -R :www-data MyBlog/media sudo chmod -R 775 MyBlog/media/ |
we now need to modify your projects settings.py file, in the ALLOWED_HOSTS = [] you will need to put your server IP in or domain name in. towwards the bottom of this file you will need to add the following.
1 2 |
STATIC_ROOT = os.path.join(BASE_DIR, 'static') STATIC_URL = '/static/' |
Now we create our Python Virtual Envioment, CD into your project directory.
1 2 |
python3.6 -m venv venv source venv/bin/activate |
Creating the Virtual Host
We are now at the point where we can create the virtual host for our Django Project.
1 2 3 |
cd /etc/apache2/site-available sudo cp 000-default.conf django-project.conf sudo nano django-project.conf |
within this file add the following between <VirtualHost> remember to change your user and project name
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
Alias /static /home/joe/MyBlog/static <Directory /home/joe/MyBlog/static> Require all granted </Directory> Alias /media /home/joe/MyBlog/media <Directory /home/joe/MyBlog/media> Require all granted </Directory> <Directory /home/joe/MyBlog/MyBlog> <Files wsgi.py> Require all granted </Files> </Directory> WSGIScriptAlias / /home/joe/MyBlog/MyBlog/wsgi.py WSGIDaemonProcess MyBlog_app python-path=/home/joe/MyBlog python-home=/home/joe/MyBlog/venv WSGIProcessGroup MyBlog_app |
we can now enable our site
1 2 |
sudo a2ensite django-project.conf sudo a2dissite 000-default.conf |
Install mod_wsgi
1 |
sudo apt-get install libapache2-mod-wsgi |
get the load path
1 |
mod_wsgi-express module-config |
now we need to update the wsgi.load like so, you will need to copy the LoadModule output from the above command.
1 |
sudo nano /etc/apache2/mods-available/wsgi.load |
we can now restart Apache2
1 |
sudo service apache2 restart |
Installing Project Dependencies
we now need to install our project dependencies, by running the following commnad from within our project, and collecting the static files
1 2 |
pip install -r requirements.txt python3 manage.py collectstatic |
if all being well you should now be able to navigate to your site