In this blog, I use the AWS Ubuntu 22.04 instance as a Hosting platform and used an Apache2 server with mod_wsgi for configurations.
Step 1. Run these below cmd's to get the latest updates.
$ sudo apt-get update
$ sudo apt-get upgrade
$ sudo apt-get install -y python3-pip
$ sudo python3 -m pip install --upgrade pip
$ mkdir /var/www
Move into the directory:
$ cd /var/www
I will prefer to create a small project and then import or clone your existing project to make sure other things are working as expected.
Check that the Python is installed
$ python3 --version
$ sudo python3 -m pip install django
or
$ sudo python3 -m pip install django==2.2
Check the Django version.
$ python3 -m django --version
Create a project using django admin.
$ sudo django-admin startproject mysite
Change directory into that project:
$ cd mysite/
Create an app using manage.py.
$ sudo python3 manage.py startapp website
Migrate the changes to create db instances.
$ sudo python3 manage.py migrate
If you face any issue in migration run the below cmd:
$ sudo python3 manage.py migrate --run-syncdb
Run the django server.
$ sudo python3 manage.py runserver
Step 4. Now install apache2 and mod_wsgi for Python3.
Now let's setup the apache config file for our website:
Configuration:
<VirtualHost *:80>
ServerName neptuneworld.in
ErrorLog ${APACHE_LOG_DIR}/mysite-error.log
CustomLog ${APACHE_LOG_DIR}/mysite-access.log combined
WSGIDaemonProcess mysite processes=2 threads=25 python-path=/var/www/mysite
WSGIProcessGroup mysite
WSGIScriptAlias / /var/www/mysite/mysite/wsgi.py
Alias /robots.txt /var/www/mysite/static/robots.txt
Alias /favicon.ico /var/www/mysite/static/favicon.ico
Alias /static/ /var/www/mysite/static/
Alias /media/ /var/www/mysite/media/
<Directory /var/www/mysite/mysite>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
<Directory /var/www/mysite/static>
Require all granted
</Directory>
<Directory /var/www/mysite/media>
Require all granted
</Directory>
<Directory /var/www/mysite>
WSGIApplicationGroup %{GLOBAL}
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
ServerName, WSGIProcessGroup and also change Directory as per the project.
Remove the default site if you wish to remove it.
$ sudo rm /etc/apache2/sites-available/000-default.conf
Now enable our new site “mysite.conf” and reload the Apache server.
$ sudo a2ensite mysite
$ sudo systemctl reload apache2
Check the status using below cmd.
$ sudo systemctl status apache2.service
Finally, we can try the url or IP that we configured in “mysite.conf” in ServerName.
Example: ServerName neptuneworld.in.
But we find that we can't access the Images and not able to login to application because our database is read-only for our user.
In this case, using apache, our "user" is www-data. We need to give this user permission to edit these files for us.
$ cd /var/www/
$ sudo chown www-data mysite/
$ sudo chown www-data mysite/db.sqlite3
$ sudo chown www-data mysite/*
$ sudo chown www-data mysite/media/*
$ sudo chown www-data mysite/static/*
$ sudo chown -R www-data:www-data /var/www/mysite
To check the owner or user permission
$ sudo ls -ld /var/www/mysite
Then reload server:
$ sudo service apache2 reload
or
$ sudo systemctl reload apache2
You can check the users using:
$ ls -l
You can check the status of your apache server:
$ sudo systemctl status apache2.service
To start or stop apache service use below cmds:
$sudo systemctl start apache2.service
$sudo systemctl stop apache2.service
After successful run now you can import your existing project.
$ sudo git clone https://github.com/Neptune998/repository.git
Then install all the required applications like:
$ sudo pip install django-taggit==1.3.0
$ sudo pip install django-crispy-forms==1.10.0
$ sudo pip install django-summernote==0.8.11.6
$ sudo apt install Pillow==8.1.0
Run this cmd to get all the static files in a path that you configured in settings.py so that django can use them.
$ sudo python3 manage.py collectstatic
To check the logs use below cmds:
$ cd /var/log/apache2
Move to the directory or directly access using below nano cmd:
$ sudo nano /var/log/apache2/mysite-error.log
$ sudo nano /var/log/apache2/mysite-access.log
Thanks for Reading !!!