Skip to content

Latest commit

 

History

History
407 lines (310 loc) · 11.9 KB

apache2.md

File metadata and controls

407 lines (310 loc) · 11.9 KB

Django Depolyment With Apache2

Here we will see how can we depoly our django app with apache2 and after that we will see how to configure data base for our site then look over other djano framworks (celery, django channels) and learn how to depoly them

Downloading Requirments

so let's download our Requirments, before that update your system

sudo apt update
sudo apt upgrade

Run This commands one by one to Download all requirments

sudo apt install apache2
sudo apt install python
sudo apt install libapache2-mod-wsgi-py3
sudo apt install python3-pip
sudo pip install virtualenv

Now we have donwloaded everything now let's check everyting is fine or not. Run This follwing commands if we are not getting any error then Everthing is fine

First we need to set up Firewall permission to apache, just copy the command and pasted it.

ufw allow "Apache Full"
  • Checking Python
      python3
    
  • Checking Apache
    sudo service apache2 status
    

Now we are done next we will deploy our project

Downloading Project Requirments

In order to do that you need to activate your virtualenv

source myvnv/bin/activate

Now we can download required python modules in my case I have requirments.txt (if you don't have requirments.txt then you download one by one)

pip install -r requirments.txt

After donwloading everything you Check it by this command -:

pip list

We have successfully downloaded all requirments now deactivate the virtual env and move to depolyment stage

    deactivate

Media File Permission

If You have media files then you need to go thorugh the following steps

  • Adding User to 'www-data' group (www-data is a user and group that the service httpd (Apache) uses to run on a system)
  • In my case my username is trisha
sudo usermod -a -G www-data trisha
  • Giving Permission To Media Folder ( I am gussing That Your Media folder name is Media and Run This caommnd Where your media file is)
sudo chown -R www-data:www-data media

Deploying Project

Now we need to move our project file to a 'www' dirctory to do that

sudo mv project_folder_name /var/www/

example:- In our case our project name is 'myproject'

sudo mv myproject  /var/www/

now let's go to the localtion and check if the project moved successfully or not

cd /var/www/
ls

After that you will be able to see your project in the directory now let's move to the next step. Now go your project and active your virtual env and download all requirments . For now I hope that you know how to do that and skipping it for currently and moveing to the next step.

Now suppose I have a domain which I have pointed to my vps Ip and the domain is- 'subho.com' so you will have create a config file for apahce with name of your domain. Don't forgot to use '.conf' extension with your config file

syntax: sudo nano /etc/apache2/sites-available/your_domain.conf
example: sudo nano /etc/apache2/sites-available/subho.com.conf

After runing this command a code editor will open to you and paste the following command there (You will need to make changes here to use it , I will discuss it after the code)

<VirtualHost *:80>
   ServerName www.example.com
   ServerAlias example.com
   ServerAdmin contact@example.com
   DocumentRoot /var/www/project_folder_name
   ErrorLog ${APACHE_LOG_DIR}/error.log
   CustomLog ${APACHE_LOG_DIR}/access.log combined
   
   Alias /static /var/www/project_folder_name/static
   <Directory /var/www/project_folder_name/static>
       Require all granted
   </Directory>
   
   Alias /media /var/www/project_folder_name/media
   <Directory /var/www/project_folder_name/media>
       Require all granted
   </Directory>
   
   <Directory /var/www/project_folder_name/Inner_project_folder_name>
       <Files wsgi.py>
           Require all granted
       </Files>
   </Directory>
   
   WSGIDaemonProcess project_folder_name python-home=/var/www/project_folder_name/virtualenv_name python-path=/var/www/project_folder_name
   WSGIProcessGroup project_folder_name
   WSGIScriptAlias /  /var/www/project_folder_name/inner_project_folder_name/wsgi.py
</VirtualHost>

Explanation now let's unser stand how to use the whole code so make changes one by one.

Step 1:

ServerName www.example.com
ServerAlias example.com

Here You need to Put your doamin in (in Server name With www and in ServerAlias Name without www) my case it's 'subho.com'

ServerName www.subho.com

Step 2:

ServerAdmin contact@example.com

in this section you need to put your email in my case it is 'subho1307@gmail.com'

ServerAdmin subho1307@gmail.com

Now let me tell you the use of the email . If your webserver or user faced any problem it will show your email to the user and tell them to contact you to fix the problem.

Step 3:

DocumentRoot /var/www/project_folder_name
Alias /static /var/www/project_folder_name/static
<Directory /var/www/project_folder_name/static>
Alias /media /var/www/project_folder_name/media
<Directory /var/www/project_folder_name/media>

Here I am showing some different line code and here is a keyword that is 'project_folder_name' and there some more keyword in your just change it with your projectname in my case this is 'myproject' so I will replace all 'project_folder_name' with 'myproject'. After There YOu will find one more keyword that will be 'inner_project_folder_name' there you will have provide that folder name which contains 'settings.py' in my case that is also same 'myproject' so I will replace all of them with that.

Step 4 (final):

    WSGIDaemonProcess project_folder_name python-home=/var/www/project_folder_name/virtualenv_name python-path=/var/www/project_folder_name

in this line of code here is a keyword call 'virtualenv_name' just replace it with your virtualenv name in my case it's 'myvnv' . After Making all changes it should look like this .

<VirtualHost *:80>
   ServerName www.subho.com
   ServerAlias subho.com
   ServerAdmin subho1307@gmail.com
   DocumentRoot /var/www/myproject
   ErrorLog ${APACHE_LOG_DIR}/error.log
   CustomLog ${APACHE_LOG_DIR}/access.log combined
   
   Alias /static /var/www/myproject/static
   <Directory /var/www/myproject/static>
       Require all granted
   </Directory>
   
   Alias /media /var/www/myproject/media
   <Directory /var/www/myproject/media>
       Require all granted
   </Directory>
   
   <Directory /var/www/myproject/myproject>
       <Files wsgi.py>
           Require all granted
       </Files>
   </Directory>
   
   WSGIDaemonProcess myproject python-home=/var/www/myproject/myvnv python-path=/var/www/myproject
   WSGIProcessGroup myproject
   WSGIScriptAlias /  /var/www/myproject/myproject/wsgi.py
</VirtualHost>

Now we are almost done , next we will check that your conf code is correct or not and then we will active it , so in order to do that run this

sudo apache2ctl configtest

and if everything is fine then we will activeate this conf file.

sudo a2ensite your_domain.conf

in my case my conf file name was 'subho.com.conf'

sudo a2ensite subho.com.conf

Now we need to make some more small chnages then we can depoly our project so to order to do that run this following command

cd /etc/apache2
sudo nano apache2.conf

and then go at the bottom of the file and then paste this :-

WSGIApplicationGroup %{GLOBAL}

Then exit from the nano editor after saveing the changes ( I hope you know how to do that ). Now restart Apache server

sudo service apache2 restart

Now We are all done with apache , and now let's look at our django project and make some small changes in that project with some following steps. If you every get any error in this apache server then check the error log with this command

cd /var/log/apache2
cat error.log

** Use This command as sudo ** To clear the error log you can run this

sudo bash -c 'echo > /var/log/apache2/error.log'

Step 1:

Go to our settings.py file and open it in nano editor ( or any other text editor)
cd /var/www/myproject/myproject
sudo nano sudo nano settings.py

Step 2:

Make The following Changes
ALLOWED_HOST = ["your_domain"]
  • Type Your Domain Here in my case this is 'subho.com' so it should look like this
  •     ALLOWED_HOST = ["subho.com","www.subho.com"]
    
  • I have't any domain then write your IP address of your VPS here ( type 'ifconfig' in your terminal of vps to know our ip)
  •         ALLOWED_HOST = ["192.168.0.1"]
    

Step 3:

Write this in to Your settings.py
DEBUG = False
STATIC_ROOT = BASE_DIR / 'static'

and then save and exit from the text editor

Step 4:

Now ativate your virtual env and collect staticfiles
source myvnv/bin/activate

and then run this

python manage.py collectstatic

If you get any error after running this command you make a googel search for it or you can message me in this repo.

After all of that we need to restart our apache server and then we can see that our site is on live.

to do that run this command

sudo service apache2 restart

After That you can open the web address and you will able to see your site. But currently I will run on http not https so we need to enable SSL for our site , and keep in mind you can't work in your site due to uncifgured data base , so will do everythig one by one . First let's active SSL

Enableing SSL

Here We will enable SSL for our site in few easy and simple steps

  • First We need to install "certbot" to do that run this follwing command

    apt install certbot python3-certbot-apache
  • Open Your Site Config file of apache ( where We written code for our site)

syntax: sudo nano /etc/apache2/sites-available/your_domain.conf
example: sudo nano /etc/apache2/sites-available/subho.com.conf
  • Now comment The Last 3 Lines by useing '#' (excpet '')
 #WSGIDaemonProcess myproject python-home=/var/www/myproject/myvnv python-path=/var/www/myproject
 #WSGIProcessGroup myproject
 #WSGIScriptAlias /  /var/www/myproject/myproject/wsgi.py
  • Verify Web Server Ports are Open and Allowed through Firewall
  ufw status verbose

If you Don't see apache name after running this command the run the follwing command then run the command

sudo ufw allow "Apache Full"
  • Now Run This command and you will see all domains that are pointed to your vps chose one or multipule. ** in our case we will see two options (ex: www.subho.com and subho.com) type both option number with coma and press enter
certbot --apache
  • After Doing That Open your site conf file Again And uncomment The lines
  • Next There will be another new file with almost same name of your conf file (end with '-le-ssl.conf') open that file in text editor .
  • Uncommnet the same 3 lines and change your project name or add any extra letters in your project name (ex: my project name was 'myproject' and I made it 'myprojects')
WSGIDaemonProcess myprojects python-home=/var/www/myproject/myvnv python-path=/var/www/myproject
WSGIProcessGroup myprojects
WSGIScriptAlias /  /var/www/myproject/myproject/wsgi.py
  • Now Restart Your Apache To see the changes
    sudo service apache2 restart
    ** Now You Have enabled SSL To your Site ,Now You need to configure Data base To use Your site properly Now Chose any Data Base Opetion From Following to do that

Auto Restart

Now Everything is working fine now we need to make enable auto restart on to apache server so that if we boot our vps apache will start automatically. just run the following command

sudo systemctl enable apache2

Configure Data Base