Adding a new website to an existing Nginx setup can seem daunting, but with careful planning and execution, it becomes a manageable task. This guide will walk you through the necessary steps to expand your server’s capabilities by hosting multiple sites.
Table of Contents
- Prerequisites
- Step 1: Set Up the Directory Structure
- Step 2: Configure Nginx
- Step 3: Test and Reload Nginx
- Conclusion
- Further Reading
Prerequisites
Before you begin, ensure that you have:
- A running instance of Nginx. If you need guidance on Nginx installation, you might find the link helpful.
- Access to server terminal and permissions to view and edit configuration files.
- A basic understanding of server and web technologies.
Step 1: Set Up the Directory Structure
The first step is organizing the files and directories where your website’s files will be stored. It’s crucial to maintain a standardized directory structure for easy management.
1 2 3 |
sudo mkdir -p /var/www/yournewsite.com/html sudo chown -R $USER:$USER /var/www/yournewsite.com/html sudo chmod -R 755 /var/www/yournewsite.com |
Replace yournewsite.com
with your actual domain name. Ensure the permissions allow Nginx to serve the files while maintaining system security.
Step 2: Configure Nginx
Next, you need to create a configuration file. This tells Nginx how to treat requests for your new site.
Create a new configuration file in
/etc/nginx/sites-available/
:1
sudo nano /etc/nginx/sites-available/yournewsite.com
Add the following basic server configuration:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
server { listen 80; listen [::]:80; server_name yournewsite.com www.yournewsite.com; root /var/www/yournewsite.com/html; index index.html index.htm; location / { try_files $uri $uri/ =404; } }
Make sure to replace yournewsite.com
with your actual domain name. This configuration is a minimal setup sufficient for a static site, and it may be expanded as required for dynamic content or other technologies.
Create a symbolic link between
sites-available/
andsites-enabled/
to enable the configuration:1
sudo ln -s /etc/nginx/sites-available/yournewsite.com /etc/nginx/sites-enabled/
Use Nginx’s testing utility to ensure that your configuration is correct:
1
sudo nginx -t
Step 3: Test and Reload Nginx
Once the configuration has been added, tested, and found correct, reload Nginx to apply the changes:
1
|
sudo systemctl reload nginx
|
You can now navigate to http://yournewsite.com
in your browser to verify that your site is live. If it doesn’t work as expected, inspect the Nginx error logs found at /var/log/nginx/error.log
for issues.
Conclusion
Congratulations, you have successfully added a new website to your existing Nginx setup! This simple guide introduces the basics, but Nginx offers rich functionality to explore further as your needs grow. For more comprehensive tasks such as deploying in cloud environments, consider exploring Nginx deployment.
Further Reading
- For advanced configurations like redirects from HTTP to HTTPS, check out this guide on Nginx redirects.
By following best practices and organizing your server layout effectively, you’ll maintain a scalable and managed hosting environment with Nginx.