From charlesreid1

Line 227: Line 227:
$ sudo service nginx restart
$ sudo service nginx restart
</pre>
</pre>
==Works!==
And whaddya know, it works.


=Using /www=
=Using /www=

Revision as of 16:17, 24 June 2015

I finally got fed up with Apache's endless permissions problems and incomprehensible labyrinth of config files and virtualhosts that never, ever work, ever, not even a single time.

I switched to nginx.

Also see Gunicorn page for running Python apps on top of nginx.

Installing

Ubuntu

sudo apt-get install nginx

Basic Info

By default, nginx serves files out of

/usr/share/nginx/html

The default config file is located in

/etc/nginx/sites-available/default

To start/stop nginx, use it as a service,

sudo service nginx start
sudo service nginx stop

It is generally a good idea to add virtual servers into the default configuration file, despite what I do below.

Virtual Hosts

Directory Structure and Permissions

I have created two http root directories, to serve two virtual hosts:

/www/example.com/public_html/
/www/test.com/public_html/

Both contain an index.html file with a simple hello world message.

Now I transfer ownership of these two directories to my regular username,

sudo chown -R $USER:$USER /www/example.com/public_html
sudo chown -R $USER:$USER /www/test.com/public_html
sudo chmod -R 755 /www/

Config File

Create a copy of the config file for each site:

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

The contents:


example.com config file

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

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

    root /www/example.com/public_html;
    index index.html index.htm;

    server_name example.com www.example.com;

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


test.com config file

/etc/nginx/sites-available/test.com
--------------------------
server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    root /www/test.com/public_html;
    index index.html index.htm;

    server_name test.com www.test.com;

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


Enabling Site/Site Config

To enable the site whose config files we just created, we create symlinks in nginx's sites-enabled:

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


Changing Hash Bucket Size

Mentioned by [1]. Edit the nginx config file:

sudo vim /etc/nginx/nginx.conf

and uncomment this line:

server_names_hash_bucket_size 64;

Restarting nginx to Implement Changes

sudo service nginx restart

Works

Works like a dream!

Virtual Hosts Revisited

I wasn't able to successfully implement virtual hosts with nginx using the procedure above, so I gave it another shot using the procedure outlined below.

Domain Records

I have two domains, doxuments.com and doxcompany.com, through NameCheap. I log in to name cheap and change the domain records of each domain.

For each domain, I create an A Name record for @ and an A Name record for www. Both point to the IP address of the server running nginx.

This makes two IP addresses, with two A Name records each, all pointing to the same IP address.

Set Permissions

I have two sites, served by a single instance of nginx out of two directories, /www/doxuments.com and /www/doxcompany.com. The permissions on these folders are set following [2]:

$ sudo chown -R www-data:www-data doxcompany.com/ doxuments.com/

$ sudo chmod 755 doxcompany.com/ doxuments.com/

Now onto the nginx settings.

Nginx

I change the domain records so that I have two domains, doxuments.com and doxcompany.com, pointing to the same server - one single IP address.

$ cat doxcompany.com
server {
    listen 80;
    listen [::]:80 ipv6only=on;

    server_name doxcompany.com;

    root /www/doxcompany.com/htdocs;
    index index.html index.htm;

    server_name doxcompany.com www.doxcompany.com;
    location / {
        try_files $uri $uri/ =404;
    }
}

$ cat doxuments.com
server {
    listen 80;
    listen [::]:80;

    server_name doxuments.com;

    root /www/doxuments.com/htdocs;
    index index.html index.htm;

    server_name doxuments.com www.doxuments.com;
    location / {
        try_files $uri $uri/ =404;
    }
}

doxcompany.com points to /www/doxcompany.com/htdocs and doxuments.com points to /www/doxuments.com/htdocs. Couldn't be simpler.

Enable site

I enable these sites by copying these sites-available files to the sites-enabled folder:

$ sudo ln -fs /etc/nginx/sites-available/doxuments.com /etc/nginx/sites-enabled/doxuments.com

$ sudo ln -fs /etc/nginx/sites-available/doxcompany.com /etc/nginx/sites-enabled/doxcompany.com

Restart Nginx

Now restart the nginx service.

$ sudo service nginx restart

Works!

And whaddya know, it works.

Using /www

If you want to use a particular directory structure, like /www/htdocs, you can do it this way:

Edit the file corresponding to the desired site name, something like /etc/nginx/sites-available/basic. Change the line:

    root /www/htdocs;

to reflect whatever directory structure you want. Then restart the service:

sudo service nginx restart

and you're off!

Errors

Starting Nginx service, but nginx is not listening

I was trying to get nginx to run, but having difficulties. I tried modifying the nginx config file, and then I restarted the nginx service:

sudo service nginx restart

and I could see that nginx was actually running when the service would start or restart:

$ ps aux www | grep nginx
root      9830  0.0  0.1  86288  2912 ?        Ss   00:38   0:00 nginx: master process /usr/sbin/nginx
www-data  9831  0.0  0.1  86612  3380 ?        S    00:38   0:00 nginx: worker process
www-data  9833  0.0  0.1  86612  3380 ?        S    00:38   0:00 nginx: worker process
www-data  9834  0.0  0.1  86612  3380 ?        S    00:38   0:00 nginx: worker process
www-data  9835  0.0  0.1  86612  3380 ?        S    00:38   0:00 nginx: worker process
charles   9840  0.0  0.1   9460  2228 pts/0    S+   00:38   0:00 grep nginx

but when I looked at processes and the ports they were running on, I didn't see nginx anywhere:

$ sudo netstat -altnp
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1929/sshd
tcp        0      0 127.0.0.1:6010          0.0.0.0:*               LISTEN      9396/0
tcp        0      0 127.0.0.1:6011          0.0.0.0:*               LISTEN      9753/1
tcp        0      0 0.0.0.0:5000            0.0.0.0:*               LISTEN      3403/python
tcp        0      0 127.0.0.1:27017         0.0.0.0:*               LISTEN      32687/mongod
tcp        0      0 45.33.40.53:5000        61.240.144.65:60000     ESTABLISHED 3403/python
tcp        0     48 45.33.40.53:22          24.19.57.61:52552       ESTABLISHED 9317/sshd: charles
tcp       35      0 45.33.40.53:5000        66.240.236.119:43072    CLOSE_WAIT  -
tcp        0      0 45.33.40.53:22          24.19.57.61:52587       ESTABLISHED 9700/sshd: charles
tcp       35      0 45.33.40.53:5000        188.138.9.50:41610      CLOSE_WAIT  -
tcp       35      0 45.33.40.53:5000        71.6.167.142:45389      CLOSE_WAIT  -
tcp        0      0 45.33.40.53:5000        61.240.144.67:60000     ESTABLISHED -
tcp       35      0 45.33.40.53:5000        198.20.69.98:48423      CLOSE_WAIT  -
tcp6       0      0 :::22                   :::*                    LISTEN      1929/sshd
tcp6       0      0 ::1:6010                :::*                    LISTEN      9396/0
tcp6       0      0 ::1:6011                :::*                    LISTEN      9753/1

I checked to make sure the configuration file was ok:

$ sudo nginx -t -c /etc/nginx/nginx.conf
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful