How To Set Up Website Hosting On Ubuntu

Sites are also known as server blocks or virtual hosts.
You must set up the following first before following this instruction to set up your sites.
  • Domain name:
  • Hostname:
  • Username: alex
  1. Configure DNS for the site.
  • Buy a domain if you haven’t got one. Let’s say your domain is “”.
  • Update your domain’s DNS settings.
  • If your hosting provider is DigitalOcean, go to your DigitalOcean account page → Networking → Domains → Select your domain.
  • Set up as in the picture below.


Note: DNS changes can take up to 72 hours to propagate. If you visit the domain shortly after making any changes, you will probably see an error or some unexpected results.
  1. SSH into your server
  1. Create directories for the site.
        We will use the following directory structure pattern

 └── sites 
   └── domain1 
     ├── logs
     └── public
   └── domain2
     ├── logs
     └── public
   └── domainN
     ├── logs
     └── public
For a domain called and an admin user called alex, the directories will be
└── sites
        ├── logs
        └── public
cd ~
mkdir -p sites/ sites/
chmod -R 755 sites/
  1. Configure Nginx server block for the site.
  • Go to the sites-available directory.
cd /etc/nginx/sites-available
  • Create a new file to hold the configuration.
sudo nano
Add the following content to the file.
Change to your domain name.
Change alex to your username.
server {
  listen 80;
  listen [::]:80;

  access_log /home/alex/sites/;
  error_log /home/alex/sites/;

  root /home/alex/sites/;
  index 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.1-fpm.sock;
     fastcgi_index index.php;
     include fastcgi_params;
  • To enable the site, symlink the file that you just created into the enabled-sites directory
sudo ln -s /etc/nginx/sites-available/ /etc/nginx/sites-enabled/
  • Ensure that the configuration has no errors.
sudo nginx -t
  • If everything is ok, restart Nginx.
sudo service nginx restart
  1. Add a DB for the site.

When hosting multiple sites on a single server, it’s good practice to create a separate user and database for each individual site. You should also lock down the user privileges so that the user only has access to the databases that they require.
  • Log into MariaDB/MySQL with the root user.  
mysql -u root -p
Screen Shot 2017-03-21 at 5.28.16 PM.png
  • Once logged in, create a new database for the site. Let’s call it yourdomain_com for the domain
CREATE DATABASE yourdomain_com CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_520_ci;
  • Create the new user. Remember to substitute the username and password for your own values.
CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';
  • Grant all privileges but restrict them to the yourdomain_com database only. Remember to substitute the username for your own value.
GRANT ALL PRIVILEGES ON yourdomain_com.* TO 'username'@'localhost';
  • Flush the MySQL privileges table.
  • Exit MariaDB/MySQL.

  1. Install the website's code.

You may put any php or html page in the site's public directory and you'll be able to see its content in a browser. Alternatively, you can install a site engine such as WordPress. See How To Set Up A WordPress Site On Ubuntu.
Below is a simple example.
  • Go to the site’s public directory.
cd ~/sites/yourdomain_com/public
  • Put any php or html page in the public directory and you'll see it in a browser. Below is an example.
sudo nano info.php
Add the following content to the file.
  • Visit the page in a browser. Go to You'll see something like this.

  1. Done.

Popular posts from this blog

How To Set Up Quasar v0.14 On A Laravel+Vue Framework

How To Set Up Quasar v0.13 On A Laravel(5.4)+Vue Framework

How To Fix Random Disappearing Of HTML Elements On iPhone/iPad