
Before you learn How to install/configure – NGINX, you should have a regular, non-root user with Sudo privileges or a root user configured on your server.
Step 1:
(A) Installing Nginx: https://do.co/2STpzpJ
root@VirtualBox:/#sudo apt-get update
root@VirtualBox:/#sudo apt-get install nginx
You can access How to install/configure – NGINX landing page to confirm that the software
is running properly by visiting your server’s domain name or public IP address in your web browser.
http://server_domain_name_or_IP
(B) Manage the Nginx Process
Now that you have your web server up and running, we can go over some basic management commands.
-- To stop your web server, you can type: root@VirtualBox:#sudo service nginx stop -- To start the web server when it is stopped, type: root@VirtualBox:#sudo service nginx start -- To stop and then start the service again, type: root@VirtualBox:#sudo service nginx restart Note: We can make sure that our web server will restart automatically when the server is rebooted by typing: root@VirtualBox:#sudo update-rc.d nginx defaults
This should already be enabled by default, so you may see a message like this: System start/stop links for /etc/init.d/nginx already exist.
(C) By default, Nginx on Ubuntu 14.04 has one server block enabled by default.
It is configured to serve documents out of a directory at: /usr/share/nginx/html
We won’t use the default since it is easier to work with things in the /var/www directory.
Note: Ubuntu’s Nginx package does not use /var/www as its document root by default due to a Debian policy about packages utilizing /var/www.
Since we are users and not package maintainers, we can tell Nginx that this is where we want our document roots to be. Specifically, we want a directory for each of our sites within the /var/www directory and we will have a directory under these called html to hold our actual files.
Note: First, we need to create the necessary directories. We can do this with the following command. The -p flag tells mkdir to create any necessary parent directories along the way:
root@VirtualBox:#sudo mkdir -p /var/www/example.com/html
root@VirtualBox:#sudo mkdir -p /var/www/test.com/html
Now that you have your directories created, we need to transfer ownership to our regular user. We can use the $USER environmental variable to substitute the user account that we are currently signed in on.
This will allow us to create files in this directory without allowing our visitors to create content.
root@VirtualBox:#sudo chown -R $USER:$USER /var/www/example.com/html
root@VirtualBox:#sudo chown -R $USER:$USER /var/www/test.com/html
Note: The example.com and test.com are necessary to difference different web pages to avoid confusion.
The permissions of our web roots should be correct already if you have not ^ modified your umask value, but we can make sure by typing:
root@VirtualBox:#sudo chmod -R 755 /var/www
Step Two – Create Sample Pages for Each Site
Now that we have our directory structure set up, let’s create a default page
for each of our sites so that we will have something to display.
Create an index.html file in your first domain:
root@VirtualBox:/#nano /var/www/example.com/html/index.html
Note: index.html (the index here can be renamed to anything)
Save and close the file when you are finished.
Since the file for our second site is basically going to be the same, we can copy it over to our second document root like this:
root@VirtualBox:/#cp /var/www/example.com/html/index.html /var/www/test.com/html/
Step Three – Create Server Block Files for Each Domain
Now that we have the content we wish to serve, we need to actually create the server blocks that will tell Nginx how to do this.
By default, Nginx contains one server block called default which we can use as a template for our own configurations. We will begin by designing our first domain’s server block, which we will then copy over for our second domain and make the necessary modifications.
Create the First Server Block File
As mentioned above, we will create our first server block config file by copying over the default file:
root@VirtualBox:#sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/example.com Now, open the new file you created in your text editor with root privileges: root@VirtualBox:#sudo nano /etc/nginx/sites-available/example.com The file will look similar to this: root@VirtualBox:/etc/nginx/sites-available$ vim default # You should look at the following URL's in order to grasp a solid understanding # of Nginx configuration files in order to fully unleash the power of Nginx. # http://wiki.nginx.org/Pitfalls # http://wiki.nginx.org/QuickStart # http://wiki.nginx.org/Configuration # Generally, you will want to move this file somewhere, and start with a clean # file but keep this around for reference. Or just disable in sites-enabled. # Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples. # Default server configuration server { listen 80 default_server; listen [::]:80 default_server; # SSL configuration # listen 443 ssl default_server; # listen [::]:443 ssl default_server; # Note: You should disable gzip for SSL traffic. # See: https://bugs.debian.org/773332 # Read up on ssl_ciphers to ensure a secure configuration. # See: https://bugs.debian.org/765782 # Self signed certs generated by the ssl-cert package # Don't use them in a production server! # include snippets/snakeoil.conf; root /var/www/html; # Add index.php to the list if you are using PHP index index.html index.htm index.nginx-debian.html; server_name _; location / { # First attempt to serve request as file, then # as directory, then fall back to displaying a 404. try_files $uri $uri/ =404; } # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 #location ~ .php$ { # include snippets/fastcgi-php.conf; # # With php7.0-cgi alone: # fastcgi_pass 127.0.0.1:9000; # # With php7.0-fpm: # fastcgi_pass unix:/run/php/php7.0-fpm.sock; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one #location ~ /.ht { # deny all; #} } "default" [readonly] 86L, 2074C
Note: Now do this for the second site as well. After creating the second server block file,
Ensure to Take a look at the listen directives again. If you left the default_server
option enabled in the last file, you’ll have to remove it in this file. Furthermore, you’ll have to get rid of the ipv6only=on option,
as it can only be specified once per address/port combination:
Now Adjust the document root directive to point to your second domain’s document root:
root /var/www/test.com/html;
Adjust the server_name to match your second domain and any aliases:
server_name test.com http://www.test.com;
This is how it looks when you view it using the “cat command”
test@test-VirtualBox:/etc/nginx/sites-available$ cat default.
#server {
# listen 80;
# listen [::]:80;
#
# server_name example.com;
#
# root /var/www/example.com;
# index index.html;
#
# location / {
# try_files $uri $uri/ =404;
# }
#}
Note: Use any of the Editors to modify the two fields above. root /var/www/test.com/html; server_name test.com www.test.com;
Step Four – Enable your Server Blocks and Restart Nginx
You now have your server blocks created, we need to enable them. We can do this by creating symbolic links from these files to the sites-enabled directory, which Nginx reads from during startup.
We can create these links by typing:
root@VirtualBox:/sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
root@VirtualBox:/sudo ln -s /etc/nginx/sites-available/test.com /etc/nginx/sites-enabled/
These files are now in the enabled directory. However, the default server block file we used as a template is also enabled currently and will conflict with our file that has the default_server parameter set.
Note: We can disable the default server block file by simply removing the symbolic link. It will still be available for reference in the sites-available directory, but it won’t be read by Nginx on startup:
root@VirtualBox:/sudo rm /etc/nginx/sites-enabled/default
We also need to adjust one setting really quickly in the default Nginx configuration file. Open it up by typing:
root@VirtualBox:/sudo nano /etc/nginx/nginx.conf
We need to uncomment one line. Find and remove the comment from this:
server_names_hash_bucket_size 64;
Now, we are ready to restart Nginx to enable your changes. You can do that by typing:
root@VirtualBox:/sudo service nginx restart
Set Up Local Hosts File (Optional)
Suppose you have not known How to install/configure – NGINX been using domain names that you own but have been using dummy values. In that case, you can modify your local computer’s configuration to allow you to test your Nginx server block configuration temporarily.
This will not allow other visitors to view your site correctly, but it will allow you to reach each site independently and test your configuration. This works by intercepting requests, usually going to DNS to resolve domain names. Instead, we can set the IP addresses we want our local computer to use when we request the domain names.
Ensure you operate on your local computer during these steps and not your VPS server. You will need to have root access, be a member of the administrative group, or otherwise be able to edit system files to do this.
If you are on a Mac or Linux computer at home, you can learn How to install/configure – NGINX and edit the file needed by typing:
root@VirtualBox:/sudo nano /etc/hosts
If you are on Windows, you can find instructions for altering your host’s file here. You need your server’s public IP address and the domains you want to route to the server. Assuming that my server’s public IP address is xxxx.xxx.111.111, the lines I would add to my file would look something like this:
127.0.0.1 localhost 127.0.0.1 guest-desktop 111.111.111.111 example.com 111.111.111.111 test.com This will intercept any requests for example.com and test.com and send them to your server, which is what we want if we don't actually own the domains that we are using. Save and close the file when you are finished.
Step Six – Test your Results
Now that you learned How to install/configure – NGINX, you should test that your server blocks function correctly. You can do that by visiting the domains in your web browser:
http://example.com
You should see a page that looks like this: