Linux 120: Setting Up a Linux Web Server with Apache and Nginx

Linux 120: Setting Up a Linux Web Server with Apache and Nginx

Web servers are core components of most Linux server setups. In this article, we'll walk through setting up two of the most widely used web servers: Apache (httpd) and Nginx, including how to install, configure, and serve your first website.


1. Introduction to Apache and Nginx

FeatureApacheNginx
ArchitectureProcess/thread-basedEvent-driven, asynchronous
Best for.htaccess, PHP-heavy sitesStatic files, reverse proxy
Config formatModular, httpd.confSimple, block-based

2. Installing Apache

On Ubuntu/Debian:

sudo apt update sudo apt install apache2

On RHEL/CentOS:

sudo yum install httpd sudo systemctl enable httpd sudo systemctl start httpd

Check Web Server:

curl http://localhost

Or open http://<server-ip> in a browser.


3. Installing Nginx

On Ubuntu/Debian:

sudo apt update sudo apt install nginx

On RHEL/CentOS:

sudo yum install nginx sudo systemctl enable nginx sudo systemctl start nginx

4. Configuring Apache

Apache configuration files:

  • /etc/apache2/apache2.conf (Ubuntu)

  • /etc/httpd/httpd.conf (CentOS)

Host a Website

sudo mkdir -p /var/www/mywebsite echo "<h1>Hello from Apache</h1>" | sudo tee /var/www/mywebsite/index.html

Create a virtual host file:

sudo nano /etc/apache2/sites-available/mywebsite.conf

Example content:

<VirtualHost *:80> ServerName mywebsite.local DocumentRoot /var/www/mywebsite </VirtualHost>

Enable the site:


sudo a2ensite mywebsite.conf sudo systemctl reload apache2

5. Configuring Nginx

Default config directory: /etc/nginx/nginx.conf
Site configs (Ubuntu): /etc/nginx/sites-available/

Host a Website

sudo mkdir -p /var/www/mynginxsite echo "<h1>Hello from Nginx</h1>" | sudo tee /var/www/mynginxsite/index.html

Create a server block:


sudo nano /etc/nginx/sites-available/mynginxsite

Example content:

server { listen 80; server_name mynginxsite.local; root /var/www/mynginxsite; index index.html; }

Enable it:


sudo ln -s /etc/nginx/sites-available/mynginxsite /etc/nginx/sites-enabled/ sudo nginx -t sudo systemctl reload nginx

6. Hosting Multiple Sites

Both Apache and Nginx support multiple virtual hosts (Apache) or server blocks (Nginx). You can configure each to listen on different domains or ports.


7. Reverse Proxy with Nginx

Use Nginx in front of an application (e.g., Node.js, Flask):


server { listen 80; server_name app.example.com; location / { proxy_pass http://localhost:3000; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } }

8. SSL with Let's Encrypt

Install Certbot:


sudo apt install certbot python3-certbot-nginx sudo certbot --nginx

For Apache:


sudo apt install certbot python3-certbot-apache sudo certbot --apache

Certificates are auto-renewed via cron.


9. Managing Web Servers

Start/Stop/Restart Apache:


sudo systemctl restart apache2

Start/Stop/Restart Nginx:


sudo systemctl restart nginx

Check status:


sudo systemctl status nginx sudo systemctl status apache2

10. Conclusion

Whether you’re hosting a simple static site or acting as a reverse proxy for backend services, Apache and Nginx provide robust, scalable solutions. Nginx is preferred for performance and load balancing, while Apache excels in flexibility and dynamic content handling.


Next Steps:

  • Set up HTTPS using Let’s Encrypt

  • Configure caching and compression

  • Explore load balancing with Nginx

  • Use Apache or Nginx with PHP and databases for dynamic websites


Coming next: Linux 121: Managing Services and Processes on Linux

Post a Comment (0)
Previous Post Next Post

ads