To install Django on Ubuntu 20.04 with Nginx as a reverse proxy, you'll need to follow these steps:
1. Update the system packages:
sudo apt update
sudo apt upgrade
2. Install pip (Python package installer) and other dependencies:
sudo apt install python3-pip python3-dev libpq-dev nginx
3. Create a Python virtual environment for your Django project:
sudo apt install python3-venv
mkdir ~/myproject
cd ~/myproject
python3 -m venv myprojectenv
4. Activate the virtual environment:
source myprojectenv/bin/activate
5. Install Django and Gunicorn (a WSGI HTTP server for running Django applications):
pip install django gunicorn
6. Start a new Django project:
django-admin startproject myproject
7. Configure Django to use a PostgreSQL database (optional):
- Install PostgreSQL and its dependencies:
sudo apt install postgresql postgresql-contrib
- Create a new PostgreSQL database and user:
sudo -u postgres psql
CREATE DATABASE myprojectdb;
CREATE USER myprojectuser WITH PASSWORD 'password';
GRANT ALL PRIVILEGES ON DATABASE myprojectdb TO myprojectuser;
\q
- Update the database settings in the Django project's `settings.py` file:
cd myproject
nano settings.py
Update the following settings:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'myprojectdb',
'USER': 'myprojectuser',
'PASSWORD': 'password',
'HOST': 'localhost',
'PORT': '',
}
}
8. Collect the static files for your Django project:
python manage.py collectstatic
9. Test your Django project by running the development server:
python manage.py runserver
Visit http://localhost:8000 in your web browser to see if the Django application is working correctly. Press `Ctrl + C` to stop the development server.
10. Configure Nginx as a reverse proxy:
- Create a new Nginx configuration file:
sudo nano /etc/nginx/sites-available/myproject
- Add the following configuration (replace `your_domain` with your actual domain name or IP address):
server {
listen 80;
server_name your_domain;
location / {
proxy_pass http://localhost:8000;
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;
}
}
- Enable the Nginx site:
sudo ln -s /etc/nginx/sites-available/myproject /etc/nginx/sites-enabled/
- Test the Nginx configuration:
sudo nginx -t
- Restart Nginx:
sudo systemctl restart nginx
11. Access your Django application through the Nginx reverse proxy. Visit `http://your_domain` in your web browser, and you should see your Django application running.
That's it! You have successfully installed Django on Ubuntu 20.04 with Nginx as a reverse proxy
No comments:
Post a Comment