Nginx and Apache Setup: Virtual Host Configuration
Install and configure Nginx and Apache web servers, compare their strengths, set up virtual hosts, configure SSL/TLS, and manage web services on Linux.
Table of Contents
Nginx and Apache Setup: Virtual Host Configuration
Nginx and Apache are the two most widely used web servers in the world. Both can serve web applications, static files, and act as reverse proxies on Linux servers. This guide covers installation, basic configuration, and virtual host setup for both.
Nginx vs Apache Comparison
| Feature | Nginx | Apache |
|---|---|---|
| Architecture | Async, event-driven | Process/thread-based |
| Static files | Very fast | Slower |
| Dynamic content | Requires reverse proxy | Built-in modules |
| Memory usage | Low | Higher |
| Configuration | Simpler | .htaccess support |
| Best for | High-traffic sites | PHP applications |
Choose Nginx for high-traffic static content or reverse proxy; choose Apache for PHP-based applications (WordPress, Laravel). They can also be used together: Nginx as a front-end reverse proxy, Apache as the back-end application server.
Installing Nginx
Debian/Ubuntu
# Update package list
sudo apt update
# Install Nginx
sudo apt install nginx -y
# Start and enable the service
sudo systemctl start nginx
sudo systemctl enable nginx
# Check status
sudo systemctl status nginx
RHEL/CentOS/AlmaLinux
# Add EPEL repository
sudo dnf install epel-release -y
# Install Nginx
sudo dnf install nginx -y
# Start and enable the service
sudo systemctl start nginx
sudo systemctl enable nginx
Basic Nginx Commands
# Test configuration
sudo nginx -t
# Reload configuration (no downtime)
sudo systemctl reload nginx
# Restart service
sudo systemctl restart nginx
# Show Nginx version
nginx -v
Nginx Virtual Host Configuration
In Nginx, create a separate configuration file under /etc/nginx/sites-available/ for each site.
Basic Virtual Host
# Create configuration file
sudo nano /etc/nginx/sites-available/example.com
server {
listen 80;
listen [::]:80;
server_name example.com www.example.com;
root /var/www/example.com/html;
index index.html index.htm index.php;
location / {
try_files $uri $uri/ =404;
}
# For PHP-FPM
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
}
# Block access to hidden files
location ~ /\.ht {
deny all;
}
# Log files
access_log /var/log/nginx/example.com.access.log;
error_log /var/log/nginx/example.com.error.log;
}
# Enable the site
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
# Create web directory
sudo mkdir -p /var/www/example.com/html
sudo chown -R www-data:www-data /var/www/example.com
# Test and reload
sudo nginx -t && sudo systemctl reload nginx
Nginx Reverse Proxy Configuration
server {
listen 80;
server_name app.example.com;
location / {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_cache_bypass $http_upgrade;
}
}
Installing Apache
Debian/Ubuntu
# Install Apache
sudo apt update
sudo apt install apache2 -y
# Start and enable the service
sudo systemctl start apache2
sudo systemctl enable apache2
# Check status
sudo systemctl status apache2
RHEL/CentOS/AlmaLinux
# Install Apache (httpd)
sudo dnf install httpd -y
# Start and enable the service
sudo systemctl start httpd
sudo systemctl enable httpd
Basic Apache Commands
# Test configuration
sudo apachectl configtest
# Reload configuration
sudo systemctl reload apache2
# Enable modules
sudo a2enmod rewrite
sudo a2enmod ssl
# Enable/disable sites
sudo a2ensite example.com
sudo a2dissite 000-default
Apache Virtual Host Configuration
# Create configuration file
sudo nano /etc/apache2/sites-available/example.com.conf
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example.com/html
<Directory /var/www/example.com/html>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/example.com-error.log
CustomLog ${APACHE_LOG_DIR}/example.com-access.log combined
</VirtualHost>
# Create web directory
sudo mkdir -p /var/www/example.com/html
sudo chown -R www-data:www-data /var/www/example.com
# Enable the site
sudo a2ensite example.com.conf
# Test and reload
sudo apachectl configtest && sudo systemctl reload apache2
SSL/TLS Configuration (Let's Encrypt)
Use Certbot for a free SSL certificate:
# Install Certbot
sudo apt install certbot -y
# For Nginx
sudo apt install python3-certbot-nginx -y
sudo certbot --nginx -d example.com -d www.example.com
# For Apache
sudo apt install python3-certbot-apache -y
sudo certbot --apache -d example.com -d www.example.com
# Test automatic renewal
sudo certbot renew --dry-run
Manual SSL Configuration (Nginx)
server {
listen 443 ssl http2;
server_name example.com www.example.com;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers HIGH:!aNULL:!MD5;
root /var/www/example.com/html;
index index.html;
}
# Redirect HTTP to HTTPS
server {
listen 80;
server_name example.com www.example.com;
return 301 https://$server_name$request_uri;
}
Firewall Settings
# Allow HTTP and HTTPS with ufw
sudo ufw allow 'Nginx Full'
# or
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
# For Apache
sudo ufw allow 'Apache Full'
On REXE servers, also remember to open HTTP (80) and HTTPS (443) ports from the Path Panel firewall.
Conclusion
Nginx and Apache are powerful web servers for different use cases. Nginx is ideal for high-performance static content and reverse proxy, while Apache is preferred for PHP applications and .htaccess-based configurations. With virtual host configuration on either server, multiple domains can be hosted on the same server.
Related Articles
How to Connect to Your VDS Server via SSH (Complete Guide)
Step-by-step SSH connection guide for Linux VDS: PuTTY, Terminal, key-based authentication, Path Panel firewall rules, and security best practices.
How to Connect to Windows VDS via RDP: Complete Guide
Complete guide to connecting to Windows Server VDS via Remote Desktop (RDP): setup steps, NLA security, Path Panel firewall rule, and troubleshooting tips.
How to Change VDS Server OS: Step-by-Step Reinstall Guide
Step-by-step guide to changing your VDS server OS via REXE panel: backup tips, supported Linux distributions and Windows Server versions explained.