Nginx

This is a report for the Configuration Management Systems course taught by Tero Karvinen

I started setting up nginx manually by updating and downloading nginx.

sudo apt update -y
sudo apt install nginx -y

At this point i verified that the webserver was running with the following command.

curl localhost

This command retured the defaul nginx page.

<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

I wanted to do something more interesting than just a webserver and create a virtual host in nginx. I looked at the default configuration in /etc/nginx/sites-available/default and saw this example at the bottom of the file.

# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
#server {
#       listen 80;
#       listen [::]:80;
#
#       server_name example.com;
#
#       root /var/www/example.com;
#       index index.html;
#
#       location / {
#               try_files $uri $uri/ =404;
#       }
#}

Based on this example i created my own configuration as follows.

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

        server_name heiskane;

        root /var/www/heiskane/html;
        index index.html;

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

This will return a webpage from /var/www/heiskane/html when the host header in the HTTP request is set to heiskane. I also created this directory.

sudo mkdir -p /var/www/heiskane/html

I added a basic index.html file to test.

echo '<h1>Hello World</h1>' > index.html

At this point i fixed the permissions on these files and restarted nginx.

sudo chown -R www-data:www-data /var/www/heiskane/html
sudo chmod -R 755 /var/www/heiskane/html
sudo systemctl restart nginx

Lastly to test if this is working i added heiskane to /etc/hosts file like this.

127.0.0.1       localhost heiskane
127.0.1.1       vagrant.vm      vagrant

# The following lines are desirable for IPv6 capable hosts
::1     localhost ip6-localhost ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
127.0.2.1 slave-1 slave-1

Here i realized that its not working yet because i forgot to actually enable this configuration. To enable it a symlink to the configuration file must be added in /etc/nginx/sites-enabled/.

sudo ln -s /etc/nginx/sites-available/heiskane /etc/nginx/sites-enabled/

Now after restarting nginx again running curl heiskane return the following content.

<h1>Hello World</h1>

Everything seems to be working here now so its time for automation. I had actually already created a salt state to do this for me during the lesson but ill go over it here. I started with the installation of nginx.

nginx:
  pkg.installed

I verified that nginx was installed and moved on to getting the files over to the minion. I used file.recurse in case that i want to jave multiple files in the future.

/var/www/heiskane/html:
  file.recurse:
    - source: salt://nginx/html/
    - user: www-data
    - group: www-data
    - makedirs: True
    - file_mode: 755

As usual after applying the state i verified that everything tranfered and moved on to the next thing which was the configuration file and the symlink for it.

/etc/nginx/sites-available/heiskane:
  file.managed:
    - source: salt://nginx/heiskane

/etc/nginx/sites-enabled/heiskane:
  file.symlink:
    - target: /etc/nginx/sites-available/heiskane

Again i verified that the files existed on the minion and moved on to restarting nginx.

restart_nginx:
  service.running:
    - name: nginx
    - enable: True
    - restart: True
    - watch:
      - file: /etc/nginx/sites-available/heiskane
    - require:
      - pkg: nginx

As seen above im watching the configuration file and nginx is restarted when the file changes. This also means that for it to work the first time i added a new line in the file so it would make nginx restart. Lastly for testing purposes i also added a new hosts file so i can curl the heiskane domain i created here.

/etc/hosts:
  file.managed:
    - source: salt://nginx/hosts

Running curl heiskane on the minion returns the new page i had created now but this minion already had everything setup so i also tried this on a fresh minion. Applying it to the frsh minon was successful as well. I actually applied the state twice to also make sure it is idempotent as well. Ill leave the full init.sls below.

nginx:
  pkg.installed

/var/www/heiskane/html:
  file.recurse:
    - source: salt://nginx/html/
    - user: www-data
    - group: www-data
    - makedirs: True
    - file_mode: 755

/etc/nginx/sites-available/heiskane:
  file.managed:
    - source: salt://nginx/heiskane

/etc/nginx/sites-enabled/heiskane:
  file.symlink:
    - target: /etc/nginx/sites-available/heiskane

restart_nginx:
  service.running:
    - name: nginx
    - enable: True
    - restart: True
    - watch:
      - file: /etc/nginx/sites-available/heiskane
    - require:
      - pkg: nginx

/etc/hosts:
  file.managed:
    - source: salt://nginx/hosts