ubuntu编译安装nginx
yum和apt方式安装就不在所说,没有最新版的nginx就去找对应的源就行。
ubuntu安装依赖包:
root@ubuntu:~# apt install libgd-dev libgeoip-dev libpcre3 libpcre3-dev libssl-dev gcc make -y
centos7下安装依赖包
[root@centos7 nginx-1.16.1]# yum install make gcc pcre-devel.x86_64 openssl-devel.x86_64 gd-devel.x86_64 GeoIP-devel.x86_64 -y
nginx官方下载地址: http://nginx.org/en/download.html
#解压nginx包
root@ubuntu:/data# tar xvf nginx-1.16.1.tar.gz
root@ubuntu:/data# cd nginx-1.16.1/
#开始编译
root@ubuntu:/data/nginx-1.16.1# ./configure --prefix=/apps/nginx \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_image_filter_module \
--with-http_geoip_module \
--with-http_gunzip_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-pcre \
--with-stream \
--with-stream_ssl_module \
--with-stream_realip_module
root@ubuntu:/data/nginx-1.16.1# make && make install
#创建nginx用户
root@ubuntu:~# useradd -r -s /sbin/nologin nginx
编译选项解释:
./configure --prefix=/apps/nginx \ #指定nginx的安装路径
--with-http_ssl_module \ #启用http的ssl模块
--with-http_v2_module \ #启用HTTP/2模块
--with-http_realip_module \ #该模块将客户端地址更改为在指定的标头字段中发送的地址,利于观察客户端地址
--with-http_addition_module \ #该模块在响应之前和之后添加文本
--with-http_image_filter_module \ #该模块可以转换JPEG,GIF,PNG和WebP格式的图像
--with-http_geoip_module \ #该模块根据客户端IP地址和预编译的MaxMind数据库创建变量。
--with-http_gunzip_module \ #http解压模块
--with-http_stub_status_module \ #该模块提供对基本状态信息的访问,
--with-http_gzip_static_module \ #压缩模块
--with-pcre \ #使用PCRE库,该库对于location指令中的正则表达式支持和ngx_http_rewrite_module模块是必需的
--with-stream \ #该模块用于tcp代理。
--with-stream_ssl_module \ #tcp的ssl支持
--with-stream_realip_module #启用构建ngx_stream_realip_module模块的功能,该模块将客户端地址更改为PROXY协议标头中发送的地址
官方编译安装的详细文档: http://nginx.org/en/docs/configure.html
编译安装成功后:
root@ubuntu:/data/nginx-1.16.1# cd /apps/nginx/
root@ubuntu:/apps/nginx# ll
total 44
drwxr-xr-x 11 root root 4096 Dec 31 11:58 ./
drwxr-xr-x 3 root root 4096 Dec 31 11:40 ../
drwx------ 2 nginx root 4096 Dec 31 11:58 client_body_temp/
drwxr-xr-x 2 root root 4096 Dec 31 12:01 conf/
drwx------ 2 nginx root 4096 Dec 31 11:58 fastcgi_temp/
drwxr-xr-x 2 root root 4096 Dec 31 11:40 html/
drwxr-xr-x 2 root root 4096 Dec 31 11:58 logs/
drwx------ 2 nginx root 4096 Dec 31 11:58 proxy_temp/
drwxr-xr-x 2 root root 4096 Dec 31 11:40 sbin/
drwx------ 2 nginx root 4096 Dec 31 11:58 scgi_temp/
drwx------ 2 nginx root 4096 Dec 31 11:58 uwsgi_temp/
#相关目录
conf:该⽬录中保存了nginx所有的配置⽂件,其中nginx.conf是nginx服务器的最核⼼最主要的配置⽂件,其他的.conf则是⽤来配置nginx相关的功能的,例如fastcgi功能使⽤的是fastcgi.conf和fastcgi_params两个⽂件,配置⽂件⼀般都有个样板配置⽂件,是⽂件名.default结尾,使⽤的使⽤将其复制为并将default去掉即可。
html:该⽬录中保存了nginx服务器的web⽂件,但是可以更改为其他⽬录保存web⽂件,另外还有⼀个50x的web⽂件是默认的错误⻚⾯提⽰⻚⾯。
logs:该⽬录⽤来保存nginx服务器的访问⽇志错误⽇志等⽇志,logs⽬录可以放在其他路径,⽐
如/var/logs/nginx⾥⾯。
sbin:该⽬录⽤来保存nginx⼆进制启动脚本,可以接受不同的参数以实现不同的功能。
验证版本及编译参数
root@ubuntu:/data/nginx-1.16.1# /apps/nginx/sbin/nginx -V
nginx version: nginx/1.16.1
built by gcc 7.5.0 (Ubuntu 7.5.0-3ubuntu1~18.04)
built with OpenSSL 1.1.1 11 Sep 2018
TLS SNI support enabled
configure arguments: --prefix=/apps/nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_image_filter_module --with-http_geoip_module --with-http_gunzip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module
#nginx启动
root@ubuntu:~# /apps/nginx/sbin/nginx -t
nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /apps/nginx/conf/nginx.conf test is successful
root@ubuntu:~# /apps/nginx/sbin/nginx
访问http://nginx服务器ip
nginx自启动脚本:
[root@s1 ~]# cat /usr/lib/systemd/system/nginx.service
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/run/nginx.pid
# Nginx will fail to start if /run/nginx.pid already exists but has the wrong
# SELinux context. This might happen when running `nginx -t` from the cmdline.
# https://bugzilla.redhat.com/show_bug.cgi?id=1268621
ExecStartPre=/usr/bin/rm -f /run/nginx.pid
ExecStartPre=/apps/nginx/sbin/nginx -t
ExecStart=/apps/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true
[Install]
WantedBy=multi-user.target