Deploy a web application without Docker: complete guide with PM2 and Nginx
Introduction
Docker is often presented as the essential solution for deployment. But there is a more direct approach: install the tools directly on the server, manage the processes with PM2 and expose the application with Nginx.
This approach is simpler to understand, lighter on resources and perfectly suited for a VPS server or a local Ubuntu machine.
In this guide, we deploy a Python/FastAPI backend and a Next.js frontend on Ubuntu 24.04.
Prerequisites
- An Ubuntu 24.04 server (VPS or local VM)
- Root or sudo access
- Projects hosted on GitLab or GitHub
1. System update
Before any installation, update the system packages:
sudo apt update && sudo apt upgrade -y
2. Install Nginx
Nginx plays the role of reverse proxy. It receives requests on port 80 and redirects them to the correct service, port 3000 for the frontend, port 8000 for the backend. Without Nginx, you would have to expose each service on its own port.
sudo apt install nginx -y
sudo systemctl start nginx
sudo systemctl enable nginx
3. Install Node.js 20
Node.js is the runtime environment needed to run the Next.js frontend.
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo bash -
sudo apt install nodejs -y
node -v
npm -v
4. Install Python 3
Python is the FastAPI backend language. Venv allows you to isolate project dependencies to avoid conflicts with system packages.
sudo apt install python3 python3-pip python3-venv -y
python3 --version
5. Install MongoDB
MongoDB stores all application data.
curl -fsSL https://www.mongodb.org/static/pgp/server-7.0.asc | sudo gpg -o /usr/share/keyrings/mongodb-server-7.0.gpg --dearmor
echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-7.0.gpg ] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/7.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list
sudo apt update
sudo apt install mongodb-org -y
sudo systemctl start mongod
sudo systemctl enable mongod
Check that MongoDB is running:
sudo systemctl status mongod
6. Install PM2
PM2 is a process manager. It keeps the frontend and backend active at all times, automatically restarts them in case of a crash, and restarts them when the server starts.
sudo npm install -g pm2
pm2 --version
7. Install Certbot
Certbot automatically generates and renews SSL certificates to enable HTTPS.
sudo apt install certbot python3-certbot-nginx -y
8. Install Git and clone projects
sudo apt install git -y
cd /var/www
sudo git clone https://gitlab.com/ton-username/ton-backend.git
sudo git clone https://gitlab.com/ton-username/ton-frontend.git
GitLab no longer accepts passwords for Git authentication. You must create a personal access token from GitLab β Edit profile β Access β Personal access tokens β Generate token β Legacy token with the read_repository scope. Use this token as password when cloning.
Fix permissions after cloning, without this step, pip install and npm install return a permission error:
sudo chown -R $USER:$USER /var/www/ton-backend
sudo chown -R $USER:$USER /var/www/ton-frontend
9. Configure the backend```bash
cd /var/www/ton-backend python3 -m venv env source env/bin/activate pip install -r requirements.txt
Create the `.env` file:
```bash
nano .env
MONGO_URI=mongodb://localhost:27017
MONGO_DB=mybase
SECRET_KEY=a_very_long_and_random_secret
ALGORITHM=HS256
ACCESS_TOKEN_EXPIRE_MINUTES=60
MAIL_FROM=ton@email.com
ALLOWED_ORIGINS=["http://localhost:3000"]
10. Launch the backend with PM2
cd /var/www/ton-backend
source env/bin/activate
pm2 start "uvicorn app.main:app --host 0.0.0.0 --port 8000" --name my-backend
pm2 save
pm2 startup
PM2 displays a copy and paste command to activate automatic startup at boot:
sudo env PATH=$PATH:/usr/bin /usr/lib/node_modules/pm2/bin/pm2 startup systemd -u ton-user --hp /home/ton-user
Check that the backend responds:
curl http://localhost:8000/
11. Configure the frontend
cd /var/www/ton-frontend
nano .env.local
NEXT_PUBLIC_API_URL=http://localhost:8000
Install dependencies and builder:
npm install
npm run build
12. Launch the frontend with PM2
pm2 start "npm start" --name my-frontend
pm2 save
Check that both processes are running:
pm2 status
Expected result:
ββββββ¬βββββββββββββββββββ ββ¬βββββββββββ¬ββββββββββββ
β id β name β mode β status β
ββββββΌβββββββββββββββββββ ββΌβββββββββββΌββββββββββββ€
β 0 β my-backend β fork β online β
β 1 β my-frontend β fork β online β
ββββββ΄βββββββββββββββββββ ββ΄βββββββββββ΄ββββββββββββ
13. Configure Nginx
sudo nano /etc/nginx/sites-available/myapp
server {
listen 80;
server_name mydomain.com www.mydomain.com;
location / {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
location /api/ {
proxy_pass http://localhost:8000/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
Activate configuration:
sudo ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled/
sudo rm /etc/nginx/sites-enabled/default
sudo nginx -t
sudo systemctl reload nginx
14. Configure DNS
On your registrar (Hostinger, OVH, etc.), add these A records:
| Type | Name | Value |
|---|---|---|
| A | @ | PUBLIC_IP_OF_SERVER |
| A | www | PUBLIC_IP_OF_SERVER |
DNS propagation can take between 10 minutes and 24 hours.
15. Enable SSL with Certbot
Once the DNS is propagated:
sudo certbot --nginx -d mydomain.com -d www.mydomain.com
Certbot automatically modifies the Nginx configuration to enable HTTPS and configures automatic certificate renewal.
Useful PM2 commands
pm2 status # see process status
pm2 logs my-backend # see logs in real time
pm2 restart my-backend # restart a process
pm2 stop my-backend # stop a process
pm2 delete my-backend # delete a process
ConclusionThis Docker-free approach is ideal for understanding the basics of deployment. Each component is visible and controllable directly on the server. PM2 handles process persistence, Nginx handles traffic routing, and Certbot handles SSL.
To go further, the following article explains how to automate updates with GitLab CI/CD so you never have to manually connect to the server again.