Nginx is a popular open-source web server that can handle high concurrency and serve static and dynamic content. It can also act as a reverse proxy, load balancer, and HTTP cache. In this article, we will show you how to install and configure Nginx on a Linux system.
Installing Nginx
The easiest way to install Nginx is to use the package manager of your Linux distribution. For example, on Ubuntu or Debian, you can use the following command:
sudo apt update
sudo apt install nginx
On CentOS or Fedora, you can use the following command:
sudo yum update
sudo yum install nginx
Alternatively, you can download the source code from the official website and compile it yourself. You will need to install some dependencies, such as GCC, PCRE, zlib, and OpenSSL. You can also customize the configuration options and modules according to your needs. For example, to install Nginx with the default options, you can use the following commands:
wget https://nginx.org/download/nginx-1.21.4.tar.gz
tar xzf nginx-1.21.4.tar.gz
cd nginx-1.21.4
./configure
make
sudo make install
By default, Nginx will be installed in the /usr/local/nginx
directory. You can change the installation path by using the --prefix
option in the ./configure
command.
Configuring Nginx
After installing Nginx, you can start, stop, and restart the web server by using the following commands:
sudo nginx # start the web server
sudo nginx -s stop # stop the web server
sudo nginx -s reload # reload the configuration
You can also use the systemctl
command to manage the Nginx service, if your Linux system supports systemd. For example, you can use the following commands:
sudo systemctl start nginx # start the web server
sudo systemctl stop nginx # stop the web server
sudo systemctl restart nginx # restart the web server
sudo systemctl reload nginx # reload the configuration
sudo systemctl status nginx # check the status of the web server
sudo systemctl enable nginx # enable the web server to start on boot
The main configuration file of Nginx is located in the /etc/nginx/nginx.conf
directory, if you installed Nginx from the package manager, or in the /usr/local/nginx/conf/nginx.conf
directory, if you installed Nginx from the source code. You can edit this file to change the global settings of Nginx, such as the number of worker processes, the error log level, the user and group, and the HTTP directives.
The HTTP directives define how Nginx handles HTTP requests. They include parameters such as the port number, the server name, the document root, the index file, the location blocks, and the proxy settings. You can also include other configuration files in the main configuration file by using the include
directive. For example, the default configuration file includes the /etc/nginx/conf.d/*.conf
and the /etc/nginx/sites-enabled/*
directories, where you can store the configuration files for each virtual host or server block.
A virtual host or server block is a way of hosting multiple websites on a single web server. Each virtual host has its own configuration file, where you can specify the domain name, the document root, the SSL certificates, the access and error logs, and the location blocks for each website. A location block is a way of defining how Nginx handles requests for a specific URI or path. You can use regular expressions, modifiers, and directives to match and process different requests. For example, you can use the proxy_pass
directive to pass requests to a backend server, such as a PHP-FPM or a Node.js application. You can also use the try_files
directive to serve static files or fallback to a dynamic script.
To create a virtual host or server block, you need to create a configuration file in the /etc/nginx/conf.d
or the /etc/nginx/sites-available
directory, and then create a symbolic link to the /etc/nginx/sites-enabled
directory. For example, to create a virtual host for the domain example.com
, you can use the following commands:
sudo nano /etc/nginx/sites-available/example.com.conf # create and edit the configuration file
sudo ln -s /etc/nginx/sites-available/example.com.conf /etc/nginx/sites-enabled/ # create a symbolic link
sudo nginx -t # test the configuration
sudo nginx -s reload # reload the configuration
Here is an example of a configuration file for a virtual host that serves static files from the /var/www/example.com
directory and passes PHP requests to a PHP-FPM server listening on port 9000:
server {
listen 80; # listen on port 80
server_name example.com www.example.com; # specify the domain name
root /var/www/example.com; # specify the document root
index index.html index.php; # specify the index file
access_log /var/log/nginx/example.com.access.log; # specify the access log
error_log /var/log/nginx/example.com.error.log; # specify the error log
location / {
try_files $uri $uri/ =404; # try to serve static files or return 404
}
location ~ \.php$ {
try_files $uri =404; # try to serve PHP files or return 404
fastcgi_pass 127.0.0.1:9000; # pass requests to PHP-FPM
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; # set the script filename
include fastcgi_params; # include the fastcgi parameters
}
}
Conclusion
In this article, we have shown you how to install and configure Nginx on a Linux system. Nginx is a powerful and versatile web server that can handle high concurrency and serve static and dynamic content. https://fileenergy.com/ You can also use Nginx as a reverse proxy, load balancer, and HTTP cache. You can customize the configuration of Nginx by editing the main configuration file and creating virtual hosts or server blocks for each website. You can also use location blocks to define how Nginx handles requests for different URIs or paths. We hope this article has helped you to get started with Nginx and enjoy its features and benefits.