{"id":14156,"date":"2024-06-13T15:28:09","date_gmt":"2024-06-13T12:28:09","guid":{"rendered":"http:\/\/local.brightwhiz\/?p=14156"},"modified":"2024-06-13T15:28:10","modified_gmt":"2024-06-13T12:28:10","slug":"how-to-run-multiple-websites-using-nginx-webserver-on-rocky-linux-8-10-systems","status":"publish","type":"post","link":"http:\/\/local.brightwhiz\/how-to-run-multiple-websites-using-nginx-webserver-on-rocky-linux-8-10-systems\/","title":{"rendered":"How to Run Multiple Websites Using Nginx Webserver on Rocky Linux 8.10 Systems"},"content":{"rendered":"\n
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.<\/p>\n\n\n\n
Before you start, ensure you have:<\/p>\n\n\n\n
First, update your system to ensure all existing packages are up to date.<\/p>\n\n\n\n
$ sudo dnf update -y<\/code><\/pre>\n\n\n\nStep 2: Install Nginx<\/h3>\n\n\n\n
Install Nginx using the dnf<\/code> package manager.<\/p>\n\n\n\n$ sudo dnf install nginx -y<\/code><\/pre>\n\n\n\nStart and enable Nginx to run on boot.<\/p>\n\n\n\n
$ sudo systemctl start nginx\n$ sudo systemctl enable nginx<\/code><\/pre>\n\n\n\nStep 3: Configure Firewall<\/h3>\n\n\n\n
If your system has a firewall enabled, allow HTTP and HTTPS traffic to access the web server.<\/p>\n\n\n\n
$ sudo firewall-cmd --permanent --add-service=http\n$ sudo firewall-cmd --permanent --add-service=https\n$ sudo firewall-cmd --reload<\/code><\/pre>\n\n\n\nStep 4: Set Up Directory Structure<\/h3>\n\n\n\n
Create a directory structure for your websites. For example, create directories for site1<\/code> and site2<\/code>.<\/p>\n\n\n\n$ sudo mkdir -p \/var\/www\/site1.com\/html\n$ sudo mkdir -p \/var\/www\/site2.com\/html<\/code><\/pre>\n\n\n\nSet the appropriate permissions.<\/p>\n\n\n\n
$ sudo chown -R $USER:$USER \/var\/www\/site1.com\/html\n$ sudo chown -R $USER:$USER \/var\/www\/site2.com\/html\n$ sudo chmod -R 755 \/var\/www<\/code><\/pre>\n\n\n\nStep 5: Create Sample Web Pages<\/h3>\n\n\n\n
Create an index.html<\/code> file for each site to serve as a placeholder.<\/p>\n\n\n\n$ 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\n$ 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<\/code><\/pre>\n\n\n\nStep 6: Configure Nginx Server Blocks<\/h3>\n\n\n\n
Nginx uses server blocks to manage multiple websites. Create configuration files for each site.<\/p>\n\n\n\n
Site1 Configuration<\/h4>\n\n\n\n
Create a configuration file for site1<\/code>.<\/p>\n\n\n\n$ sudo nano \/etc\/nginx\/conf.d\/site1.com.conf<\/code><\/pre>\n\n\n\nAdd the following configuration:<\/p>\n\n\n\n
server {\n listen 80;\n server_name site1.com www.site1.com;\n\n root \/var\/www\/site1.com\/html;\n index index.html;\n\n location \/ {\n try_files $uri $uri\/ =404;\n }\n\n error_page 404 \/404.html;\n location = \/404.html {\n internal;\n }\n\n error_page 500 502 503 504 \/50x.html;\n location = \/50x.html {\n internal;\n }\n}<\/code><\/pre>\n\n\n\nSite2 Configuration<\/h4>\n\n\n\n
Create a configuration file for site2<\/code>.<\/p>\n\n\n\n$ sudo nano \/etc\/nginx\/conf.d\/site2.com.conf<\/code><\/pre>\n\n\n\nAdd the following configuration:<\/p>\n\n\n\n
server {\n listen 80;\n server_name site2.com www.site2.com;\n\n root \/var\/www\/site2.com\/html;\n index index.html;\n\n location \/ {\n try_files $uri $uri\/ =404;\n }\n\n error_page 404 \/404.html;\n location = \/404.html {\n internal;\n }\n\n error_page 500 502 503 504 \/50x.html;\n location = \/50x.html {\n internal;\n }\n}<\/code><\/pre>\n\n\n\nStep 7: Test Nginx Configuration<\/h3>\n\n\n\n
Test the Nginx configuration for any syntax errors.<\/p>\n\n\n\n
$ sudo nginx -t<\/code><\/pre>\n\n\n\nIf the test is successful, restart Nginx to apply the changes.<\/p>\n\n\n\n
$ sudo systemctl restart nginx<\/code><\/pre>\n\n\n\nStep 8: Configure DNS Records<\/h3>\n\n\n\n
Ensure that your domain names (site1.com<\/code> and site2.com<\/code>) are pointing to your server’s IP address. You can do this by configuring the A records in your domain registrar’s DNS settings.<\/p>\n\n\n\nStep 9: Verify the Setup<\/h3>\n\n\n\n
Open a web browser and navigate to http:\/\/site1.com<\/code> and http:\/\/site2.com<\/code>. You should see the respective placeholder pages for each site.<\/p>\n\n\n\nStep 10: Secure Your Sites with SSL (Optional)<\/h3>\n\n\n\n
To secure your websites with HTTPS, you can use Let’s Encrypt to obtain free SSL certificates.<\/p>\n\n\n\n
Install Certbot<\/h4>\n\n\n\n$ sudo dnf install certbot python3-certbot-nginx -y<\/code><\/pre>\n\n\n\nObtain SSL Certificates<\/h4>\n\n\n\n
Run Certbot to obtain and install SSL certificates.<\/p>\n\n\n\n
$ sudo certbot --nginx<\/code><\/pre>\n\n\n\nFollow the prompts to configure SSL for your domains. Certbot will automatically modify your Nginx configuration to use the SSL certificates.<\/p>\n\n\n\n
Step 11: Set Up Automatic SSL Renewal<\/h3>\n\n\n\n
Let’s Encrypt certificates are valid for 90 days. Set up a cron job to automatically renew the certificates.<\/p>\n\n\n\n
Open the crontab for editing.<\/p>\n\n\n\n
$ sudo crontab -e<\/code><\/pre>\n\n\n\nAdd the following line to check for renewal twice a day.<\/p>\n\n\n\n
0 *\/12 * * * \/usr\/bin\/certbot renew --quiet<\/code><\/pre>\n\n\n\nConclusion<\/h3>\n\n\n\n
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.<\/p>\n\n\n\n
Happy hosting!<\/p>\n\n\n\n
References<\/h3>\n\n\n\n\n- Nginx Official Website<\/a><\/li>\n\n\n\n
- Rocky Linux Official Website<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"
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…<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,23,24,25,27,28,16,18],"tags":[193,354,424,433,449,472,1268,531,572,573,591],"yoast_head":"\n
Run Multiple Websites Using Nginx Webserver on Rocky Linux 8.10<\/title>\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\n\t\n\t\n