在Ubuntu 18.04 LTS服务器上安装并编译nginx1.14
时间:2019-08-20 17:58:20 来源:igfitidea点击:
当我们通过从源代码包编译来安装nginx时,它给了我们一个主要的好处,那就是在安装之后可以添加额外的模块。
以root/超级用户身份登录
sudo su
创建Nginx系统用户
sudo useradd -s /sbin/nologin nginx
安装依赖包
安装所有依赖项。列出这些所有的包是因为我们将添加相关模块。
sudo apt update sudo apt install gcc libpcre3-dev zlib1g-dev libssl-dev libxml2-dev libxslt1-dev libgd-dev google-perftools libgoogle-perftools-dev libperl-dev
下载并安装nginx1.14
在本节中,我们将阅读如何安装和编译nginx1.14.
每一个新的nginx版本都会引入一些更改,因此它也可能会反映在编译期间将添加的模块列表上。
下载Nginx源代码包
curl -O http://nginx.org/download/nginx-1.14.0.tar.gz
解压下载的Nginx源码包
sudo tar -xvzf nginx-1.14.0.tar.gz
从Nginx源文件编译/安装
cd nginx-1.14.0
开始编译。
sudo ./configure --user=nginx --group=nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --with-select_module --with-poll_module --with-threads --with-file-aio --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module --with-http_xslt_module=dynamic --with-http_image_filter_module --with-http_image_filter_module=dynamic --with-http_geoip_module --with-http_geoip_module=dynamic --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_slice_module --with-http_stub_status_module --with-http_perl_module --with-http_perl_module=dynamic --with-mail --with-mail=dynamic --with-mail_ssl_module --with-stream --with-stream=dynamic --with-stream_ssl_module --with-stream_realip_module --with-stream_geoip_module --with-stream_geoip_module=dynamic --with-stream_ssl_preread_module --with-google_perftools_module --with-cpp_test_module --with-compat --with-pcre --with-pcre-jit --with-zlib-asm=CPU --with-libatomic --with-debug --with-ld-opt="-Wl,-E"
执行make并安装。
sudo make && sudo make install
为nginx创建systemd服务脚本文件
要启动、停止、重新启动和检查nginx服务的状态,可以创建systemd服务脚本。
创建一个文件
sudo vi /lib/systemd/system/nginx.service
添加如下内容:
[Unit] Description=The Nginx 1.14 service After=syslog.target network.target remote-fs.target nss-lookup.target [Service] Type=forking PIDFile=/usr/local/nginx/logs/nginx.pid ExecStartPre=/usr/sbin/nginx -t ExecStart=/usr/sbin/nginx ExecReload=/usr/sbin/nginx -s reload ExecStop=/bin/kill -s QUIT $MAINPID PrivateTmp=true [Install] WantedBy=multi-user.target
创建nginx服务文件后,应该重新加载systemd管理器配置。
sudo systemctl daemon-reload
Ubuntu中启动nginx服务
sudo systemctl start nginx.service
Ubuntu中停止nginx服务
sudo systemctl stop nginx.service
Ubuntu中重启nginx服务
sudo systemctl restart nginx.service
Ubuntu中查看nginx服务状态
sudo systemctl status nginx.service
Ubuntu中使nginx服务能够在引导时运行
sudo systemctl enable nginx.service
设置nginx服务在引导时不运行
sudo systemctl disable nginx.service
检查并确认Nginx 默认端口和网页
检查默认监听端口(端口号80或者http)
运行下面的命令来检查nginx默认监听端口。
root@theitroad:~# ss -tanlp|grep 80 LISTEN 0 128 0.0.0.0:80 0.0.0.0:* users:(("nginx",pid=1511,fd=6),("nginx",pid=1507,fd=6)) root@theitroad:~#
检查默认Nginx网页
在浏览器打开http://服务器ip
为什么在编译nginx源代码之前安装依赖包
我们之所以安装了特定的依赖软件包是有原因的。所以我们在nginx编译过程中共享依赖包名及其相关错误的信息。
如果某个包没有安装,编译过程中就会报错,具体如下所示:
## Package to install: gcc ./configure: error: C compiler cc is not found ## Package to install: lipcre3-dev ./configure: error: the HTTP rewrite module requires the PCRE library. You can either disable the module by using --without-http_rewrite_module option, or install the PCRE library into the system, or build the PCRE library statically from the source with nginx by using --with-pcre=<path> option. ## Package to install: zlib1g-dev ./configure: error: the HTTP gzip module requires the zlib library. You can either disable the module by using --without-http_gzip_module option, or install the zlib library into the system, or build the zlib library statically from the source with nginx by using --with-zlib=<path> option. ## Package to install: libxml2-dev, libxslt1-dev ./configure: error: the HTTP XSLT module requires the libxml2/libxslt libraries. You can either do not enable the module or install the libraries ## Package to install: libgd-dev ./configure: error: the HTTP image filter module requires the GD library. You can either do not enable the module or install the libraries. ## Package to install: libgeoip1 libgeoip-dev ./configure: error: the GeoIP module requires the GeoIP library. You can either do not enable the module or install the library. ## Package to install: google-perftools, libgoogle-perftools-dev ./configure: error: the Google perftools module requires the Google perftools library. You can either do not enable the module or install the library. ## Package to install: libatomic-ops-dev ./configure: error: libatomic_ops library was not found. ## Package to install: libperl-dev /usr/bin/x86_64-linux-gnu-ld: cannot find -lperl collect2: error: ld returned 1 exit status objs/Makefile:1607: recipe for target 'objs/ngx_http_perl_module.so' failed make[1]: *** [objs/ngx_http_perl_module.so] Error 1 make[1]: Leaving directory '/root/nginx-1.14.0' Makefile:8: recipe for target 'build' failed make: *** [build] Error 2