Setting up Nginx server blocks, also known as virtual hosts, allows you to host multiple websites on a single server. Each server block can have its own configuration, including a separate domain or subdomain. Here's a step-by-step guide to set up Nginx server blocks on Ubuntu 22.04:
Step 1 : Install Nginx:
Install Nginx using the following command:
Step 2 : Create Directory Structure:
Start by creating a directory structure to store your website files. You can create a directory for each website you plan to host. For example:
Step 3 : Set Permissions:
Ensure that the web directory has the correct permissions:
Step 4 : Create Sample HTML Files:
Create sample HTML files for each website to test:
Step 5 : Create Nginx Server Block Configuration:
Create a new Nginx server block configuration file for each website in the /etc/nginx/sites-available/
directory. For example:
Add the following configuration:
Repeat the process for the second website:
Add a similar configuration with the appropriate domain and file paths.
Step 6 : Create Symbolic Links:
Create symbolic links to the configuration files in the sites-enabled
directory:
Step 7 : Test Nginx Configuration:
Before restarting Nginx, it's a good idea to test the configuration to make sure there are no syntax errors:
Step 8 : Restart Nginx:
Restart Nginx to apply the changes:
Step 9 : Configure DNS:
Make sure your domain names point to the correct server IP address. You can do this by updating your domain registrar's DNS settings.
Step 10 : Test Your Websites:
Open a web browser and navigate to your websites using their domain names (e.g., http://example.devtutorial.io and http://another-example.devtutorial.io). You should see the respective HTML content you created.
By following these steps, you've set up Nginx server blocks to host multiple websites on your Ubuntu 22.04 server.