Guide VPS & Servers

How to install Nginx or Apache

Installing Nginx or Apache on your VPS, checking it serves a page, and where each one keeps its site configuration afterwards.

Updated 8 min read Beginner

Nginx and Apache are the two web servers you will meet almost everywhere, and either one is a perfectly good choice for a general-purpose VPS. This guide installs both, one at a time, so you can pick whichever your stack or your familiarity favours — do not install both together on the same server unless you have deliberately set them up to run on different ports, since by default they both try to claim port 80.

Installing Nginx

sudo apt update
sudo apt install nginx -y      # Ubuntu / Debian

sudo dnf install nginx -y      # RHEL / Rocky / AlmaLinux

Start it and set it to launch automatically on every boot:

sudo systemctl enable --now nginx

Check it is actually running:

sudo systemctl status nginx

If a firewall is active — it should be, see setting up a firewall on your VPS — allow HTTP traffic through it:

sudo ufw allow 'Nginx HTTP'                                   # ufw
sudo firewall-cmd --permanent --add-service=http && sudo firewall-cmd --reload   # firewalld

Visit the server's IP address in a browser. Nginx's default welcome page confirms it is working.

Installing Apache

sudo apt install apache2 -y    # Ubuntu / Debian

sudo dnf install httpd -y      # RHEL / Rocky / AlmaLinux
sudo systemctl enable --now apache2   # Ubuntu / Debian
sudo systemctl enable --now httpd     # RHEL / Rocky / AlmaLinux

Note the package and service name genuinely differ: it is apache2 on Debian-family systems and httpd on RHEL-family systems, both for the package and for every systemctl command afterwards. Allow HTTP through the firewall the same way as above, then visit the server's IP address — you should see Apache's own default page.

Where each one keeps its configuration

NginxApache (Debian/Ubuntu)Apache (RHEL family)
Main config/etc/nginx/nginx.conf/etc/apache2/apache2.conf/etc/httpd/conf/httpd.conf
Site configs/etc/nginx/sites-available//etc/apache2/sites-available//etc/httpd/conf.d/
Enabled sites/etc/nginx/sites-enabled/ (symlinks)/etc/apache2/sites-enabled/, via a2ensiteAnything in conf.d/ is active
Default web root/var/www/html/var/www/html/var/www/html
Log files/var/log/nginx//var/log/apache2//var/log/httpd/

Testing config changes before you reload

Both web servers can check a configuration file for syntax errors without applying it — always run this before reloading, since a broken config left applied means a web server that refuses to (re)start at all:

sudo nginx -t
sudo apache2ctl configtest      # Debian / Ubuntu
sudo apachectl configtest       # RHEL / Rocky / AlmaLinux

Once a config passes, apply it with a reload rather than a full restart where possible — a reload re-reads configuration without dropping active connections:

sudo systemctl reload nginx
sudo systemctl reload apache2   # or httpd
Nginx and Apache handle dynamic content differently

Nginx does not run PHP itself — it hands requests off to a separate PHP-FPM process over a socket or a port. Apache can run PHP the same way, or via an embedded module (mod_php) depending on how it is installed. This matters when you get to installing a LAMP stack, since the PHP setup step differs slightly between the two.

A basic hardening step worth doing immediately

Both web servers reveal their exact version number in response headers by default, which is minor but genuinely unnecessary information to hand out to anyone who asks. Hide it:

# Nginx — in the http {} block of nginx.conf
server_tokens off;
# Apache — in apache2.conf or httpd.conf
ServerTokens Prod
ServerSignature Off

Reload the service after either change to apply it. This does not meaningfully stop a determined attacker — version fingerprinting has other methods — but there is no good reason to volunteer the information either.

Checking the version you installed

nginx -v
apache2 -v      # Debian / Ubuntu
httpd -v        # RHEL / Rocky / AlmaLinux

Worth doing once at the start, since the version your distribution's default repository provides changes over time and differs between Ubuntu, Debian and the RHEL family — if a specific feature or module version matters for what you plan to run, confirm it here before building anything on top of it.

Which one should you actually pick?

Both are mature, well-documented, and capable of running anything from a static site to a busy application. The practical differences that tend to matter for a general VPS:

  • Nginx configuration is more explicit and, once learned, often easier to reason about for reverse proxying and serving static files efficiently. It is also frequently placed in front of another application server rather than serving dynamic content directly.
  • Apache has a longer history with .htaccess-based per-directory configuration, which some older applications and shared-hosting-era tutorials assume is available. If you are following documentation written for cPanel-era shared hosting, it likely assumes Apache and .htaccess.

If you genuinely have no preference, either is a reasonable default. Once one is installed and confirmed serving the default page, the next steps are usually pointing a real domain at it and adding a free SSL certificate.

Related reading