Table of contents
Preface
Nginx is an excellent high-performance web server that is widely used to host websites and applications. This article aims to provide you with a detailed guide to help you successfully install, configure and start the Nginx server on your Linux system. Through this process, you will learn how to integrate Nginx into your system to easily host your websites and applications.
Install nginx
Install dependencies
Before you start installing Nginx, you first need to install some dependencies to ensure that Nginx compiles and runs properly. Open a terminal and execute the following command:
yum install -y wget gcc-c++ pcre-devel zlib-devel openssl-devel
This will install the necessary tools and libraries to enable Nginx to compile and run.
Download Nginx
Download the latest stable version from the Nginx official website. You can find the download link for the latest version athttps://nginx.org/en/download.html.
# 例如,下载Nginx 1.24.0版本
wget https://nginx.org/download/nginx-1.24.0.tar.gz
Unzip Nginx
Unzip the downloaded Nginx source code package:
tar -zxvf nginx-1.24.0.tar.gz
Compile and install
Enter the decompressed Nginx directory and compile and install:
# 切换到 Nginx 解压目录
cd nginx-1.24.0
# 编译前的配置和依赖检查
./configure
# 编译安装
make && make install
After Nginx is installed, the /usr/local/nginx
directory is automatically created by default, and necessary files and directories are created, including configuration files, log files, HTML files, etc.
Firewall settings
If your system has a firewall enabled, you need to turn it off
# 查看防火墙状态
systemctl status firewalld
# 关闭防火墙
systemctl stop firewalld
# 开机禁用防火墙
systemctl disable firewalld
Start Nginx
Enter the Nginx installation directory:
cd /usr/local/nginx/sbin
Then, start the Nginx server:
./nginx
You can now verify that Nginx is working properly by accessing your server's IP address or domain name through your browser.
Configure Nginx to serve the system
Making Nginx a system service eliminates the need for you to manually execute commands in the Nginx installation directory to start it. Instead, the system will automatically start Nginx when booting, making the startup process more convenient and automated.
Configure Nginx service files
Create a new service file in the /etc/systemd/system/
directory, for example nginx.service
:
vi /etc/systemd/system/nginx.service
In the opened file, add the following content:
[Unit]
Description=Nginx HTTP Server
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=true
[Install]
WantedBy=multi-user.target
Execute the following command to reload the systemd configuration file:
systemctl daemon-reload
Start Nginx service
Execute the following command to start the Nginx service:
systemctl start nginx
Nginx will now run in the background as a system service.
Set up auto-start at boot
If you want Nginx to start automatically when the system starts, you can execute the following command to set it to start automatically at boot:
systemctl enable nginx
This way, Nginx will start automatically when the system starts.
Check Nginx status
systemctl status nginx
Stop Nginx service
systemctl stop nginx
Restart Nginx service
systemctl restart nginx
Uninstall Nginx
If you need to uninstall Nginx, you can perform the following steps:
Stop the Nginx service:
Execute the following command to stop the Nginx service:
systemctl stop nginx
If you are using non-system service mode to start Nginx, you can use the following command to stop Nginx:
/usr/local/nginx/sbin/nginx -s stop
Determine where Nginx is installed:
Execute the following command to find the installation location of Nginx:
whereis nginx
This command will return the path of the Nginx executable file, for example /usr/local/nginx
.
Delete the Nginx installation directory:
Execute the following command to delete the Nginx installation directory:
rm -rf /usr/local/nginx
Find and delete related files:
Execute the following command to find files that may be related to Nginx:
find / -name nginx
This will search the file system for all file names containing "nginx" and you can delete these files if necessary.
After completing the above steps, Nginx will be completely uninstalled.
Conclusion
Nginx is a powerful and versatile web server that not only provides excellent performance but also has flexible configuration options. Through the guide in this article, you have successfully mastered the basic installation and configuration of Nginx, and learned how to set it up as a system service. This will provide stability and high performance to your web application while giving you rich customization options to meet various needs. Hopefully this guide has helped you on your successful journey in the world of Nginx.