A Beginner's Guide to Nginx: Getting Started with the Popular Web Server.
Nginx is a lightweight, high-performance web server that is used by millions of websites around the world. It is known for its speed, scalability, and reliability. This beginner's guide to Nginx will teach you the basics of what you need to know to get started with this popular web server.
Well, we have all heard about Nginx as a web server, reverse proxy, load balancer, and proxy server for email communications, and it is also known for its performance and stability. Let us start with some hands-on with Nginx and see what it can do.
How to Set Up Nginx
Let's first install nginx by running the below command
sudo apt install nginx -y
Once installed, it should be automatically up and running. You can check the status by running.
sudo systemctl status nginx
If the status is running, then we all are set. If not, then we may have to explicitly start the service by running.
sudo systemctl start nginx
Now, if everything is set, you can visit localhost
, and you should see an nginx welcome page.
Getting Started with Web Server Configuration
After installing Nginx, the Linux system stores all the Nginx files in the directory /etc
. So let's go to /etc/nginx
and check all the files. In those files, there is one particular file which is very interesting to us, i.e. nginx.conf
, which has all the instructions for various configurations.
Let's do one thing. Let's take a backup of that file and let's write our own code. For taking a backup, lets run this
sudo mv nginx.conf nginx.conf.backup
Now, lets have our configuration.
events {
}
http {
server {
listen 80;
return 200 "Hello from nginx!"
}
}
It's a simple one; we just defined an HTTP server which listens to port 80, and when accessed, it will just return Hello from nginx!
Before that, we need to tell Nginx about our new configuration and then restart Nginx. First, let's check for any syntax mistakes by running.
sudo nginx -t
If there were no errors, it should say successful at the end. Once that is done, lets now restart our nginx by running
sudo systemctl restart nginx
Note:
There are various ways to reload nginx, which we will mention in further blogs.
Now, let's refresh our localhost
and see the response. Vola. it shows Hello from nginx!
Conclusion
See, a simple Nginx server is relatively easy to set up. Let's learn more about nginx and its configurations in our next blog.