Skip to content
Tutorials Center
My learning

Topics

Lesson 2 of 30 ยท First Steps

Installing and Finding Your Way Around

Install Nginx and Apache on Debian, Ubuntu and RHEL, learn where config, logs and web roots live, test config safely and reload with systemctl or Docker.

Installing a web server takes one command. The part that trips people up is what happens next: where did the configuration go, which file do I edit, how do I check my change before it breaks the site, and how do I apply it? The answers differ between Linux families, and between Nginx and Apache, so a map of the file system is worth more than any single command.

This lesson covers the packages, the two main layouts (Debian-style sites-available and sites-enabled, and Red Hat-style conf.d), the daily commands and the official Docker images. The commands run as root on a real server, so they are read-only in this course. To still let you practice, the runnable stand-ins in this lesson are JavaScript scripts that walk a fake directory tree and resolve which files a main config would load.

Installing the packages

Both servers are in every major distribution's repositories. On Debian and Ubuntu the package manager is apt:

Terminal
sudo apt update
sudo apt install nginx          # or: sudo apt install apache2

On the Red Hat family (RHEL, Rocky Linux, AlmaLinux, Fedora, Amazon Linux) it is dnf:

Terminal
sudo dnf install nginx          # or: sudo dnf install httpd

Notice the name difference: the Apache package and service are called apache2 on Debian-based systems and httpd on Red Hat-based ones. They are the same software with different packaging. The Debian packages start the service immediately after installing, and they open nothing in the firewall: if a firewall is active you still have to allow ports 80 and 443 yourself. Package versions follow the distribution, so they can lag behind the newest release. Nginx also publishes its own repository if you need a newer one. See Packages and Services for how package managers and service units work in general.

Check that it is running with the service manager:

Terminal
systemctl status nginx
curl -I http://127.0.0.1/

A default install answers with its welcome page on port 80. If something else already owns port 80, the service fails to start with an "address already in use" error.

Layout 1: Debian and Ubuntu

Debian-family packages split the configuration into "available" and "enabled" directories. You write a site file in sites-available, and enabling it means making a symbolic link to it in sites-enabled. The main file only includes what is enabled, so you can switch a site on and off without deleting anything.

TEXT
/etc/nginx/
โ”œโ”€โ”€ nginx.conf               main file, includes the others
โ”œโ”€โ”€ mime.types
โ”œโ”€โ”€ conf.d/*.conf            extra snippets, all loaded
โ”œโ”€โ”€ sites-available/default  every site you have written
โ”œโ”€โ”€ sites-enabled/default    symlinks to the ones that are live
โ”œโ”€โ”€ snippets/                fragments you include by hand
โ””โ”€โ”€ modules-enabled/         dynamic modules to load

/etc/apache2/
โ”œโ”€โ”€ apache2.conf             main file
โ”œโ”€โ”€ ports.conf               Listen directives
โ”œโ”€โ”€ sites-available/000-default.conf
โ”œโ”€โ”€ sites-enabled/           symlinks (a2ensite / a2dissite)
โ”œโ”€โ”€ mods-available/ mods-enabled/   (a2enmod / a2dismod)
โ””โ”€โ”€ conf-available/ conf-enabled/   (a2enconf / a2disconf)

Apache ships helper commands that create and remove the links for you, so you rarely run ln -s by hand: a2ensite, a2dissite, a2enmod, a2dismod, a2enconf and a2disconf. For Nginx you create the link yourself.

Terminal
sudo ln -s /etc/nginx/sites-available/blog /etc/nginx/sites-enabled/blog
sudo a2ensite blog.conf

Layout 2: Red Hat family

Red Hat-family packages skip the enabled/available dance. The main file includes every file ending in .conf from a drop-in directory, so a site is "enabled" by existing in that directory and "disabled" by renaming it or moving it away.

TEXT
/etc/nginx/
โ”œโ”€โ”€ nginx.conf               main file
โ””โ”€โ”€ conf.d/*.conf            one file per site or topic

/etc/httpd/
โ”œโ”€โ”€ conf/httpd.conf          main file
โ”œโ”€โ”€ conf.d/*.conf            drop-in files (sites, ssl.conf, ...)
โ””โ”€โ”€ conf.modules.d/*.conf    LoadModule lines

Neither layout is better. Debian's lets you stage a change and flip it on atomically. The conf.d style is simpler to automate with configuration management tools: write one file, done. Whichever you use, remember the rule that underlies both: the server only reads what the main file tells it to include. A file in the right directory that is not included does nothing.

Logs and web roots

Where logs and files live also depends on the family:

What Debian / Ubuntu RHEL family
Nginx logs /var/log/nginx/access.log and error.log same
Apache logs /var/log/apache2/ /var/log/httpd/
Default web root /var/www/html Nginx /usr/share/nginx/html, Apache /var/www/html
Service name (Apache) apache2 httpd
Worker user www-data nginx or apache

The worker user matters: workers can only serve files that user can read. Many "403 Forbidden" mysteries are file permissions (see Logs and Troubleshooting).

Our first stand-in is a script that walks a fake Debian-style tree and prints each file with its role. It is the kind of map you build in your head when you log into an unfamiliar server.

JS Walk a fake config tree

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Testing before you apply

The single most useful habit with either server is to test the configuration before you apply it. A typo in an Nginx or Apache file will stop a restart cold and take the site down, but the test command catches it first:

Terminal
sudo nginx -t
# nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
# nginx: configuration file /etc/nginx/nginx.conf test is successful

sudo apachectl configtest        # Debian also has apache2ctl
# Syntax OK

Then apply the change with a reload, not a restart. A reload starts new workers with the new configuration while the old workers finish their current requests, so nobody is dropped. A restart stops everything and starts again.

Terminal
sudo systemctl reload nginx
sudo systemctl reload apache2     # httpd on the RHEL family

The two commands that will carry you for the next thirty lessons are therefore test and reload. If the test fails, the message names the file and line, and the running server is unaffected. Reloads can still fail at runtime for reasons a syntax test cannot see (for example a port already in use or a certificate file that does not exist), so check systemctl status and the error log afterwards.

Other commands you will use constantly:

Terminal
sudo systemctl start nginx
sudo systemctl enable nginx       # start at boot
sudo nginx -T                     # test and dump the whole merged config
sudo apachectl -S                 # show the virtual hosts Apache parsed
sudo apachectl -M                 # list loaded modules
nginx -V                          # version and compile-time options

Docker images

You can also skip the package manager. The official images are nginx and httpd:

Terminal
docker run -d --name web -p 8080:80 nginx
docker run -d --name web2 -p 8081:80 httpd

-p 8080:80 publishes the container's port 80 on the host's port 8080. Inside the Nginx image the config is /etc/nginx/nginx.conf plus /etc/nginx/conf.d/default.conf, and the web root is /usr/share/nginx/html. The Apache image keeps its tree in /usr/local/apache2 (the file is conf/httpd.conf, documents in htdocs). You normally mount your own files over those paths. Containers are covered in Reloads, Zero-Downtime Deploys and Containers and in the Docker tutorial.

Which files does the main config load?

The second stand-in shows the rule from earlier in action: the main file includes a glob, and only the files that match are read. It expands include patterns against a list of file names, in alphabetical order (both servers load glob matches in sorted order, which is why people prefix files with numbers).

JS Resolve include globs

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Notice that old.conf.bak is skipped because the pattern ends in .conf, and sites-available/blog is not loaded because only sites-enabled is included. That is exactly why enabling a site means linking it into the right place.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. On a Debian-based server, how do you take a site from sites-available live in Nginx?

    The main config includes sites-enabled, so a symlink there makes the site part of the loaded configuration. A reload then applies it.
  2. 2. Why use nginx -t or apachectl configtest before reloading?

    The test parses the files and reports the first error with its file and line. The live server keeps running with its old configuration while you fix it.
  3. 3. What is the Apache package called on Rocky Linux and RHEL?

    Red Hat-family systems package Apache as httpd (service httpd). Debian and Ubuntu call it apache2.
  4. 4. Why prefer reload over restart on a live site?

    A graceful reload starts new workers with the new settings and lets old workers finish in-flight requests, so connections are not dropped.

Summary

  • Install with apt install nginx or apache2 on Debian and Ubuntu, and dnf install nginx or httpd on the Red Hat family.
  • Debian-style layouts use sites-available plus symlinks in sites-enabled; Red Hat-style layouts include every *.conf in conf.d.
  • The server reads only what the main config includes: a file in the wrong place is ignored.
  • Logs live in /var/log/nginx, /var/log/apache2 or /var/log/httpd; the workers run as www-data, nginx or apache.
  • Always test with nginx -t or apachectl configtest, then systemctl reload.
  • The official Docker images are nginx and httpd, with their configuration under /etc/nginx and /usr/local/apache2.

Next: Nginx vs Apache: Architecture and Choosing