4.6 KiB
Installing Packages from Ubuntu Repositories
-
Update your Ubuntu system:
sudo apt update -
Install necessary packages:
sudo apt install python3-venv python3-dev libpq-dev postgresql postgresql-contrib nginx curl
Creating the PostgreSQL Database and User
-
Log into an interactive Postgres session:
sudo -u postgres psql -
Inside the PostgreSQL prompt, create a database for your project:
CREATE DATABASE myproject; -
Create a database user for your project with a secure password:
CREATE USER myprojectuser WITH PASSWORD 'password'; -
Modify connection parameters for the user:
ALTER ROLE myprojectuser SET client_encoding TO 'utf8'; ALTER ROLE myprojectuser SET default_transaction_isolation TO 'read committed'; ALTER ROLE myprojectuser SET timezone TO 'UTC'; -
Grant the new user access to administer the new database:
GRANT ALL PRIVILEGES ON DATABASE myproject TO myprojectuser; -
Exit the PostgreSQL prompt:
\q
Creating a Python Virtual Environment for Your Project
-
Create a directory for your project files:
mkdir ~/myprojectdir cd ~/myprojectdir -
Create a Python virtual environment:
python3 -m venv myprojectenv -
Activate the virtual environment:
source myprojectenv/bin/activate -
Install Django, Gunicorn, and psycopg2:
pip install django gunicorn psycopg2-binary
Creating and Configuring a New Django Project
-
Create a new Django project with a defined directory:
django-admin startproject myproject ~/myprojectdir -
Adjust settings in the
settings.pyfile:- Set
ALLOWED_HOSTSto['*'] - Configure
DATABASESwith PostgreSQL details
- Set
-
Add static root configuration to
settings.py:import os STATIC_ROOT = os.path.join(BASE_DIR, 'static/') -
Migrate initial database schema:
~/myprojectdir/manage.py makemigrations ~/myprojectdir/manage.py migrate -
Create an administrative user:
~/myprojectdir/manage.py createsuperuser -
Collect static content:
~/myprojectdir/manage.py collectstatic -
Allow port 8000:
sudo ufw allow 8000 -
Start Django development server:
~/myprojectdir/manage.py runserver 0.0.0.0:8000 -
Access your application in a web browser.
-
Stop Apache2 service:
sudo /etc/init.d/apache2 restart
Testing Gunicorn
-
Test Gunicorn to ensure it can serve the application:
cd ~/myprojectdir gunicorn --bind 0.0.0.0:8000 myproject.wsgi -
Stop Gunicorn:
- Press
CTRL-C
- Press
Creating systemd Socket and Service Files for Gunicorn
-
Create a systemd socket file for Gunicorn:
sudo nano /etc/systemd/system/gunicorn.socket -
Create and open a systemd service file for Gunicorn:
sudo nano /etc/systemd/system/gunicorn.service -
Configure the service file with appropriate details.
Checking for the Gunicorn Socket File
-
Check the status of the Gunicorn socket:
sudo systemctl status gunicorn.socket -
Check for the existence of the Gunicorn socket file:
file /run/gunicorn.sock -
Check Gunicorn socket logs:
sudo journalctl -u gunicorn.socket
Testing Socket Activation
-
Test the socket activation mechanism:
sudo systemctl status gunicorn -
Send a connection to the socket through curl:
curl --unix-socket /run/gunicorn.sock localhost
Configuring Nginx to Proxy Pass to Gunicorn
-
Create and open a new server block in Nginx's sites-available directory:
sudo nano /etc/nginx/sites-available/myproject -
Configure the server block with appropriate settings.
-
Enable the server block:
sudo ln -s /etc/nginx/sites-available/myproject /etc/nginx/sites-enabled -
Test Nginx configuration for syntax errors:
sudo nginx -t -
Restart Nginx:
sudo systemctl restart nginx -
Open firewall to normal traffic on port 80:
sudo ufw delete allow 8000 sudo ufw allow 'Nginx Full'
Conclusion
You should now be able to access your Django application via your server's domain or IP address.