DEV Community

Seb
Seb

Posted on

Installing Nginx on Ubuntu

Today I'll be showing you how to install Nginx, pronounced Engine X, on Ubuntu..

But first.. what is Nginx?

Because I want to give you the straight forward explanation of what it is..

"NGINX is open source software for web serving, reverse proxying, caching, load balancing, media streaming, and more. It started out as a web server designed for maximum performance and stability"

To read more about it, please visit their website:
https://www.nginx.com/resources/glossary/nginx/

Today I'll be going over how I installed it on my machine!

1. Update your System

Before doing anything, I always like to make sure my system is up to date. To do the same, run this command:

n1

2. Install Nginx

Simple install it by running the command below:

n2

3. Create new folder in /var/www

Remember, if we want to create a folder in a specific directory, we will need to move to that directory.

n3

While in /var/www, we will create the new folder. Name it whatever you would like, and then, move into that folder, we will need to create a text file in there.

Here is an example of how to do so:

n5

4. Create index.html file

After we move into the new folder we just created. We are going to create the text file.

I prefer to use VIM, but you can honestly use anything you want, like nano.

If you are using VIM, run the command:

sudo vim index.html

n6

Once you've created the file using sudo vim index.html, you're going to need to edit that text file.

You first have to press 'i' to edit. You can copy what I did below as far as html tags, but you can write whatever you want between the tags.

When done, hit 'esc', type ':wq', and hit enter to quit. Now that file should be saved.

n7

5. Create the Virtual Host

To create the virtual host, you will need to move to the correct directory first.

See my example on line 5 in the screenshot:

nn

Once we are in that directory, we will create a new file.

sudo vim (name)

In my example, I also made the name of my file linuxseb, even though I named my folder that. I suggest creating different names for files and folders so it is less confusing, but it is up to you how you want to go about that! :)

See screenshot below, and copy what I have.

Also, don't forget!

Be sure for the server_name line and the root line to replace "linuxwithseb.com" and "linuxseb" with your correct names.

n11

6. Restart Nginx

We are just about done. Go ahead and restart the service by running the command:

sudo service nginx restart

7. Test it

Once that has been restarted, go to your browser and type localhost:81

and...

this is what you should get!

final

If your website is working and it shows up, it means you've successfully installed Nginx :)

To Conclude

Tutorials like this are for beginners and for practice. Please remember to "stop" Nginx if you're not going to use it. This is just for the sake of security.

To stop it, run this command:

sudo systemctl stop nginx

and to confirm that it has stopped, run this command:

sudo systemctl status nginx

and there you go, you've now learned how to install and configure Nginx on your machine! Thank you all for tuning into my tutorial! More coming in the future :)

Top comments (2)

Collapse
 
peter279k profile image
peter279k • Edited

Since Ubuntu 18.04 has been released, it should have the better way about using the systemctl to control Nginx service.

For example,

  • You can use the sudo systemctl status nginx to check Nginx service status.
  • Using the sudo systemctl reload nginx to reload Nginx setting.
  • Using the sudo systemctl restart nginx to restart Nginx service daemon.
  • Using the sudo systemctl enable --now nginx to enable Nginx service daemon starting on boot.
  • Using the sudo systemctl disable --now nginx to disable Nginx service daemon starting on boot.

By default, using the sudo apt install nginx will install a specific Nginx version that included in official Ubuntu mirror.

To install other/specific or upgrade Nginx versions, it can use the ondrej/nginx to resolve this issue :).

Collapse
 
danielvandenberg95 profile image
Daniël van den Berg

Do not use

sudo service nginx restart

. Instead use nginx -t && nginx -s reload`. This prevents downtime and warns if your config is invalid.