Skip to content

Instantly share code, notes, and snippets.

@evgv
Last active August 14, 2017 11:24
Show Gist options
  • Save evgv/1d523522a27d5338dd6e68411493a02a to your computer and use it in GitHub Desktop.
Save evgv/1d523522a27d5338dd6e68411493a02a to your computer and use it in GitHub Desktop.
Nginx basic setup steps for Ubuntu

Nginx basic setup steps for Ubuntu

Update apt and install nginx

sudo apt-get update
sudo apt-get install nginx

Start/stop server

sudo /etc/init.d/nginx {command} // or sudo service nginx {command}

nginx {start|stop|restart|reload|force-reload|status|configtest|rotate|upgrade}

Create test site example.com

Add directory, set rights and create index.html

sudo mkdir -p /var/www/example.com/html

sudo chown -R {USER}:{USER} /var/www/example.com/html

nano /var/www/example.com/html/index.html

Add html markup INto index.html

<html>
    <head>
        <title>Welcome to Example.com!</title>
    </head>
    <body>
        <h1>Success!  The example.com server block is working!</h1>
    </body>
</html>

Create virtual host

Copy sample of virtual host

sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/example.com

Edit virtual host params

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

Minimal required options: port, root, index, server_name and location

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

        root /var/www/example.com/html;
        index index.php index.html index.htm;

        server_name example.com www.example.com;

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

Enabe virtual host

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

Check nginx configuration file syntax

sudo nginx -t

Reload nginx

sudo /etc/init.d/nginx reload

Add new domain to hosts

Open hosts file

sudo nano /etc/hosts

Add domain

127.0.0.1   example.com www.example.com

Check our configured site in browser

http://example.com
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment