How to Deploy Django 3 on Ubuntu 18.04
Introduction
Deploying a Django application can be a rewarding experience, allowing developers to share their applications with the world. This tutorial aims to guide you through the process of deploying Django 3 on Ubuntu 18.04, ensuring that your application runs smoothly and efficiently.
Prerequisites
- Basic knowledge of Python and Django.
- An Ubuntu 18.04 server with root access.
- Python 3.6 or later installed on your server.
- PostgreSQL (or another database) installed for production use.
- Familiarity with the command line and SSH.
Main Steps
Step 1: Update Your Server
sudo apt update && sudo apt upgrade -y
Start by updating your package index and upgrading any existing packages to ensure your server is running the latest software.
Step 2: Install Required Packages
You'll need to install several packages, including Python, pip, and virtualenv.
sudo apt install python3-pip python3-dev libpq-dev nginx curl
Step 3: Create a Virtual Environment
Creating a virtual environment helps to manage dependencies for your Django project.
python3 -m venv myprojectenv
Activate your virtual environment:
source myprojectenv/bin/activate
Step 4: Install Django and Gunicorn
Once the virtual environment is active, install Django and Gunicorn.
pip install django gunicorn
Step 5: Create a Django Project
Now, create your Django project if you havenβt done so already.
django-admin startproject myproject
Change into the project directory:
cd myproject
Step 6: Configure Your Database
Edit the settings.py file to configure your database settings. If using PostgreSQL, make sure you have it installed and configured.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'mydatabase',
'USER': 'mydatabaseuser',
'PASSWORD': 'mypassword',
'HOST': 'localhost',
'PORT': '',
}
}
Step 7: Collect Static Files
Run the collectstatic command to gather all static files into one directory.
python manage.py collectstatic
Step 8: Migrate Your Database
Run migrations to create the database schema.
python manage.py migrate
Step 9: Test the Development Server
Run the Django development server to test your setup.
python manage.py runserver 0.0.0.0:8000
Visit http://your_server_ip:8000 to see if your project is running.
Step 10: Configure Gunicorn
Run Gunicorn to serve your application.
gunicorn --bind 0.0.0.0:8000 myproject.wsgi:application
Advanced Configuration
Setting Up Nginx as a Reverse Proxy
To serve your Django application over HTTP, configure Nginx as a reverse proxy for Gunicorn.
server {
listen 80;
server_name your_server_ip;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /path/to/your/project;
}
location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
}
Save this configuration in /etc/nginx/sites-available/myproject and enable it:
sudo ln -s /etc/nginx/sites-available/myproject /etc/nginx/sites-enabled
Test Nginx Configuration
Check the configuration for syntax errors.
sudo nginx -t
If everything is okay, restart Nginx:
sudo systemctl restart nginx
Securing Your Application with SSL (Optional)
You can secure your application using Let's Encrypt. Install Certbot:
sudo apt install certbot python3-certbot-nginx
Obtain and install the SSL certificate:
sudo certbot --nginx -d your_domain
Best Practices
- Always use a virtual environment to manage dependencies.
- Keep your Django application and libraries updated.
- Secure your database and use strong passwords.
- Use environment variables to manage sensitive information.
- Implement logging and monitoring for your application.
Troubleshooting
If you encounter issues with Gunicorn not starting, check the permissions on the socket file and ensure Gunicorn is running.
For Nginx, if your site is not reachable, check the configuration syntax and ensure Nginx is running properly.
Database connection issues can often be resolved by checking the settings in the
settings.pyfile and ensuring the database server is running.
Conclusion
Deploying Django on Ubuntu 18.04 requires careful attention to detail, especially regarding configuration and security. By following the steps outlined in this tutorial, you can successfully deploy your Django application and make it accessible to users worldwide. Continue to monitor and update your application regularly to ensure it remains secure and efficient.
Verifikasi Teknis
Panduan ini disusun berdasarkan referensi teknis terbaru. Namun, konfigurasi server dapat bervariasi. Lihat sumber referensi asli β