文章目录
类型
对于CentOS而言一般分为rpm安装和源码编译安装两种。
rpm
下载对应rpm包
以CentOS7
为例:
wget http://nginx.org/packages/centos/7/x86_64/RPMS/nginx-1.16.0-1.el7.ngx.x86_64.rpm
rpm导入
rpm -ivh nginx-1.16.0-1.el7.ngx.x86_64.rpm
yum安装
yum install nginx
启动、停止、重启及开机启动
systemctl start nginx
systemctl stop nginx
systemctl restart nginx
systemctl enable nginx
默认配置
主配置文件在/etc/nginx/
目录下
日志由主配置文件定义,默认在/var/log/nginx
下
可以通过whereis nginx
查看其他配置,其运行结果为
nginx: /usr/sbin/nginx /usr/lib64/nginx /etc/nginx /usr/local/nginx /usr/share/nginx /usr/share/man/man8/nginx.8.gz
其中/usr/sbin/nginx
是nginx的启动脚本。
编译
安装编译工具及库文件
yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel
安装 PCRE
PCRE 作用是让 Nginx 支持 Rewrite 功能。
-
下载 PCRE 安装包,下载地址: http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz
wget -P /usr/local/src/ http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz
-
解压安装包
指定解压目录为
/usr/local/src
tar zxvf /usr/local/src/pcre-8.35.tar.gz -C /usr/local/src/
-
编译
cd /usr/local/src/pcre-8.35 ./configure make && make install
-
测试
pcre-config --version
源码
-
下载
wget -P /usr/local/ http://nginx.org/download/nginx-1.17.2.tar.gz
-
解压
tar zxvf /usr/local/nginx-1.17.2.tar.gz -C /usr/local
-
编译
./configure \ --prefix=/usr/local/nginx \ --sbin-path=/usr/sbin/nginx \ --with-pcre=/usr/local/src/pcre-8.35 \ --conf-path=/etc/nginx/nginx.conf \ --error-log-path=/var/log/nginx/error.log \ --http-client-body-temp-path=/var/lib/nginx/body \ --http-fastcgi-temp-path=/var/lib/nginx/fastcgi \ --http-log-path=/var/log/nginx/access.log \ --http-proxy-temp-path=/var/lib/nginx/proxy \ --lock-path=/var/lock/nginx.lock \ --pid-path=/var/run/nginx.pid \ --with-debug \ --with-http_gzip_static_module \ --with-http_realip_module \ --with-http_stub_status_module \ --with-http_ssl_module \ --with-http_sub_module \ --with-mail \ --with-mail_ssl_module make && make install
-
测试
nginx -v
开机启动
参考官方文档编写启动脚本/etc/init.d/nginx
chmod a+x /etc/init.d/nginx
systemctl enable nginx
systemctl start nginx