How To Set Up Website Hosting On Ubuntu

Sites are also known as server blocks or virtual hosts.
Prerequisites
You must set up the following first before following this instruction to set up your sites.
Context
  • Domain name: yourdomain.com
  • Hostname: bluedroplet.yourdomain.com
  • Username: alex
Steps
  1. Configure DNS for the site.
  • Buy a domain if you haven’t got one. Let’s say your domain is “yourdomain.com”.
  • 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
ssh alex@bluedroplet.yourdomain.com
  1. Create directories for the site.
        We will use the following directory structure pattern

 └── sites 
   └── domain1 
     ├── logs
     └── public
   └── domain2
     ├── logs
     └── public
   └── domainN
     ├── logs
     └── public
E.g.
For a domain called yourdomain.com and an admin user called alex, the directories will be
/home/alex
└── sites
  └── yourdomain.com
        ├── logs
        └── public
Commands:
cd ~
mkdir -p sites/yourdomain.com/logs sites/yourdomain.com/public
chmod -R 755 sites/yourdomain.com
  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 yourdomain.com
Add the following content to the file.
Change yourdomain.com to your domain name.
Change alex to your username.
server {
  listen 80;
  listen [::]:80;
  server_name yourdomain.com www.yourdomain.com;

  access_log /home/alex/sites/yourdomain.com/logs/access.log;
  error_log /home/alex/sites/yourdomain.com/logs/error.log;

  root /home/alex/sites/yourdomain.com/public/;
  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/yourdomain.com /etc/nginx/sites-enabled/yourdomain.com
  • 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 yourdomain.com.
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.
FLUSH PRIVILEGES;
  • Exit MariaDB/MySQL.
exit;

  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.
<?php
phpinfo();
?>
  • Visit the page in a browser. Go to http://yourdomain.com/info.php. You'll see something like this.
phpinfo.png

  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 Set Up NodeJs And Npm On Linux

How To Set Up PHP on Ubuntu

How To Set Up A Vue Web App Development Environment

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

How To Set Up WordPress On Google App Engine (GAE)

What Is Markdown ?

How To Set Up The Nginx Web Server On Ubuntu