安装过程
http://nginx.org/download/nginx-1.20.1.tar.gz 下载nginx包
tar -xzvf nginx-1.20.1.tar.gz
cd nginx-1.20.1.tar.gz
yum install pcre pcre-devel
yum -y install openssl openssl-devel
yum -y install GeoIP GeoIP-devel GeoIP-data
编译安装
./configure --prefix=/usr/local/nginx \
--with-file-aio \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_sub_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gzip_static_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_stub_status_module \
--with-mail \
--with-mail_ssl_module \
--with-http_perl_module \
--with-debug \
--with-http_xslt_module \
--with-http_image_filter_module \
--with-http_degradation_module \
--with-ld-opt=-Wl,-E
make
make install
make的过程是把各种语言写的源码文件,变成可执行文件和各种库文件;
make install是把这些编译出来的可执行文件和库文件复制到合适的地方。
可能出现错误
在配置信息./configure 时,出现错误:
/configure: error: the HTTP rewrite module requires the PCRE library.
安装pcre
yum install pcre pcre-devel
yum -y install openssl openssl-devel
yum install -y zlib-devel
yum -y install libxml2 libxml2-dev
yum -y install libxslt-devel
yum -y install gd-devel
yum -y install perl-devel perl-ExtUtils-Embed
yum -y install GeoIP GeoIP-devel GeoIP-data
缺少ssl错误,错误信息如下:
./configure: error: the HTTP cache module requires md5 functions
from OpenSSL library. You can either disable the module by using
--without-http-cache option, or install the OpenSSL library into the system,
or build the OpenSSL library statically from the source with nginx by using
--with-http_ssl_module --with-openssl=<path> options.
安装openssl
yum -y install openssl openssl-devel
缺少zlib包,错误信息如下:
./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.
安装zlib
yum install -y zlib-devel
缺少libxml2,错误信息如下:
./configure: error: the HTTP XSLT module requires the libxml2/libxslt
libraries. You can either do not enable the module or install the libraries.
安装:
yum -y install libxml2 libxml2-dev
yum -y install libxslt-devel
http_image_filter_module是nginx提供的集成图片处理模块,需要gd-devel的支持,错误信息如下:
./configure: error: the HTTP image filter module requires the GD library.
You can either do not enable the module or install the libraries.
yum -y install gd-devel
缺少ExtUtils,错误信息如下:
./configure: error: perl module ExtUtils::Embed is required
yum -y install perl-devel perl-ExtUtils-Embed
缺少GeoIP,错误信息如下:
./configure: error: the GeoIP module requires the GeoIP library.
You can either do not enable the module or install the library.
yum -y install GeoIP GeoIP-devel GeoIP-data
启动
确保系统的 80 端口没被其他程序占用,运行/usr/local/nginx/nginx 命令来启动 Nginx,
netstat -ano|grep 80
如果查不到结果后执行,有结果则忽略此步骤(ubuntu下必须用sudo启动,不然只能在前台运行)
/usr/local/nginx/nginx
浏览器访问此机器的 IP,如果浏览器出现 Welcome to nginx! 则表示 Nginx 已经安装并运行成功。
设置nginx开机自启动(centos7.x)
vim /lib/systemd/system/nginx.service
[Unit] Description=nginx service 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
[Unit]:服务的说明
Description:描述服务
After:描述服务类别
[Service]服务运行参数的设置
Type=forking是后台运行的形式
ExecStart为服务的具体运行命令
ExecReload为重启命令
ExecStop为停止命令
PrivateTmp=True表示给服务分配独立的临时空间
注意:[Service]的启动、重启、停止命令全部要求使用绝对路径
[Install]运行级别下服务安装的相关设置,可设置为多用户,即系统运行级别为3
命令:
# systemctl start nginx.service 启动nginx服务 # systemctl stop nginx.service 停止服务 # systemctl restart nginx.service 重新启动服务 # systemctl list-units --type=service 查看所有已启动的服务 # systemctl status nginx.service 查看服务当前状态 # systemctl enable nginx.service 设置开机自启动 # systemctl disable nginx.service 停止开机自启动
错误解决
Warning: nginx.service changed on disk. Run 'systemctl daemon-reload' to reload units.
直接按照提示执行命令systemctl daemon-reload 即可。
# systemctl daemon-reload