Web servers on a VPS: nginx, Apache and Gunicorn

A web server turns the files and apps on your VPS into a website. nginx and Apache serve web pages; Gunicorn runs Python web apps (Flask, Django and so on), usually behind nginx. Any of them runs on the cheapest plans. The commands below are for Debian and Ubuntu; on CentOS-style systems swap apt for yum and apache2 for httpd.

nginx

nginx is fast and light on memory. It serves websites, works as a reverse proxy in front of other apps, and can balance load.

sudo apt update
sudo apt install nginx
sudo systemctl enable --now nginx
sudo systemctl status nginx

Open your server's IP address in a browser to see the welcome page. Site settings live in /etc/nginx/sites-available/; after a change run sudo nginx -t to check it and sudo systemctl reload nginx to apply it.

Apache

Apache is the most widely used web server. It supports .htaccess files, which many PHP apps such as WordPress rely on.

sudo apt update
sudo apt install apache2

It starts automatically; visit your server's IP address to see the default page. The main configuration is in /etc/apache2/apache2.conf, and you can serve several websites from one server with virtual hosts in /etc/apache2/sites-available/, enabled with sudo a2ensite <name>.

Gunicorn (for Python apps)

Gunicorn is a Python WSGI server. It starts several worker processes so your app can handle requests in parallel.

sudo apt install gunicorn
gunicorn --bind 127.0.0.1:8000 app:application

Here app is the Python module and application is the app object inside it. For anything more than a test, put the options in a file such as gunicorn.conf.py:

bind = "127.0.0.1:8000"
workers = 3
user = "www-data"

Run it with gunicorn --config gunicorn.conf.py app:application, keep it running with a systemd service, and have nginx pass requests on to port 8000 with proxy_pass. Binding to 127.0.0.1 keeps the app reachable only through nginx.

Setting up WordPress? See how to install WordPress on a VPS.

Need a server for this? Cheapest VPS plans with 1 to 4 GB RAM

In stock when this page loaded. See all 10 matching plans

More RackNerd guides