deploying django app on ubuntu server with help of apache2
Assumed
- Ubuntu server on EC2 or awslightsail
- connect server using SSH key
- Project files in github and ssh key added to git
- MySQL server installed and configured.
-
Logged into ubuntu server using SSH key and update
sudo apt-get update
-
install pip and apache2 with wsgi mod
sudo apt-get install python3-pip apache2 libapache2-mod-wsgi-py3
-
create virtual environment
python3 -m venv myvenv
if getting any error try below comment
sudo apt install -y python3-venv
-
activate virtual environment
source myvenv/bin/activate
-
Now virtual environment activated then we ready to clone project from git
git clone projectsshpathmyproject.git
-
navigate to project folder
cd myproject
-
install requirements
pip install -r requirements.txt
-
collect static assets
python manage.py collectstatic
-
migrate database
python manage.py makemigrations python manage.py migrate
-
test server is working or not
python manage.py runserver 0.0.0.0:8000
it will run port on 8000 example http://yourpublicip:8000. it works fine lets configure to apache server
-
configure apache conf file for domain
sudo nano /etc/apache2/sites-available/domainname.conf
-
add apache configuration on domainname.conf file
WSGIDaemonProcess myproject python-path=/home/ubuntu/myproject python-home=/home/ubuntu/myvenv WSGIProcessGroup myproject WSGIScriptAlias / /home/ubuntu/myproject/myproject/wsgi.py <VirtualHost *:80> ServerAdmin webmaster@localhost ServerName domainname ServerAlias www.domainname DocumentRoot /home/ubuntu/myproject <Directory /home/ubuntu/myproject/myproject> <Files wsgi.py> Require all granted </Files> </Directory> Alias /static /home/ubuntu/myproject/static <Directory /home/ubuntu/myproject/static> Require all granted </Directory> Alias /media /home/ubuntu/myproject/media <Directory /home/ubuntu/myproject/media> Require all granted </Directory> ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost>
-
enable apache conf
sudo a2ensite domain.conf sudo service apache2 restart
-
Gave permission to server access files
sudo chmod 664 db.sqlite3 sudo chown :www-data db.sqlite3 sudo chown :www-data ~/myproject sudo service apache2 restart