Deploying a Django project with Nginx on Ubuntu usually involves the following steps:
1. Install necessary software
You need to install Nginx and some Python environment tools (such as pip, virtualenv) to run Django.
sudo apt update
sudo apt install nginx
sudo apt install python3-pip python3-venv
2. Create a Django project (if you haven't already)
If you haven't created a Django project yet, you can create a new one with the following steps.
# Create and activate a virtual environment
source myenv/bin/activate
# Install Django
pip install django
# Create a Django project
django-admin startproject myproject
cd myproject
3. Configure the Django project
Modify the myproject/settings.py file and make sure you have correctly configured ALLOWED_HOSTS and DATABASES for the production environment.
ALLOWED_HOSTS = ['your_domain_or_ip', 'localhost']
4. Install Gunicorn
Gunicorn is a WSGI HTTP server that can run Django applications.
pip install gunicorn
5. Start Gunicorn
You can start a Django project with Gunicorn.
# Start Gunicorn in the project directory
gunicorn --workers 3 myproject.wsgi:application
Gunicorn runs on port 8000 by default, and you can access your Django application at http://your_domain_or_ip:8000.
6. Configure Nginx
Nginx will act as a reverse proxy and forward requests to Gunicorn. First, create a new Nginx configuration file.
sudo nano /etc/nginx/sites-available/myproject
Add the following to the file:
server {
listen 80;
server_name your_domain_or_ip;
location / {
proxy_pass http://127.0.0.1:8000; # Gunicorn’s address
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
location /static/ {
alias /path/to/your/static/directory; # Points to the static directory of the Django project
}
location /media/ {
alias /path/to/your/media/directory; # points to the media directory of the Django project
}
}
7. Enable Nginx configuration
Create a symbolic link to link the configuration file to the sites-enabled directory.
sudo ln -s /etc/nginx/sites-available/myproject /etc/nginx/sites-enabled
8. Test Nginx configuration
Before reloading Nginx, make sure there are no syntax errors.
sudo nginx -t
9. Reload Nginx
If there are no errors, reload the Nginx configuration to apply the changes.
sudo systemctl restart nginx
10. Collect static files
Django projects need to collect static files into a common directory so that Nginx can serve them.
python manage.py collectstatic
11. Configure Gunicorn as a background process
In order to make Gunicorn run in the background, it is recommended to use systemd to manage the Gunicorn process.
Create a systemd service file:
sudo nano /etc/systemd/system/gunicorn.service
Add the following content:
[Unit]
Description=gunicorn daemon for Django project
After=network.target
[Service]
User=your_user
Group=your_group
WorkingDirectory=/path/to/your/django/project
ExecStart=/path/to/your/virtualenv/bin/gunicorn --workers 3 --bind unix:/path/to/your/django/project/gunicorn.sock myproject.wsgi:application
[Install]
WantedBy=multi-user.target
Start the Gunicorn service:
sudo systemctl start gunicorn
sudo systemctl enable gunicorn
12. Access your Django application
You should now be able to access your Django application through Nginx. Open your browser and visit http://your_domain_or_ip. You should see the default Django welcome page.
These are the basic steps to deploy a Django project with Nginx on Ubuntu. If you have more configuration requirements or need to optimize performance, you can further adjust the configuration of Gunicorn and Nginx.