In this article, we will discuss how to serve HTML and PHP files from multiple folders on an Nginx host. This is useful for organizing your website's files and making them more manageable. We will cover the following topics:
- Setting up the Nginx configuration file
- Creating a new server block
- Setting up the root directory
- Enabling PHP processing
- Testing the configuration
Setting up the Nginx configuration file
The first step is to open the Nginx configuration file. This file is usually located at /etc/nginx/nginx.conf but it can vary depending on your system. Once you have opened the file, you will need to find the http block. This block contains the configuration for the Nginx web server. Inside this block, you will need to add a new server block.
Creating a new server block
A server block is used to define a virtual host. This allows you to serve multiple websites from a single Nginx instance. To create a new server block, you will need to add the following code to the http block:
server {
listen 80;
server_name example.com;
root /var/www/example.com;
index index.html index.php;
}
In this example, we are creating a new server block for the website example.com. The listen directive tells Nginx to listen on port 80 for incoming requests. The server_name directive specifies the domain name of the website. The root directive sets the root directory for the website's files. The index directive specifies the default files that Nginx should look for when trying to serve a request.
Setting up the root directory
The root directory is where you will store the HTML and PHP files for your website. In this example, we have set the root directory to /var/www/example.com. You can change this to any directory that you want. Just make sure that the Nginx user has permission to access the directory. You can do this by running the following command:
sudo chown -R www-data:www-data /var/www/example.com
This command changes the owner of the directory to the Nginx user (www-data). The -R option makes the command recursive, so it will also change the owner of all the files in the directory.
Enabling PHP processing
In order to serve PHP files, you will need to enable PHP processing in the Nginx configuration. This can be done by adding the following code to the server block:
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
}
This code tells Nginx to use the FastCGI process manager (php-fpm) to process PHP files. The location block is used to match requests for PHP files. The include directive tells Nginx to include the fastcgi-php.conf file, which contains the configuration for PHP processing. The fastcgi_pass directive specifies the socket that php-fpm is listening on.
Testing the configuration
Once you have made all the changes, you will need to test the configuration to make sure that there are no errors. This can be done by running the following command:
sudo nginx -t
If the configuration is correct, you should see a message saying that the test is successful. If there are any errors, you will need to fix them before continuing. Once the configuration has been tested, you can restart the Nginx service to apply the changes.
Serving files from multiple folders
Now that you have set up the Nginx configuration, you can start serving files from multiple folders. To do this, you will need to create new directories inside the root directory. For example, you could create a directory called images to store all the images for your website. You can then access these images by using the following URL:
http://example.com/images/image.jpg
This will tell Nginx to look for the image.jpg file inside the images directory. You can create as many directories as you want, and Nginx will automatically serve the files from them.
In this article, we have discussed how to serve HTML and PHP files from multiple folders on an Nginx host. We have covered the following topics:
- Setting up the Nginx configuration file
- Creating a new server block
- Setting up the root directory
- Enabling PHP processing
- Testing the configuration
- Serving files from multiple folders
By following the steps in this article, you should be able to serve HTML and PHP files from multiple folders on your Nginx host. This will make it easier to organize your website's files and make them more manageable.
References
| Title | URL |
|---|---|
| Nginx Documentation | https://nginx.org/en/docs/ |
| PHP-FPM Documentation | https://www.php.net/manual/en/install.fpm.php |