Nginx is a powerful web server that is well-suited for running multiple websites on a single server due to its high performance and low resource usage. This guide will walk you through the steps to configure Nginx to host multiple websites on a Rocky Linux 8.10 system.
Prerequisites
Before you start, ensure you have:
- A running Rocky Linux 8.10 system
- A user account with sudo privileges
- Internet connectivity to download packages
- Domain names for your websites pointing to your server’s IP address
Step 1: Update Your System
First, update your system to ensure all existing packages are up to date.
$ sudo dnf update -y
Step 2: Install Nginx
Install Nginx using the dnf
package manager.
$ sudo dnf install nginx -y
Start and enable Nginx to run on boot.
$ sudo systemctl start nginx
$ sudo systemctl enable nginx
Step 3: Configure Firewall
If your system has a firewall enabled, allow HTTP and HTTPS traffic to access the web server.
$ sudo firewall-cmd --permanent --add-service=http
$ sudo firewall-cmd --permanent --add-service=https
$ sudo firewall-cmd --reload
Step 4: Set Up Directory Structure
Create a directory structure for your websites. For example, create directories for site1
and site2
.
$ sudo mkdir -p /var/www/site1.com/html
$ sudo mkdir -p /var/www/site2.com/html
Set the appropriate permissions.
$ sudo chown -R $USER:$USER /var/www/site1.com/html
$ sudo chown -R $USER:$USER /var/www/site2.com/html
$ sudo chmod -R 755 /var/www
Step 5: Create Sample Web Pages
Create an index.html
file for each site to serve as a placeholder.
$ echo "<html><head><title>Welcome to Site1</title></head><body><h1>Site1 is live!</h1></body></html>" | sudo tee /var/www/site1.com/html/index.html
$ echo "<html><head><title>Welcome to Site2</title></head><body><h1>Site2 is live!</h1></body></html>" | sudo tee /var/www/site2.com/html/index.html
Step 6: Configure Nginx Server Blocks
Nginx uses server blocks to manage multiple websites. Create configuration files for each site.
Site1 Configuration
Create a configuration file for site1
.
$ sudo nano /etc/nginx/conf.d/site1.com.conf
Add the following configuration:
server {
listen 80;
server_name site1.com www.site1.com;
root /var/www/site1.com/html;
index index.html;
location / {
try_files $uri $uri/ =404;
}
error_page 404 /404.html;
location = /404.html {
internal;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
internal;
}
}
Site2 Configuration
Create a configuration file for site2
.
$ sudo nano /etc/nginx/conf.d/site2.com.conf
Add the following configuration:
server {
listen 80;
server_name site2.com www.site2.com;
root /var/www/site2.com/html;
index index.html;
location / {
try_files $uri $uri/ =404;
}
error_page 404 /404.html;
location = /404.html {
internal;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
internal;
}
}
Step 7: Test Nginx Configuration
Test the Nginx configuration for any syntax errors.
$ sudo nginx -t
If the test is successful, restart Nginx to apply the changes.
$ sudo systemctl restart nginx
Step 8: Configure DNS Records
Ensure that your domain names (site1.com
and site2.com
) are pointing to your server’s IP address. You can do this by configuring the A records in your domain registrar’s DNS settings.
Step 9: Verify the Setup
Open a web browser and navigate to http://site1.com
and http://site2.com
. You should see the respective placeholder pages for each site.
Step 10: Secure Your Sites with SSL (Optional)
To secure your websites with HTTPS, you can use Let’s Encrypt to obtain free SSL certificates.
Install Certbot
$ sudo dnf install certbot python3-certbot-nginx -y
Obtain SSL Certificates
Run Certbot to obtain and install SSL certificates.
$ sudo certbot --nginx
Follow the prompts to configure SSL for your domains. Certbot will automatically modify your Nginx configuration to use the SSL certificates.
Step 11: Set Up Automatic SSL Renewal
Let’s Encrypt certificates are valid for 90 days. Set up a cron job to automatically renew the certificates.
Open the crontab for editing.
$ sudo crontab -e
Add the following line to check for renewal twice a day.
0 */12 * * * /usr/bin/certbot renew --quiet
Conclusion
You have successfully configured Nginx to host multiple websites on your Rocky Linux 8.10 system. Your web server is now capable of serving multiple domains efficiently. For more advanced configurations and optimizations, refer to the official Nginx documentation.
Happy hosting!
References
Found this article interesting? Follow Brightwhiz on Facebook, Twitter, and YouTube to read and watch more content we post.