Get started with Nginx
Nginx is widely used open source and very powerful HTTP and reverse proxy server, a mail proxy server, and a generic TCP/UDP proxy server. For a long time, it has been powering world's most busiest and popular sites.
Step1: Install nginx
Nginx is written in C language. It can be compiled manually for specific platforms. Here are the steps to install nginx packages in multiple Linux/Unix operating systems and docker containers.
Don't forget to update repositories before running the following commands. i.e. apt update
Nginx installation command on different Operating Systems. (requires root permission)
Debian - apt-get install nginx-extras
Ubuntu - apt-get install nginx-extras
Alpine - apk add nginx
Arch Linux - pacman -S nginx
Kali Linux - apt-get install nginx-extras
Fedora - dnf install nginx-extras
OS X - brew install nginx
Raspbian - apt-get install nginx-extras
TERMUX - pkg install nginx
Dockerfile - dockerfile.run/nginx
Docker - docker run cmd.cat/nginx nginx
Step2: Verify installation
After installation nginx, by default, starts running in daemon mode on port 80.
If port 80 is not already in use by any other process then you will be able to access nginx server on this address http://127.0.0.1:80/
If you don't see this page. Check if the service is running with systemctl status nginx
or service nginx status
Step3: Customize configurations. (optional)
The next thing after installing nginx is to change the default configurations like web root directory, server port, proxy, SSL settings, access/error logs, sites enabled, virtual hosts, modules, user, etc.
You can find and control nginx configuration under /etc/nginx/
directory.
You can see full fledged examples of some files (like nginx.conf
, proxy.conf
, fastcgi.conf
, mime.types
.) here.
Step4: Add website (optional)
The sites-enabled
directory is used to add virtual hosts and customize the port, web root, index page, etc. Here is a sample of the virtual host configuration file.
server {
listen 8081;
listen [::]:8081;
server_name example.com;
root /var/www/website;
index index.php;
location / {
try_files $uri $uri/ =404;
}
}
Step5: Restart nginx server
After making any changes in configurations, we need to restart the nginx server with the new configurations. To start/stop/restart nginx server, we can use systemctl
or service
utility. (root permissions required)
$ systemctl restart nginx
$ service nginx restart
Now, you can access the server at a new port and a new virtual host. (add virtual host in /etc/hosts
).