In this tutorial, you’ll deploy a Django application on Ubuntu EC2 instance running Apache2 step by step.
Prerequisites
- Install apache2
~ sudo apt update
~ sudo apt install apache2
~ sudo service apache2 start
- Apache Error watch
~ watch tail -n 15 /var/log/apache2/error.log
- Check python version
~ python3 –version
- Install pip
~ sudo apt install python3-pip
~ sudo apt install python3-venv
- Create project directory
~ mkdir demo
- Change the ownership to ubuntu. Example:
~ sudo chown -R ubuntu:ubuntu demo
~ sudo chmod 777 demo
~ sudo chmod 755 /home/<user>
- Add GitHub Repository. If you need generate SSH key for GitHub, you can visit this link.
~ git init
~ git remote add origin repository_link
~ git pull origin main
- Change the ownership of the media directory. Example:
~ sudo chown -R www-data:www-data demo/media
- Create virtual env at demo
~ cd demo
~ python3 -m venv env
~ source env/bin/activate #Activate virtual env
- Install packages
~ pip3 install -r requirements.txt
Apache Server Configurations
- Make a demo.conf file on /etc/apache2/sites-available and configure following way
<VirtualHost *:80>
ServerAdmin webmaster@localhost
ServerName example.com
ServerAlias www.example.com
DocumentRoot /home/ubuntu/demo
<Directory /home/ubuntu/demo>
AllowOverride all
Require all granted
Options FollowSymlinks
Allow from all
</Directory>
Alias /media /home/ubuntu/demo/media
Alias /static /home/ubuntu/demo/static
<Directory /home/ubuntu/demo/static>
Require all granted
</Directory>
<Directory /home/ubuntu/demo/media>
Require all granted
</Directory>
<Directory /home/ubuntu/demo/demo>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess demo python-path=/home/ubuntu/demo python-home=/home/ubuntu/demo/env
WSGIProcessGroup demo
WSGIScriptAlias / /home/ubuntu/demo/demo/wsgi.py
</VirtualHost>
- Make migrate and collect static
~ source env/bin/activate
~ python3 manage.py makemigrations
~ python3 manage.py migrate
~ python3 manage.py collectstatic
- Change permission and ownership
~ sudo chmod 664 db.sqlite3
~ sudo chown :www-data db.sqlite3
~ sudo chown :www-data demo
~ sudo chown :www-data demo/demo
- Enable apache site configuration file
~ sudo a2ensite demo.conf
~ sudo a2dissite default.conf
- Install wsgi mod in apache
~ sudo apt-get install libapache2-mod-wsgi-py3
~ sudo a2enmod wsgi
~ sudo service apache2 restart
- Apache Server log
~ watch tail -n 15 /var/log/apache2/error.log
Done! Now you can browse your application with the domain name.
Top comments (0)