
Apache and Nginx are the two most popular web servers on Linux, and either can host a website on a VPS in a few minutes. This guide walks through installing each on Ubuntu or Debian (with the Rocky Linux and AlmaLinux commands alongside), opening the firewall, hosting your own domain with a virtual host or server block, adding free HTTPS, and choosing between the two. It also shows how to run Nginx in front of Apache on the same server.
Quick answerOn Ubuntu, run sudo apt install nginx (or apache2), allow ports 80 and 443 in the firewall, put your site in /var/www/example.com, create a server block for your domain, then run certbot to enable HTTPS. Install only one of the two unless you deliberately set one up as a proxy for the other, because both want port 80.
Before you start
- A Linux VPS or dedicated server with root or sudo access.
- A domain name with an A record pointing to the server's IP address (needed for HTTPS).
- SSH access to the server.
Step 1: Update the server
# Ubuntu / Debian
sudo apt update && sudo apt upgrade -y
# Rocky Linux / AlmaLinux
sudo dnf upgrade -y
Step 2: Install Apache
# Ubuntu / Debian
sudo apt install apache2 -y
sudo systemctl enable --now apache2
# Rocky Linux / AlmaLinux (the package is called httpd)
sudo dnf install httpd -y
sudo systemctl enable --now httpd
Open http://your-server-ip in a browser. You should see the Apache test page.
Step 3: Or install Nginx
# Ubuntu / Debian
sudo apt install nginx -y
sudo systemctl enable --now nginx
# Rocky Linux / AlmaLinux
sudo dnf install nginx -y
sudo systemctl enable --now nginx
Browse to the server's IP again to see the "Welcome to nginx" page.
Step 4: Open the firewall
# Ubuntu with UFW
sudo ufw allow OpenSSH
sudo ufw allow 'Nginx Full' # or: sudo ufw allow 'Apache Full'
sudo ufw enable
# Rocky / AlmaLinux with firewalld
sudo firewall-cmd --permanent --add-service=http --add-service=https
sudo firewall-cmd --reload
Always allow SSH before enabling UFW so you do not lock yourself out.
Step 5: Host your own site
Create a folder for the site and a test page:
sudo mkdir -p /var/www/example.com
echo '<h1>It works</h1>' | sudo tee /var/www/example.com/index.html
Apache virtual host (Ubuntu/Debian)
Create /etc/apache2/sites-available/example.com.conf:
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example.com
ErrorLog ${APACHE_LOG_DIR}/example.com-error.log
CustomLog ${APACHE_LOG_DIR}/example.com-access.log combined
</VirtualHost>
sudo a2ensite example.com.conf
sudo apache2ctl configtest
sudo systemctl reload apache2
On Rocky or AlmaLinux, put the same block in /etc/httpd/conf.d/example.com.conf (with log paths under /var/log/httpd/) and reload httpd.
Nginx server block
Create /etc/nginx/sites-available/example.com on Ubuntu/Debian, or /etc/nginx/conf.d/example.com.conf on Rocky/AlmaLinux:
server {
listen 80;
server_name example.com www.example.com;
root /var/www/example.com;
index index.html index.php;
location / {
try_files $uri $uri/ =404;
}
}
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/ # Ubuntu/Debian only
sudo nginx -t
sudo systemctl reload nginx
On Rocky and AlmaLinux, SELinux may block files outside the default locations; run sudo restorecon -Rv /var/www/example.com if you see permission errors.
Step 6: Enable HTTPS with Let's Encrypt
# Nginx
sudo apt install certbot python3-certbot-nginx -y
sudo certbot --nginx -d example.com -d www.example.com
# Apache
sudo apt install certbot python3-certbot-apache -y
sudo certbot --apache -d example.com -d www.example.com
Certbot obtains a free certificate, updates your configuration and sets up automatic renewal. Test renewal with sudo certbot renew --dry-run. On Rocky/AlmaLinux, install certbot from the EPEL repository.
Apache vs Nginx: which should you choose?
- Nginx uses an event-driven design that handles many simultaneous connections with little memory. It is excellent for static files, as a reverse proxy in front of Node.js, Python or Docker apps, and for high-traffic sites. PHP runs through PHP-FPM.
- Apache is extremely flexible, with a huge range of modules and support for per-directory
.htaccessfiles. Many PHP applications and shared-hosting style setups (WordPress with plugins that write .htaccess rules, cPanel) expect Apache.
For a new VPS running a single modern site or app, Nginx is usually the better default. If your application relies on .htaccess rules, choose Apache.
Running Nginx and Apache on the same server
You can combine them: Nginx listens on ports 80 and 443, serves static files, and passes everything else to Apache running on another port, such as 8080. Change Apache's Listen 80 to Listen 127.0.0.1:8080 (in /etc/apache2/ports.conf and the virtual host), then in the Nginx server block use:
location / {
proxy_pass http://127.0.0.1:8080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
Enable Apache's remoteip module so its logs show visitors' real IP addresses instead of 127.0.0.1.
Frequently asked questions
Can I install Apache and Nginx on the same server?
Yes, but not both on port 80. Run Nginx on 80/443 as a reverse proxy and Apache on an internal port such as 8080.
Which is faster, Apache or Nginx?
Nginx is generally faster for static content and under many simultaneous connections. For dynamic PHP pages, the difference is small and depends more on PHP, caching and the database.
Where do I upload my website files?
Into the document root you set, for example /var/www/example.com, using SFTP or rsync. Make sure the web server user (www-data on Ubuntu, apache or nginx on Rocky) can read the files.
How much RAM does a web server need?
A static site runs comfortably on 1 GB. A WordPress or PHP site with a database is happier with 2 GB or more.


