linux下安装Nginx

准备库 install -y automake autoconf libtool make gcc gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel
主要是用到了pcre pcre-devel
先装pcre, zlib,前者为了重写rewrite,后者为了gzip压缩

安装PCRE库

wget https://jaist.dl.sourceforge.net/project/pcre/pcre/8.44/pcre-8.44.tar.gz
tar -zxvf pcre-8.37.tar.gz
cd pcre-8.34
./configure --prefix=/usr/local/pcre
make && make install

安装zlib库

wget https://jaist.dl.sourceforge.net/project/libpng/zlib/1.2.11/zlib-1.2.11.tar.gz
tar zxf ***.tar.gz来解压
./configure --prefix=/usr/local/zlib
make && make install

安装ssl(某些vps默认没装ssl)

wget https://www.openssl.org/source/openssl-1.0.2n.tar.gz
cd openssl
./config --prefix=/usr/local/openssl
make && make install

安装nginx

wget http://nginx.org/download/nginx-1.14.1.tar.gz
进入nginx解压目录,配置安装环境
./configure --prefix=/usr/local/nginx --with-pcre=/usr/local/src/pcre-8.42 --with-zlib=/usr/local/src/zlib-1.2.11 --with-openssl=/usr/local/src/openssl-1.0.2n/ --with-http_ssl_module
make && make install

启动Nginx:

进入/usr/local/nginx/sbin ,执行./nginx

查看开放的端口

关注ports参数

firewall-cmd --list-all

设置开放端口

firewall-cmd --add-port=80/tcp --permanent

重启防火墙

firewall-cmd --reload

查看nginx是否启动

ps aux|grep nginx

重启:

./nginx -s reload

判断配置文件是否正确

./nginx -t

查看编译的模块

./nginx -V

nginx开机启动

  1. 第一步:进入到/lib/systemd/system/目录
[root@iz2z init.d]# cd /lib/systemd/system/

2 .第二步:创建nginx.service文件,并编辑

vim 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

Description:描述服务
After:描述服务类别
[Service]服务运行参数的设置
Type=forking是后台运行的形式
ExecStart为服务的具体运行命令
ExecReload为重启命令
ExecStop为停止命令
PrivateTmp=True表示给服务分配独立的临时空间
注意:[Service]的启动、重启、停止命令全部要求使用绝对路径
[Install]运行级别下服务安装的相关设置,可设置为多用户,即系统运行级别为3
保存退出。

  1. 第三步:加入开机自启动
# systemctl enable nginx

如果不想开机自启动了,可以使用下面的命令取消开机自启动

# systemctl disable nginx

服务的启动/停止/刷新配置文件/查看状态

# 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

解决:make[1]: *** [/usr/local/pcre//Makefile] Error 127

原因是因为pcre指定的目录不正确,pcre指定的目录是源代码的目录不是安装目录
所以执行 ./configure --prefix=/usr/local/nginx/ --with-pcre=../pcre-8.00
然后执行,make && make install

解决:checking for OS+ Linux 2.6.32-220.el6.x86_64 x86_64checking for C compiler ... not found./configure: error: C compiler gcc is not found

安装gcc
yum install gcc

./configure: error: the HTTP gzip module requires the zlib library.You can either disable the module by using --without-http_gzip_moduleoption, or install the zlib library into the system, or build the zlib librarystatically from the source with nginx by using --with-zlib= option.

yum install -y zlib-devel

原文地址:https://www.cnblogs.com/qinsilandiao/p/10903709.html