How to Deploy a Web Application on Ubuntu using Nginx (Step-by-Step Guide)
Deploying a web application can feel complex, but with the right approach, it becomes simple and efficient. In this guide, we will walk through how to deploy your web application on an Ubuntu server using Nginx.
🌐 What is Nginx?
Nginx is a high-performance web server that is widely used for serving static content, load balancing, and acting as a reverse proxy. It is known for its speed, stability, and low resource usage.
⚙️ Step 1: Update Your Server
Before starting, make sure your server is up to date.
sudo apt update
sudo apt upgrade -y
📦 Step 2: Install Nginx
Install Nginx using the following command:
sudo apt install nginx -y
After installation, verify that Nginx is running:
sudo systemctl status nginx
Open your browser and visit your server IP to confirm Nginx is working.
📁 Step 3: Upload Your Project
You can upload your project using Git or by transferring files manually.
git clone https://github.com/your-repo.git
cd your-project
🐍 Step 4: Setup Backend (Django Example)
Create a virtual environment and install dependencies:
sudo apt install python3-pip python3-venv -y
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt
Apply migrations and collect static files:
python manage.py migrate
python manage.py collectstatic
Run the development server to test:
python manage.py runserver 0.0.0.0:8000
🔁 Step 5: Use Gunicorn
Install Gunicorn and run your app:
pip install gunicorn
gunicorn --bind 0.0.0.0:8000 yourproject.wsgi:application
⚡ Step 6: Configure Nginx
Create a new configuration file:
sudo nano /etc/nginx/sites-available/myproject
Add the following configuration:
server {
listen 80;
server_name yourdomain.com;
location /static/ {
root /home/ubuntu/your-project;
}
location /media/ {
root /home/ubuntu/your-project;
}
location / {
proxy_pass http://127.0.0.1:8000;
include proxy_params;
}
}
🔗 Step 7: Enable Configuration
sudo ln -s /etc/nginx/sites-available/myproject /etc/nginx/sites-enabled
sudo nginx -t
sudo systemctl restart nginx
🔐 Step 8: Secure with SSL
Install Certbot and enable HTTPS:
sudo apt install certbot python3-certbot-nginx -y
sudo certbot --nginx
🎯 Final Architecture
User Request → Nginx → Gunicorn → Application Server
⚠️ Common Errors
-
502 Bad Gateway: Gunicorn not running
-
Static files not loading: Check collectstatic and paths
-
Permission issues: Fix using chmod or chown
🏁 Conclusion
Deploying a web application using Nginx on Ubuntu is a powerful and scalable solution. Whether you are building a portfolio, blog, or SaaS platform, this setup ensures performance, security, and reliability.
Start deploying today and take your application live 🚀