Quick Setup NGINX
This is a quick guide on how to install NGINX. The following examples assumes you are using Debian and have a user with sudo
access. A domain is also required if you wish to host websites on the world wide web.
Notes about domains
If you want your website to be accessible from the internet, you will need to purchase a domain from a registrar. From there you will be able to create records to point to your server's IP address.
Purchasing a domain may not be an option or you just want to setup something quick. In that case, editing the hosts file to make an entry point to your machine will also work. On Linux, edit the /etc/hosts
file and add a new line containing the IP address of your machine and the domain you want to point it to. If you're following this tutorial, it would look like 192.168.1.23 example.vlenture.org
. The only downside is that you would have to do this on every machine you want to access the website with.
There is also the option to make your own local DNS server to centrally manage your records. All that would be required is to change the DNS servers on your router or on each device as well as creating the records itself.
Installing NGINX
Update the repos and install any packages that need upgrading:
sudo apt update
sudo apt upgrade
Then install NGINX:
sudo apt install nginx
NGINX should be enabled to startup on boot once installed. If this isn't the case, ensure no other web servers are installed e.g. Apache or programs running on port 80.
NGINX is now installed and setup to auto-start on each boot.
Creating a sample website
To host something different than the default NGINX page, you will need to create a new folder containing files for your website.
First, let's make a new directory to host our web files:
sudo mkdir /var/www/examplesite
From here we can set our permissions so we don't need sudo
to work inside of this folder.
chown
changes the user:group to be the current user you are logged in as. chmod
modifies the read/write/execute permissions. In this case it allows read/write/execute for the user and only read/execute for the group and others.
sudo chown $USER:$USER /var/www/examplesite
sudo chmod 755 /var/www/examplesite
Change directory to the newly created folder and create a new file called index.html
:
cd /var/www/examplesite
touch index.html
Using an editor such as vim/nano, edit the contents of index.html
to look like the following:
<html>
<body>
<h1>Welcome to "Example Site"</h1>
<p>Everything seems to be working just fine!</p>
</body>
</html>
This is just a bare-bones HTML file to show that we have successfully setup our web server.
Setting up the server block
Next we make a new configuration file for our webpage in the /etc/nginx/sites-available
folder:
In Debian, the folder in which to create configuration files for server blocks is at /etc/nginx/sites-available/
. The location may vary distro to distro. E.g. on OpenSUSE the location is /etc/nginx/vhosts.d/
.
sudo vim /etc/nginx/sites-available/examplesite
Insert the following code block into examplesite
:
Swap the example.vlenture.org
to a domain that you have setup records for in order to access the website
server {
listen 80;
listen [::]:80;
server_name example.vlenture.org;
root /var/www/examplesite;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
Then we make a symbolic link to the sites-enabled
directory in order to enable the website:
sudo ln -s /etc/nginx/sites-available/examplesite /etc/nginx/sites-enabled/
Now we make NGINX test our configuration files to ensure there is no issues:
sudo nginx -t
Finally, reload the NGINX service to make the website accessible:
sudo service nginx reload
If we head over to the domain we setup in the server block, we should be able to see the contents of the HTML file we created earlier on.
Follow our HTTPS guide to encrypt your website within a few minutes!
Conclusion
You have successfully setup NGINX and created a functioning website! Most software will require a bit more than what we covered here, but you should have a basic understanding of how to setup a website. Check out the attachments on the left to check out the NGINX documentation and to learn about Linux permissions.