How to install and set up nginx on Ubuntu

Let’s say we want to host in our server the domain name www.mysite.org. First of all we have to set an A DNS Record to assign the www.mysite.org to the IP address of our server. This can be done by our register control panel. In case we have changed the default name servers of our registar to a new site (usually to our hosting provider) we have to set the A DND Record there. I have set as Name Server the nameservers of DigitalOcean so I have set the A DNS Record from DigitalOcean Panel. A typical A DNS Record points the IP Address of our hosting server and it look like @ IP Address or www IP Address.

Remember that a change to an A Record needs time before it will be in effect. This time can be some minutes, some hours or even 1 or 2 days. The time period defined by the TTL value of the DNS Records. TTL (meaning Time To Live) is the time in seconds that a record is cached. After that time period DNS Record servers have to check again if the records setings have changed. So, if the TTL is 1800 this means that every 30 minutes (or 1800sec) the DND Record servers check for chances at this domain name DNS Records. So the maximum time for this is 1 hour (if a DNS Record server checked the DNS Records values for that domain 1 minute before you change it). So, if a TTL value is high (lets say 86400sec=24hours=1day) the new changes may need up to 2 days to change to all DNS Record servers.

For every site we have to make a seperate file with settings for nginx. So, go at the /etc/nginx/sites-available/ and cp the default to a new one. Althought we can give it any name we like, its best to give it the domain name of the site we want to host. We said we will host the domain www.mysite.org so we can name the new config file as mysite.org:

cd /etc/nginx/sites-available
sudo cp default mysite.org

Than we have to edit the new file (mysite.org) to set the path for the folder with the files we have to serve. Let’s say that the files for our new site are at the folder /usr/share/nginx/html/mysite.org. I will also assume you want an 301 redirect from mysite.org to www.mysite.org. I will only write the changes we have to make, not the whole file.

server {
#non-www to www redirect 301
server_name mysite.org;
return 301 $scheme://www.mysite.org$request_uri;

}

server {
listen 80;
#listen [::]:80 default_server ipv6only=on;

root /usr/share/nginx/html/mysite.org;
index index.php index.html index.htm;

# Make site accessible from http://localhost/
server_name www.mysite.org;


Also we have to create one more file for the default_server. This file will will display an error page when someone tries to see a domain that leeds to our server IP address but we don’t have set it up yet or we have it set wrong settings.

If we want to set another domain we host as the default server than we write it at its file. So the green text above from

listen 80;
have to be
listen 80 default_server;

I really don’t recommend this if you plan to host more than one site.
If you decide to follow my advice the default_server file must have this text:

server {
listen 80 default_server;
server_name _;
#return 444;
root /usr/share/nginx/html;
index 50x.html;
}

After we set up those 2 files we have to make a symbolic link for them from the folder /etc/nginx/sites-available at the folder /etc/nginx/sites-enabled:

ln -s /etc/nginx/sites-available/mysite.org /etc/nginx/sites-enabled/mysite.org
ln -s /etc/nginx/sites-available/default_server /etc/nginx/sites-enabled/default_server

An easier way to do this is:
cd /etc/nginx/sites-enabled
ln -s /etc/nginx/sites-available/mysite.org mysite.org
ln -s /etc/nginx/sites-available/default_server default_server

Remember in case you want to unlink an symbolic link the command is unlink filename.

After that we have to reload the nginx server:

sudo service nginx reload

Note
In case you want to upload large pictures (more than 1MB) inside WordPress or any other CMS you have to set the value of client_max_body_size to 2M or more. I usually set it to 3M or 4M althought I never upload an image more than 2MB. To increase it at 2M open the /etc/nginx/nginx.conf file and write inside the http braket:

client_max_body_size 2M;

nginx_conf-small

 

Owner and permissions

Nginx user is usually the www-data. This user must have permission to the files of you site. Also, you want to grant permissions to a user to upload and delete files with ftp connections. At most cases the folders must have 775 permission (read+write+execute for owner and group and read+ execute for others). Files usually must have 664 permissions (read+write for owner and group and read for others).
So, you must grant ownership and permissions to www-data and to the ftp user at all the files and folders. In our example the folder with the files of our site is the mysite.org. Lets suppose that the user for ftp is gms (you will connect with this user with FileZilla to upload files).

To grant ownership and those permissions go one level up of the folder mysite.org and do:

sudo chown -R www-data:gms mysite.org
sudo find mysite.org -type d -exec chmod 775 {} \;
sudo find mysite.org -type f -exec chmod 664 {} \;

Explanation:

  • The first command will grant the ownership to www-data and gms users to folder mysite.org and to all subfolders (option -R).
  • The second command use the find command to search within the folder mysite.org and find all the folders/directories (option -type d) and execute the command chmod to set permission 775 (option -exec chmod 775). The {} represent the folder name that the find command find. The \; tells to continue to the next folder.
  • Similar, the third command use the find command to search within the folder mysite.org and find all the files (option -type f) and execute the command chmod to set permission 664 (option -exec chmod 664). The {} represent the file name that the find command find. The \; tells to continue to the next file.

 

Final note

The above configuration is just some basic things you have to do to make nginx works. There are plenty more configuration and rewrite rules to secure your site or to make it more efficient.

One thought on “How to install and set up nginx on Ubuntu

Leave a Reply

Your email address will not be published. Required fields are marked *