Reading Time: 3 mins
Nginx can be used to host multiple websites on a single server which reduces the extensive process of creating a new server for the site. For a demonstration purpose, here the domain, sample.com will be used and you can replace it with a desirable domain name. Nginx on Ubuntu 18.04 holds an enabled server block by default which is generally configured to serve the documents out of directory at /var/www/html.
In the case of serving multiple sites, an additional directory needs to be created. The new directory structure has to be created within /var/www directory and within each of these directories, a public-html will be created to store the domain files. To know about Nginx installation steps, visit our blog on Installing Nginx on Ubuntu 18.04
Step 1: Creating new directories
To create a new directory for sample.com, use flag -p as it falls under the parent directories.
sudo mkdir -p /var/www/sample.com/public_html
(You can replace the public_html to html as per your convenience)
To assign the ownership of the directory to the current signed-in account using the $USER environment variable: (this helps to edit and manage the content right from the directory)
sudo chown -R $USER:$USER /var/www/sample.com/public_html
To assign the permissions of the web root directory use the command:
sudo chmod -R 755 /var/www
Thus, a new directory has been created in which the website content will be stored.
Note: When you go Live, assigning permission as root user won’t be safe. Thus, you need to create a new user and need to assign that particular (new) user to your web root directory.
Step 2: Adding the website content for our site
To add the website content on the web root directory, use the command:
cd /var/www/sample.com/public_html/
Note:
- We need to include either the index.php file or Magento zip file into the above-mentioned directory.
- You can also download it by using the Magento website link, https://magento.com/tech-resources/download
After completion, save and close the file. The sample pages for the corresponding directory has been created.
Step 3: Creating server block files
Even though the content is ready, it needs a server block to serve the content. Creating a new directory by leaving the default file to be used as a template for basic configurations:
sudo nano /etc/nginx/sites-available/sample.com
Copy the below configuration file to set up your server block and make sure to add your respective PHP version.
upstream fastcgi_backend { server unix:/var/run/php/php7.2-fpm.sock; } server { listen 80; listen [::]:80; set $MAGE_ROOT /var/www/sample.com/public_html; set $MAGE_MODE default; index index.php index.html index.htm index.nginx-debian.html; server_name sample.com www.sample.com; include /var/www/sample.com/public_html/nginx.conf.sample; location ~ \.php$ { try_files $uri =404; fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_pass unix:/var/run/php/php7.2-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }
Note: It is important for you to set the index value as index.php since Magento is a PHP application.
After extracting the Magento file in your web root directory, you need to include the Nginx configuration file as in the command: /var/www/sample.com/public_html/nginx.conf.sample;
The server block is now ready to serve the provided Magento site content.
Note: If you have created the PHP-FPM configuration file in the /etc/php/7.2/fpm/pool.d directory, you need to include that file in your Nginx server block like:
upstream fastcgi_backend { server unix:/var/run/php/php7.2-fpm-example.com.conf.sock; } server { listen 80; listen [::]:80; set $MAGE_ROOT /var/www/sample.com/public_html; set $MAGE_MODE default; index index.php index.html index.htm index.nginx-debian.html; server_name sample.com www.sample.com; include /var/www/sample.com/public_html/nginx.conf.sample; location ~ \.php$ { try_files $uri =404; fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_pass unix:/var/run/php/php7.2-fpm-example.com.conf.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }
Now, you can restart the Nginx service by using one of the following commands:
sudo nginx -s reload
sudo service nginx restart
sudo systemctl restart nginx
Note: If you want to know more about the PHP-FPM configuration, visit our blog post, How to Optimize PHP-FPM?
Step 4: Enabling the server block
To enable the server block, the symbolic links between these files and sites-enabled directory must be created, which Nginx keeps reading from the start.
To create the symbolic links, use the command:
sudo ln -s /etc/nginx/sites-available/sample.com /etc/nginx/sites-enabled/
The server blocks have been enabled and configured to respond to the requests as the sample.com will now respond to the requests for sample.com and www.sample.com whereas, the default directory will respond requests which don’t match any other blocks.
To adjust the single value so as to avoid the hash bucket memory issues: (which occurs due to the inclusion of additional server names)
Open the file,
sudo nano /etc/nginx/nginx.conf
Remove the # symbol by finding the server_names_hash_bucket_size to uncomment the line
http { . . . server_names_hash_bucket_size 64; . . . }
After completion, save and close the file.
To test the absence of the syntax errors, type:
sudo nginx -t
After completion, save and close the file.
Now, restart the Nginx server to enable the carried changes.
sudo systemctl restart nginx
Make sure to add your localhost url to etc hosts.
Finally, to test whether the Nginx is serving the domain name, navigate to http://sample.com
To know the how-to of Magento installation process in detail, visit our below blog post on, All about Magento Installation Process
Step 5: Managing Nginx (Optional)
You can manage the installed Nginx using the simple commands as similar to the prior ones.
To start the Nginx service, use the command:
sudo service nginx start
To stop the Nginx service, use the command:
sudo service nginx stop
To reload the Nginx service, use the command:
sudo service nginx reload
To enable the Nginx service, use the command:
sudo service nginx enable
To disable the Nginx service, use the command:
sudo service nginx disable
Now that the site has been successfully configured server blocks, you can create any number of server blocks using a similar procedure for the domains you desire to host. These are the procedures to install a basic Nginx server on Ubuntu 18.04 and it is to be noted that the above-prescribed commands will vary from one operating system to another.