Cloudbooklet
  • News
  • Artificial Intelligence
  • Applications
  • Linux
No Result
View All Result
Cloudbooklet
  • News
  • Artificial Intelligence
  • Applications
  • Linux
No Result
View All Result
Cloudbooklet
No Result
View All Result
Home Google Cloud

How to Set Up Nginx Server Block – Ubuntu 18.04

by Cloudbooklet
5 years ago
in Google Cloud, Compute Engine
How To Set Up Nginx Server Block - Ubuntu 18.04
ShareTweetSendShare
Readers like you help support Cloudbooklet. When you make a purchase using links on our site, we may earn an affiliate commission.

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. […]

ADVERTISEMENT

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:

ADVERTISEMENT

Ubuntu 18.04 LTS

CentOS 7

Debian 9

ADVERTISEMENT

Prerequisites

  1. Your Compute Engine Instance running.
  2. For setting up Compute Engine, see the Setting up Compute Engine Instance.
  3. For installing Nginx and PHP, see how to install LEMP in Compute Engine Instance.
  4. Domain name is pointed to your virtual machine.
  5. For setting up Cloud DNS, see the Setting up Google Cloud DNS for your domain.
  6. 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.

You might also like

How To Setup Ssh Keys On Ubuntu

How to Setup SSH Keys on Ubuntu 20.04

4 months ago
Draggan Ai Editing Tool Install And Use Draggan Photo Editor

DragGAN AI Editing Tool Install and Use DragGAN Photo Editor

4 months ago

Now you shall remove the default configuration and setup a new directory as follows.

ADVERTISEMENT
sudo rm /etc/nginx/sites-available/default
sudo rm /etc/nginx/sites-enabled/default

You can make the new root directory setup as follows.

Replace yourdomainname.com with your original domain name.

ADVERTISEMENT
home
-- yourdomainname.com
---- logs
---- public

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.

ADVERTISEMENT
mkdir -p yourdomainname.com/logs yourdomainname.com/public

2. Set up Correct Permissions

Once the directories are created you can setup correct permissions.

mkdir -p yourdomainname.com/logs yourdomainname.com/public
sudo chmod -R 755 yourdomainname.com

3. Create Server Block

Now create a new Nginx configuration for your website in the sites-available directory.

sudo nano /etc/nginx/sites-available/yourdomainname.com

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.

server {
listen 80;
listen [::]:80;
server_name yourdomainname.com www.yourdomainname.com;

error_log /home/username/yourdomainname.com/logs/error.log;

root /home/username/yourdomainname.com/public/;
index index.html index.htm;

location / {
try_files $uri $uri/ =404;
}
}

If you have installed PHP 7.3 you can use this configuration.

server {
listen 80;
listen [::]:80;
server_name yourdomainname.com www.yourdomainname.com;

error_log /home/username/yourdomainname.com/logs/error.log;

root /home/username/yourdomainname.com/public/;
index index.html index.php;

location / {
try_files $uri $uri/ /index.php?$args;
}

location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/run/php/php7.3-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}

To enable this newly created website configuration, symlink the file that you just created into the sites-enabled directory.

sudo ln -s /etc/nginx/sites-available/yourdomainname.com /etc/nginx/sites-enabled/yourdomainname.com

Check your configuration and restart Nginx for the changes to take effect.

sudo nginx -t
sudo service nginx restart

4. Create Sample Page

Now you can create a new sample page and test the configuration.

sudo nano /home/username/yourdomainname.com/public/index.html

Paste the below piece of code and hit Ctrl+X followed by Y and Enter to save the file.

<html>
    <head>
        <title>Welcome to Nginx Test!</title>
    </head>
    <body>
        <h1>Success!  Nginx server block is working!</h1>
    </body>
</html>

Now visit your domain name in your browser.

http://yourdomainname.com

You should the see the following message.

Success!  Nginx server block is working!

Finally delete the sample page with the below command.

sudo rm -f /home/username/yourdomainname.com/public/index.html

Great! Now you have configured Nginx server blocks successfully on Ubuntu 18.04. You can also create additional sites using the above method.

Tags: Compute EngineUbuntu 18.04
Share1Tweet1SendShare
Cloudbooklet

Cloudbooklet

Comments 6

  1. Avatar Of Victor Dorado Victor Dorado says:
    2 years ago

    Very useful thank you very much. Will you have a tutorial to create ftp accounts for each domain using nginx blocks?

    Reply
  2. Avatar Of Stephen Stephen says:
    4 years ago

    Got this error trying to make server blocks work

    nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
    nginx: [emerg] open() “/home/deletedusername/deleteddomain.com/logs/error.log” failed (2: No such file or directory)
    nginx: configuration file /etc/nginx/nginx.conf test failed

    when i do
    ls -l /var/log/nginx/
    total 8
    -rw-r—– 1 www-data adm 2242 Nov 12 06:03 access.log
    -rw-r—– 1 www-data adm 262 Nov 12 06:41 error.log

    Reply
  3. Avatar Of Buddhaflow buddhaflow says:
    4 years ago

    PHP did not work for me until I also included the following line in the PHP FPM server block:

    fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;

    I put it under ‘ include fastcgi_params;’

    Made it wasn’t able to ‘include fastcgi_params;’ for some reason?

    PHP 7.3 Ubuntu 18.04.3

    Reply
    • Avatar Of Cloudbooklet Cloudbooklet says:
      4 years ago

      Please try adding the line in /etc/nginx/fastcgi_params

      Reply
  4. Avatar Of Sanjay Sanjay says:
    4 years ago

    Wonderful tutorial.

    Any specific reason to choose ‘/home’ directory for new server blocks?
    Why not ‘/var/www/’ ?

    Reply
    • Avatar Of Cloudbooklet Cloudbooklet says:
      4 years ago

      In case if you wish to make a setup for multiple websites in same server with chroot jail setup, this kind of setup will be useful by running PHP-FPM with the respective username

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Related Posts

Set Up Deep Learning With Nvidia, Cuda, Cudnn On Ubuntu

How to Set Up Deep Learning with Nvidia, CUDA, cuDNN on Ubuntu 22.04

7 months ago
How To Install Or Upgrade Php 8.2 On Ubuntu 22.04

How to Install or Upgrade PHP 8.2 on Ubuntu 22.04

9 months ago
How To Change Timezone On Ubuntu 22.04

How to Change Timezone on Ubuntu 22.04

1 year ago
How To Install Ansible On Ubuntu 22.04

How to Install Ansible on Ubuntu 22.04

1 year ago

Follow Us

Trending Articles

Ai Comic Generator

Best 10 AI Comic Generator: Create Comic book in Seconds

September 18, 2023

AI Statistics and Trends: What You Need to Know in 2023

5 Free AI Soulmate Maker: Create Your Perfect Match

How to Delete Netflix Account Permanently

Microsoft Editor vs Grammarly: Which is the Best Grammar Checker?

Top 10 Advantages of a Cloud VPS Server

Popular Articles

Best Laptop For Minecraft-

5 Best Laptop for Minecraft in 2023: Top Picks for All Budgets

September 21, 2023

Free AI Script Generators Online to Create High Quality Scripts

10 Best AI Drawing Generators to Create Stunning Art

10 Best AI Mockup Generators to Create Stunning Mockups

Microsoft Designer: AI Design Tool Now Available in Edge

How to Use Hulu Bug Tracker to Improve Your Streaming Experience

Subscribe Now

loader

Subscribe to our mailing list to receives daily updates!

Email Address*

Name

Cloudbooklet Logo

Welcome to our technology blog, where we explore the latest advancements in the field of artificial intelligence (AI) and how they are revolutionizing cloud computing. In this blog, we dive into the powerful capabilities of cloud platforms like Google Cloud Platform (GCP), Amazon Web Services (AWS), and Microsoft Azure, and how they are accelerating the adoption and deployment of AI solutions across various industries. Join us on this exciting journey as we explore the endless possibilities of AI and cloud computing.

  • About
  • Contact
  • Disclaimer
  • Privacy Policy

Cloudbooklet © 2023 All rights reserved.

No Result
View All Result
  • News
  • Artificial Intelligence
  • Applications
  • Linux

Cloudbooklet © 2023 All rights reserved.