Linux 115: Setting Up a Web Server with Apache or Nginx

Linux 115: Setting Up a Web Server with Apache or Nginx

Hosting a website or web application on a Linux server requires a reliable and performant web server. In this article, you'll learn how to install, configure, and manage two of the most popular web servers on Linux: Apache (httpd) and Nginx.


1. Introduction to Web Servers

A web server is software that handles HTTP(S) requests and serves web content (HTML, CSS, JS, etc.) to users' browsers.

  • Apache: A mature, flexible server with powerful modules and widespread usage.
  • Nginx: Lightweight, high-performance server known for its speed and ability to handle many concurrent connections.


2. Installing Apache

On Debian/Ubuntu

sudo apt update sudo apt install apache2


On RHEL/CentOS/Fedora


sudo dnf install httpd


Start and Enable Apache


sudo systemctl start apache2 # Debian/Ubuntu sudo systemctl enable apache2 sudo systemctl start httpd # RHEL/CentOS sudo systemctl enable httpd


Verify Installation

Visit http://your_server_ip in a browser. You should see the default Apache welcome page.


3. Installing Nginx

On Debian/Ubuntu


sudo apt update sudo apt install nginx

On RHEL/CentOS/Fedora


sudo dnf install nginx

Start and Enable Nginx


sudo systemctl start nginx sudo systemctl enable nginx

Verify Installation

Visit http://your_server_ip — you should see the Nginx welcome page.


4. Basic Web Server Configuration

Apache Configuration Files

  • Main config: /etc/apache2/apache2.conf or /etc/httpd/conf/httpd.conf

  • Virtual Hosts: /etc/apache2/sites-available/ (Debian) or /etc/httpd/conf.d/ (RHEL)

Example Virtual Host (Debian-based):


sudo nano /etc/apache2/sites-available/example.com.conf

<VirtualHost *:80> ServerName example.com DocumentRoot /var/www/example.com ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost>

Enable and reload:


sudo a2ensite example.com sudo systemctl reload apache2

Nginx Configuration Files

  • Main config: /etc/nginx/nginx.conf
  • Server blocks (similar to virtual hosts): /etc/nginx/sites-available/ (Debian) or /etc/nginx/conf.d/ (RHEL)

Example server block:


sudo nano /etc/nginx/sites-available/example.com

server { listen 80; server_name example.com; root /var/www/example.com; index index.html; access_log /var/log/nginx/example.access.log; error_log /var/log/nginx/example.error.log; }

Enable and reload:


sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/ sudo nginx -t sudo systemctl reload nginx

5. Serving Content

Default Web Root

  • Apache: /var/www/html

  • Nginx: /usr/share/nginx/html or /var/www/html

Place your website files (e.g., index.html) in the appropriate directory.


6. Firewall Configuration

Allow web traffic:


sudo ufw allow 'Apache' sudo ufw allow 'Nginx HTTP'

Or open port 80 manually:


sudo ufw allow 80/tcp

7. Enable HTTPS (SSL/TLS)

Use Certbot to enable HTTPS with Let’s Encrypt.

Install Certbot


sudo apt install certbot python3-certbot-apache # Apache sudo apt install certbot python3-certbot-nginx # Nginx

Obtain and Install a Certificate


sudo certbot --apache # For Apache sudo certbot --nginx # For Nginx

Set up auto-renewal:


sudo systemctl status certbot.timer

8. Apache vs. Nginx: When to Use Which

FeatureApacheNginx
PerformanceGood, less with many usersExcellent concurrency
Configuration Style.htaccess + conf filesClean and concise conf files
Dynamic ContentBuilt-in PHP handlingUses PHP-FPM
Reverse ProxyPossibleVery strong


Use Apache if:

  • You rely on .htaccess
  • Compatibility with legacy software matters

Use Nginx if:

  • You want speed and scalability
  • You're building a modern web app


9. Conclusion

Apache and Nginx are both excellent web servers, each with its strengths. Choosing the right one depends on your project needs. With basic setup and configuration, you can now serve websites, host apps, or set up reverse proxies efficiently on your Linux server.


Next Steps:

  • Learn to configure Apache with PHP and MySQL (LAMP stack)
  • Set up Nginx as a reverse proxy for Node.js or Python apps
  • Use load balancing and caching with Nginx for performance


Up next: Linux 116: Configuring SSH for Secure Remote Access

Post a Comment (0)
Previous Post Next Post

ads