1. Background
Nginx is a lightweight Web server, reverse proxy server , because it occupies less memory, starts extremely fast, and has high concurrency capabilities, it is widely used in Internet projects. This article introduces three methods for installing nginx based on the linux environment. The installation version is the latest stable version launched by the official website at this stage.
2. Environmental description
software project | Version |
---|---|
operating system | Centos7.9 |
nginx | 1.20.2 |
docker | 20.10.9 |
3. Four installation methods of linux
- yum installation: simple and convenient, not easy to make mistakes
- rpm installation package installation: convenient and convenient
- Source package installation: a bit cumbersome, good service performance
- docker installation: easy and fast
in yum , source code installation, docker installation rely on Internet access, rpm can be installed offline.
4. Installation steps
Check the port before installation. By default, nginx uses port 80. If port 80 is already occupied, an error may be reported during the startup process. For port occupancy, you can stop the service that occupies the port or use another port to start nginx. Introduce again in this article. refer to" Centos7 firewall settings>
netstat -nltp | grep 80
4.1 yum install ningx
nginx adds yum repro library
# Download the nginx package wget https://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
# Build nginx's yum repository rpm -ivh nginx-release-centos-7-0.el7.ngx.noarch.rpm
View nginx information
yum info nginx
Check the nginx version in the yum source repository
yum --showduplicates list nginx | expand
Install nginx, the latest stable version and nginx 1.20.2 are installed by default
yum install nginx
4.2 source code Compile and install nginx
The installation steps of Nginx source package are more complicated than other installation methods, but the operation is not complicated, and some Nginx dependent libraries need to be installed in advance.
4.2.1 Dependent library installation
1. Install the gcc environment
Depends on the gcc environment when compiling
yum -y install gcc gcc-c++ autoconf automake make
2. Install pcre
Provide nginx to support rewrite function
yum -y install pcre pcre-devel
3. Install zlib
The zlib library provides many ways to compress and decompress, and nginx uses zlib to gzip the contents of the http package
yum -y install zlib zlib-devel make libtool
4. Install openssl
Secure Sockets Layer cryptographic library for communication encryption
yum -y install openssl openssl-devel
4.2.2 nginx installation
Manually create users and user groups
groupadd nginx useradd nginx -g nginx -s /sbin/nologin -M
Official website download nginx source package
wget https://nginx.org/download/nginx-1.20.2.tar.gz
decompress
tar -zxvf nginx-1.20.2.tar.gz
build directory
cd nginx-1.20.2 # Check the platform installation environment ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --user=nginx --group=nginx
Parameter Description
#Compile and install directory –prefix=/usr/local/nginx #owning user nginx –user=nginx #group nginx –group=nginx #This module provides basic status information of nginx –with-http_stub_status_module # HTTPS support –with-http_ssl_module
Compile the source code and install
make # compile make install # Install
After nginx is compiled and installed, modify nginx.conf
user nginx nginx;
4.2.3 ningx operation
start the service
/usr/local/nginx/sbin/nginx
reload service
/usr/local/nginx/sbin/nginx -s reload
Out of service
/usr/local/nginx/sbin/nginx -s stop
View progress
ps -ef | grep nginx
verify
curl IP
View nginx version information
/usr/local/nginx/sbin/nginx -V
nginx configuration service
At present, nginx is executed through commands. During the running process of nginx, nginx needs to be run as a system service. Run the service with the systemctl command
configuration service file
vi /lib/systemd/system/nginx.service
configuration information
[Unit] Description=nginx After=network.target [Service] Type=forking ExecStart=/usr/local/nginx/sbin/nginx ExecReload=/usr/local/nginx/sbin/nginx reload ExecStop=/usr/local/nginx/sbin/nginx quit PrivateTmp=true [Install] WantedBy=multi-user.target
Create service
systemctl enable nginx.service
Use the service command
systemctl status nginx
4.3 rpm installation package to install nginx
4.3.1 Download rpm installation package
Official website download Installation package
wget https://nginx.org/packages/centos/7/x86_64/RPMS/nginx-1.20.2-1.el7.ngx.x86_64.rpm
4.3.2 Install nginx package
rpm -ivh nginx-1.20.2-1.el7.ngx.x86_64.rpm
4.3.3 Configure permissions
Create nginx users and groups with root account
groupadd nginx useradd nginx -g nginx -s /sbin/nologin -M
list of authorization lists for nginx
chown -R nginx:nginx /etc/log/logrotate.d/nginx chown -R nginx:nginx /etc/nginx chown -R nginx:nginx /etc/nginx/conf.d chown -R nginx:nginx /etc/config.d/default.conf chown -R nginx:nginx /etc/nginx/fastcgi_params chown -R nginx:nginx /etc/nginx/mime.types chown -R nginx:nginx /etc/nginx/modules chown -R nginx:nginx /etc/nginx/nginx.conf chown -R nginx:nginx /etc/nginx/scgi_params chown -R nginx:nginx /etc/nginx/uwsgi_params chown -R nginx:nginx /usr/lib/systemd/system/nginx-debug.service chown -R nginx:nginx /usr/lib/systemd/system/nginx.service chown -R nginx:nginx /usr/lib64/nginx chown -R nginx:nginx /usr/lib64/nginx/modules chown -R nginx:nginx /usr/libexec/initscripts/legacy-actions/nginx chown -R nginx:nginx /usr/libexec/initscripts/legacy-actions/nginx/check-reload chown -R nginx:nginx /usr/libexec/initscripts/legacy-actions/nginx/upgrade chown -R nginx:nginx /usr/sbin/nginx chown -R nginx:nginx /usr/sbin/nginx-debug chown -R nginx:nginx /usr/share/doc/nginx-1.20.1 chown -R nginx:nginx /usr/share/doc/nginx-1.20.1/COPYRIGHT chown -R nginx:nginx /usr/share/man/man8/nginx.8.gz chown -R nginx:nginx /usr/share/nginx chown -R nginx:nginx /usr/share/nginx/html chown -R nginx:nginx /usr/share/nginx/html/50x.html chown -R nginx:nginx /usr/share/nginx/html/index.html chown -R nginx:nginx /var/cache/nginx chown -R nginx:nginx /var/log/nginx
start nginx
systemctl start nginix
verify
curl 10.6.6.161
uninstall nginx
# View the nginx installation file rpm -qa | grep nginx nginx-1.20.2-1.el7.ngx.x86_64 # Uninstall nginx installation files rpm -e nginx-1.20.2-1.el7.ngx.x86_64
4.4 Install nginx in docker
Docker installation can choose online and offline installation. Online installation is installed using docker command. After offline installation is installed using docker, it is packaged to offline server for installation. This article takes online installation as an example to expand the operation. Docker installation will not be introduced here, docker installation reference " centos7 sets up Alibaba Cloud yum source, docker source and docker image acceleration "The docker installation section in
# Get nginx image docker pull nginx:1.21.6 # View mirror docker images run nginx docker run --name nginx -p 80:80 -d nginx
During use, configuration files, sites, and log information will be mounted, and the mounting parameters can be added on the existing basis.
verify
curl ip
5, nginx other
5.1 Common operations of nginx
Common operations are based on the correct installation of nginx
start nginx
systemctl start nginx
stop nginx
systemctl stop nginx
restart nginx
systemctl restart nginx
reload configuration
systemctl reload nginx
set startup
systemctl enable nginx
Turn off startup settings
systemctl disable nginx
View version
nginx -V
5.2 Contents Description
Table of contents | illustrate |
---|---|
/etc/nginx/ | All relevant configuration file directories |
/etc/nginx/nginx.conf | /etc/nginx/nginx.conf |
/etc/nginx/nginx.conf | Independent nginx service configuration file directory |
/var/log/nginx/ | nginx log file directory |
/var/log/nginx/access.log | Access logs (IP/browser information/processing time/request URL) |
/var/log/nginx/error.log | Error log (error messages in server and request processing) |
/usr/share/nginx/html | The default site location, which can be adjusted according to the actual situation |