先下载ngnix源文件,apt安装必要的编译工具。
$ apt install -y build-essential libpcre3 libpcre3-dev openssl zlib1g-dev libssl-dev
# 下载源码
$ wget http://nginx.org/download/nginx-1.20.2.tar.gz
# 解压并准备编译
$ tar -xf nginx-1.20.2.tar.gz
$ cd nginx-1.20.2
#--prefix:Nginx主要安装路径,后续Nginx子目录依照这个变量展开
#--user:设置Nginx进程启动时,所属的用户
$ ./configure \
--prefix=/usr/local/nginx \
--user=root \
--sbin-path=/usr/local/nginx/sbin/nginx \
--conf-path=/usr/local/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx.pid \
--lock-path=/var/run/nginx.lock \
--http-client-body-temp-path=/var/cache/nginx/client_temp \
--http-proxy-temp-path=/var/cache/nginx/proxy_temp \
--http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \
--http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \
--http-scgi-temp-path=/var/cache/nginx/scgi_temp \
--with-file-aio \
--with-threads \
--with-http_addition_module \
--with-http_auth_request_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_mp4_module \
--with-http_random_index_module \
--with-http_realip_module \
--with-http_secure_link_module \
--with-http_slice_module \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-http_sub_module \
--with-http_v2_module \
--with-mail \
--with-mail_ssl_module \
--with-stream \
--with-stream_realip_module \
--with-stream_ssl_module \
--with-stream_ssl_preread_module
输入配置命令后,如果按如下提示说明无误,可以进行编译
Configuration summary
+ using threads
+ using system PCRE library
+ using system OpenSSL library
+ using system zlib library
nginx path prefix: "/usr/local/nginx"
nginx binary file: "/usr/local/nginx/sbin/nginx"
nginx modules path: "/usr/local/nginx/modules"
nginx configuration prefix: "/usr/local/nginx"
nginx configuration file: "/usr/local/nginx/nginx.conf"
nginx pid file: "/var/run/nginx.pid"
nginx error log file: "/var/log/nginx/error.log"
nginx http access log file: "/var/log/nginx/access.log"
nginx http client request body temporary files: "/var/cache/nginx/client_temp"
nginx http proxy temporary files: "/var/cache/nginx/proxy_temp"
nginx http fastcgi temporary files: "/var/cache/nginx/fastcgi_temp"
nginx http uwsgi temporary files: "/var/cache/nginx/uwsgi_temp"
nginx http scgi temporary files: "/var/cache/nginx/scgi_temp"
编译顺利直接安装,然后创建systemctl守护服务
$ make install
$ vi /usr/lib/systemd/system/nginx.service
按如下内容填写
[Unit]
Description=nginx
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 quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
这样一来,那么ngnix服务就已经安装好了,需要注意如下信息
/usr/local/nginx:为Nginx编译安装的地址。
/usr/local/nginx/nginx.conf:Nginx默认配置文件。
使用systemctl对Nginx进行管理:
启动Nginx服务:systemctl start nginx
。
重载Nginx配置:systemctl reload nginx
。
停止Nginx服务:systemctl stop nginx
。