deploy django app with apache server on ubuntu
In this article we will learn about how to deploy django app on apache server. Apache server is a free and open-source HTTP server that is free to use. It is a cross platform web-server, It support's most of the operating systems like windows, linux, Mac, etc. Apache server is not intended to use to create any website. The main purpose of apache is to redirect HTTP requests to other applications like WSGI then these will process the request. If it is a static content then we can serve the resources directly with apache server.
Advantages of Apache Server
- It is free and opensource
- Easy process to install and configure apache
- Changes will be reflected immediately
- Wide range of community support from forums like stackoverflow.com
- Support for hosting multiple websites on single server
How to install apache server on ubuntu ?
Run the below command to install the apache2 server and mod_wsgi. mod_wsgi supports the apache to run python application.
sudo apt-get install apache2 libapache2-mod-wsgi
Basic Apache configuration file todeploy django application
<VirtualHost *:80> ServerName example.com ServerAdmin email@example.com DocumentRoot /path/to/project Alias /static/ /path/to/static/ Alias /media/ /path/to/media/ WSGIScriptAlias / /path/to/project/project/wsgi.py WSGIDaemonProcess example.com python-path=/path/to/project \ python-home=/path/to/env3 processes=5 threads=2 WSGIProcessGroup example.com <Directory /path/to/project/> Options -Indexes Order deny,allow Allow from all Require all granted </Directory> <Directory /path/to/media> Options -Indexes Order deny,allow # Allow from all Require all granted </Directory> LogLevel info # PROJECT_NAME is used to seperate the log files of this application ErrorLog /path/to/logs/error.log CustomLog /path/to/logs/access.log combined </VirtualHost>
After installing it, follow the below procedure
- Go to the directory "/etc/apache2/sites-available/" and create a configuration file named "my_apache_website.conf" and copy the above configuration.
- Update the above configuration file by replacing the paths "static", "media", "project", "virtualenv", "document root", "error_log" and "access log" with actual paths and also update "server name", "server admin"
- Test the configuration file with the command "apachectl configtest"
- Now, restart the apache server with the command "sudo service apache2 restart"
- Check the status of the apache server with the command "sudo service apache2 status"
- We have successfully configured the django application with apache. Now, we can access the django web application(example.com) in the browser.
Explanation of apache config:
- ServerName: It is the domain name which is used by apache to route the incoming requests.
- ServerAdmin: It is used by apache to send error reports to the admin.
- Alias: It takes a url path and substitutes it with a file or a directory path on the system.
- WSGIScriptAlias: It takes url path and passes the request to the WSGI application if the request matches with the path.
- WSGIDaemonProcess:It specifies the distinct daemon processes should be created when starting the WSGI application.
- python-path: It specifies the project path, where the project is located in the file system.
- python-home:It specifies the path to virtual python environment.
- processes:It's a number which specifies the number of daemon processes will be started in the process group.
- threads: It's a number which specifies the number of threads that will be created with each process to handle the requests in each daemon process in the process group.
- WSGIProcessGroup: It is used to specify the group name of the WSGI application in which the application daemon processes will run under the group.
- Directory: It takes the directory path and it specifies the access permissions to the directory.
- LogLevel: It is used to format the log information based on its value.
- ErrorLog: It takes the log file path in which the error log will be stored.
- CustomLog: It takes the log file path in which the access log will be stored.