Skip to content

How to create a server block

Daisho Komiyama edited this page Apr 7, 2019 · 10 revisions

This post explains several steps I have done to get daishodesign.com up and running on Ubuntu16.04 Nginx web server.

1. Connect via SSH to your server

ssh root@xxx.xxx.xx.xxx

2. Create a directory in /var/www/ then put a test html file

Creating directories and change directory in one line. sudo mkdir -p /var/www/daishodesign.com/html && cd /var/www/daishodesign.com/html
In the directory, create a test html file. sudo vim index.html then write 'Hello world' in it. This is for testing purposes. *It looks like the last /html is additional. You can (I guess) omit it if you don't like it.

3. Reassign ownership of the web directories to my normal user account.

sudo chown -R $USER:$USER /var/www/daishodesign.com/html

4. Create server block file

Navigate to /etc/nginx/sites-available/ then create server block file with name of daishodesign.com
*If you have done the same thing before, just copy existing server block file of other site (Say example.com if there is) to make This step faster. Type cp example.com daishodesign.com then edit it as shown below.

server {
    listen 80;
    listen [::]:80;

    server_name daishodesign.com www.daishodesign.com;

    root /var/www/daishodesign.com/html;
    index index.html;

    location / {
        try_files $uri $uri/ /index.html;
        error_page 405 =200 $uri;
    }
}

5. Test and restart Nginx

Type sudo nginx -t to test what you've done so far. If everything is okay you'll get this messages.

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Restart Nginx with sudo systemctl restart nginx

6. Go back to your local machine

Open new tab in your teminal. It's time to work on your local machine. Make sure there's $ sign at the beginning of your command line which shows you are on local machine. On the other hand, if you see # sign, you are on remote server so don't do anything crazy.

7. Modify local host file

Check if the site is up and running. Open hosts file by sudo vim /etc/hosts then add a line xxx.xxx.xx.xxx daishodesign.com www.daishodesign.com *Replace xxx.xxx.xx.xxx with your server's IP.

8. Test

Type http://www.daishodesign.com in URL. 'Hello world' should be there. If not, go back and check each step carefully.

Clone this wiki locally