How to Set Up Nginx Server Block – Ubuntu 18.04 (ok)
How to Set Up Nginx Server Block – Ubuntu 18.04
In Nginx web server, server blocks (similar to the virtual hosts in Apache) can be used to configure and host more than one domain on a single server on Google Cloud
In this guide you are going to learn how to set up server blocks in Nginx on Ubuntu 18.04 server on Google Compute Engine.
Not using Ubuntu 18.04? Choose a different OS:
Ubuntu 18.04 LTS
CentOS 7
Prerequisites
Your Compute Engine Instance running.
For setting up Compute Engine, see the Setting up Compute Engine Instance.
For installing Nginx and PHP, see how to install LEMP in Compute Engine Instance.
Domain name is pointed to your virtual machine.
For setting up Cloud DNS, see the Setting up Google Cloud DNS for your domain.
Google Cloud SQL Setup, see Setup Cloud SQL and connect with Compute Engine.
Setup Server Blocks
1. Set Up New Root Directory
By default, Nginx provides a default server block configuration. It is configured to serve documents out of a directory at /var/www/html
.
Now you shall remove the default configuration and setup a new directory as follows.
You can make the new root directory setup as follows.
Replace yourdomainname.com
with your original domain name.
The public
directory is your website’s root directory and logs
directory for your error logs.
Now we create these directories with the following command.
2. Set up Correct Permissions
Once the directories are created you can setup correct permissions.
3. Create Server Block
Now create a new Nginx configuration for your website in the sites-available
directory.
Copy and paste the following configuration, ensure that you change the server_name, error_log and root directives to match your domain name. Hit CTRL+X
followed by Y
to save the changes.
If you have installed PHP 7.3
you can use this configuration.
To enable this newly created website configuration, symlink the file that you just created into the sites-enabled
directory.
Check your configuration and restart Nginx for the changes to take effect.
4. Create Sample Page
Now you can create a new sample page and test the configuration.
Paste the below piece of code and hit Ctrl+X
followed by Y
and Enter
to save the file.
Now visit your domain name in your browser.
You should the see the following message.
Finally delete the sample page with the below command.
Great! Now you have configured Nginx server blocks successfully on Ubuntu 18.04. You can also create additional sites using the above method.
Last updated