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
Feature | Apache | Nginx |
---|---|---|
Architecture | Process/thread-based | Event-driven, asynchronous |
Best for | .htaccess, PHP-heavy sites | Static files, reverse proxy |
Config format | Modular, httpd.conf | Simple, block-based |
2. Installing Apache
On Ubuntu/Debian:
On RHEL/CentOS:
Check Web Server:
Or open http://<server-ip>
in a browser.
3. Installing Nginx
On Ubuntu/Debian:
On RHEL/CentOS:
4. Configuring Apache
Apache configuration files:
-
/etc/apache2/apache2.conf
(Ubuntu) -
/etc/httpd/httpd.conf
(CentOS)
Host a Website
Create a virtual host file:
Example content:
Enable the site:
5. Configuring Nginx
Default config directory: /etc/nginx/nginx.conf
Site configs (Ubuntu): /etc/nginx/sites-available/
Host a Website
Create a server block:
Example content:
Enable it:
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):
8. SSL with Let's Encrypt
Install Certbot:
For Apache:
Certificates are auto-renewed via cron.
9. Managing Web Servers
Start/Stop/Restart Apache:
Start/Stop/Restart Nginx:
Check status:
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