「这是我参与2022首次更文挑战的第22天,活动详情查看:2022首次更文挑战」。
作者:Mintimate
Mintimate's Blog,只为与你分享
Nginx
Nginx
是一款轻量级的 HTTP 服务器,时常用于服务端的反向代理和负载均衡。
手动编译安装Nginx比较复杂,但是平时一般使用最多。原因:
- 便于管理 编译安装的Nginx,其安装地址可控,如果需要卸载,执行反编译即可。
- 模块可控 Nginx有其丰富的模块库,如:ngx-fancyindex。使用Docker或软件包管理器安装的Nginx,模块有时不方便载入。
下次给大家分享,怎么安装模块~~~
环境准备
本次安装Nginx,是在Debian发行版本的Linux上安装,如果是CentOS发行版本Linux,需要注意:
- 编译安装时,需要自行安装:
gcc
、pcre
、zlib
以及openssl
另外,如果你觉得本文的安装方法过于技术型。其实,也可以试试宝塔面板的一键操作。
或者看看上篇文章:Debian系统如何使用软件包管理器或Docker安装Nginx
本次教程使用一台Debian10 x64服务器:
安装gcc编译器
首先,我们需要安装gcc编译器用于make
编译,Debian可以通过安装build-essential
来安装GCC编译器:
apt install -y build-essential
安装正则库
正则库很关键,我们使用Nginx,在配置文件内location
进行目录匹配,就需要正则库。Debian安装正则库,可以:
apt install -y libpcre3 libpcre3-dev
安装zlib库
当然,Nginx编译过程和Http相应过程还需要gzip
格式的压缩,所以我们还需要安装zlib库
用于对HTTP包的内容做gzip格式的压缩,可以这样安装:
apt install -y zlib1g-dev
安装OpenSSL库
最后,现在SSL协议很重要,Chrome等主流浏览器,都开始默认相应HTTPS了,所以OpenSSL编译环境也很重要:
apt install -y openssl libssl-dev
依赖都安装完成,就可以下载源码来编译了。
下载Nginx源码
接下来,我们下载Nginx源码,我们进入Nginx官网:nginx.org/en/download…
下载最新的stable稳定版本:
在Debian上使用wget下载:
# 下载源码
wget http://nginx.org/download/nginx-1.20.2.tar.gz
# 解压源码
tar -xf nginx-1.20.2.tar.gz
# 进入源代码内
cd cd nginx-1.20.2
配置和编译
接下来就是make
环节了,编译时候的参数可以参考官方Nginx文档:nginx.org/en/docs/con…
我自己编译Nginx时候,选择的参数一般是:
./configure \
--prefix=/usr/local/nginx \
--user=www \
--group=www \
--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
其中:
--prefix
:Nginx主要安装路径,后续Nginx子目录依照这个变量展开--user
:设置Nginx进程启动时,所属的用户--group
:设置Nginx进程启动时,所属的用户组
如果没有问题,会提示信息:
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"
没有报错信息就可以编译了:
make
接下来就是安装了。
安装
首先是安装,很简单:
make install
我们再创建systemctl
守护,管理Nginx:
vim /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
具体使用
如果你是按我的方法编译,那么,需要注意。
/usr/local/nginx
:为Nginx编译安装的地址。/usr/local/nginx/nginx.conf
:Nginx默认配置文件。
同时,我们使用systemctl
对Nginx进行管理:
systemctl start nginx
:启动Nginx服务。systemctl reload nginx
:Nginx配置重载。systemctl stop nginx
:停止Nginx服务。
更多systemctl操作,可以看这篇教程:Linux系统服务神器:systemctl的配置与使用。
最后,我们写个HelloWorld
。
编辑配置文件:
指向目录/www
:
cd /
mkdir /www
cd www
vim index.html
重载Nginx配置:
systemctl reload nginx
浏览器访问成功:
卸载
最后,如何卸载Nginx呢?其实更简单:
# 停止Nginx服务
systemctl stop nginx
# 删除Nginx服务
rm -rf /usr/lib/systemd/system/nginx.service
# 重载配置
systemctl daemon-reload
# 删除Nginx编译文件
rm -rf nginx
这样就卸载完成了。
END
其实呢?个人是喜欢编译安装Nginx。
Nginx确实是个Web服务器神器呢~~~