Install Nginx Ubuntu Server 14.04


Install Nginx Ubuntu Server 14.04

Method 1: Installing Nginx from the Ubuntu Repositories

Nginx is available for installation from the default Ubuntu repositories using the apt package manager tool. If you are using an Ubuntu 14.04 server instance, using ssh to connect to the server as the root user and run the following command to update the apt package index:


apt-get update

Once the package index is updated next, install Nginx by typing:


apt-get install nginx

Once the installation is completed, check the version by typing:


nginx -v

This will print out the version number of the Nginx server running in your system, for example:


nginx version: nginx/1.4.6

You have now installed nginx from the Ubuntu repositories.

Method 2: Installing Nginx from the Source

Another method of installing nginx is to compile it from the source code. To start, you need to install some basic dependencies such as the compiler, libc headers and other libraries.


apt-get install build-essential
apt-get install libpcre3 libpcre3-dev libssl-dev zlib1g-dev

Once the dependencies are installed, download the source files of nginx from the official website:


wget http://nginx.org/download/nginx-1.4.6.tar.gz

Extract the archive by running the command:


tar -xzvf nginx-1.4.6.tar.gz

Move to the extracted directory and start the installation process by running the command:


cd nginx-1.4.6
./configure --sbin-path=/usr/sbin
make
make install

The configure command marks the installation prefix. The make command builds the application from the source files specified in the Makefile. And finally, the make install command installs the binary of the compiled application.

Once the installation is completed, check the version of Nginx:


nginx -v

You have now installed nginx from the source.

Configuring Nginx

Before starting Nginx, you need to configure it with the custom settings which are located in the Nginx configuration file, which is usually located at /etc/nginx/nginx.conf. These settings include server name, ports, and other configuration directives.

Once the configuration is done, start the web server by running the command:


service nginx start

You can now check the status of Nginx with the command:


service nginx status

Alternatively, you can also check if the web server is running correctly by connecting to it with a web browser.

Testing Nginx

Once the web server is running correctly, it’s time to test it to make sure it is working correctly. To do this, you can create a simple PHP script and place it in the web server document root (/var/www/html by default).

Save the following code as info.php in the document root:


echo phpinfo();
?>

Now, open a web browser and type in the address of your server, in this case it is http://localhost/info.php. You should get the output of phpinfo. This indicates that your web server is working correctly.

Securing Nginx

By default, Nginx comes with a basic configuration that should get you started. But there are some configuration settings which you must consider before deploying Nginx in production.

The first thing to consider is to modify the configuration file and change the default port of the web server to a non-standard port, which is less known to hackers. This will make it difficult for attackers to guess the port number of the web server and make it harder for them to gain access.

Another important security setting is to enable the ssl module and enable HTTPS for your web server, so that all communication between the server and the user is encrypted. To enable the ssl module, edit the /etc/nginx/nginx.conf file and add the following line:


ssl on;

Now, you need to generate a self-signed certificate and key pair, which you can do by running the following command:


openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/nginx/ssl/nginx.key -out /etc/nginx/ssl/nginx.crt

You will be prompted to enter some information which will be used for generating the certificate. You can skip all of these entries and just press Enter.

Once the certificate and key is generated, you need to configure Nginx to use the certificate and start accepting HTTPS connections. To do this, edit the /etc/nginx/nginx.conf file and add the following lines:


ssl_certificate /etc/nginx/ssl/nginx.crt;
ssl_certificate_key /etc/nginx/ssl/nginx.key;

Save the file and restart Nginx for the changes to take effect.

Conclusion

In this article, we have learned how to install and configure Nginx on an Ubuntu 14.04 server instance. We have also covered some basic security configurations to secure the web server. With these configurations, you should be able to deploy a secure and reliable web server in your environment.

FAQs

Q. Is Nginx a web server?

A. Yes, Nginx is a web server.

Q. What version of Nginx is available in the Ubuntu 14.04 repositories?

A. Nginx version 1.4.6 is available in the Ubuntu 14.04 repositories.

Q. How do I check the version of Nginx installed in my system?

A.You can check the version of Nginx by running the command:

nginx -v

Q. How do I configure Nginx to use HTTPS?

A.You need to generate a self-signed certificate and key pair and configure Nginx to use the certificate. Once the certificate and key is generated, edit the nginx.conf file and add the following lines:

ssl_certificate     /etc/nginx/ssl/nginx.crt;
ssl_certificate_key /etc/nginx/ssl/nginx.key;

Save the file and restart Nginx for the changes to take effect.

Thank you for reading this article. Please read other articles on our website for more information.

Leave a Reply

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