超级详细指南:在CentOS 7上设置LNMP环境 - 编译和YUM安装方法
nginx
1.1 查看本地有没有安装过nginx
rpm -qa | grep nginx
如果安装过,则执行如下命令,卸载原有的nginx
rpm -e nginx-*
1.2 安装 nginx 需要先将官网下载的源码进行编译,编译依赖 gcc 环境,如果没有 gcc 环境,则需要安装
yum install gcc-c++
1.3 PCRE(Perl Compatible Regular Expressions) 是一个Perl库,包括 perl 兼容的正则表达式库。nginx 的 http 模块使用 pcre 来解析正则表达式,所以需要在 linux 上安装 pcre 库,pcre-devel 是使用 pcre 开发的一个二次开发库。nginx也需要此库。命令:
yum install -y pcre pcre-devel
1.4 zlib 库提供了很多种压缩和解压缩的方式, nginx 使用 zlib 对 http 包的内容进行 gzip ,所以需要在 Centos 上安装 zlib 库。
yum install -y zlib zlib-devel
1.5 OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及 SSL 协议,并提供丰富的应用程序供测试或其它目的使用。
nginx 不仅支持 http 协议,还支持 https(即在ssl协议上传输http),所以需要在 Centos 安装 OpenSSL 库。
yum install -y openssl openssl-devel
1.6 官网下载 tar.gz安装包,地址~
Mainline version:主线版本
Stable version:稳定版本
Legacy versions:旧版本
# 我们lnmp环境全部安装在这个目录下
cd /usr/local
# 下载
wget -c https://nginx.org/download/nginx-1.20.0.tar.gz
# 解压
tar -zxvf nginx-1.20.0.tar.gz
cd nginx-1.20.0
# 编译
./configure --prefix=/usr/local/nginx
# 安装
make && make install
# make[1]: 离开目录“/usr/local/nginx-1.20.0” 最后显示这个是编译安装成功的意思!
启动、停止nginx
cd /usr/local/nginx/sbin/
./nginx
./nginx -s stop
./nginx -s quit
./nginx -s reload
# 查看ngin进程
ps aux | grep nginx
配置nginx的service服务
首先
先停掉nginx服务
killall -9 nginx
或者使用 cd /usr/local/nginx/sbin/ && ./nginx -s stop
然后编辑nginx.service配置文件
vim /etc/init.d/nginx
在里面写入以下配置:
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig: - 85 15
# description: NGINX is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /etc/nginx/nginx.conf
# config: /etc/sysconfig/nginx
# pidfile: /var/run/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
# 配置nginx命令的位置
# 修改为你的nginx可执行命令的路径如: /usr/local/nginx/sbin/nginx
nginx="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)
# 指向你的配置文件路径,如:/usr/local/nginx/conf/nginx.conf
NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx
make_dirs() {
# make required directories
user=`$nginx -V 2>&1 | grep "configure arguments:.*--user=" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
if [ -n "$user" ]; then
if [ -z "`grep $user /etc/passwd`" ]; then
useradd -M -s /bin/nologin $user
fi
options=`$nginx -V 2>&1 | grep 'configure arguments:'`
for opt in $options; do
if [ `echo $opt | grep '.*-temp-path'` ]; then
value=`echo $opt | cut -d "=" -f 2`
if [ ! -d "$value" ]; then
# echo "creating" $value
mkdir -p $value && chown -R $user $value
fi
fi
done
fi
}
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
make_dirs
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
configtest || return $?
stop
sleep 1
start
}
reload() {
configtest || return $?
echo -n $"Reloading $prog: "
killproc $prog -HUP
retval=$?
echo
}
force_reload() {
restart
}
configtest() {
$nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac
然后执行
# 重新加载某个服务的配置文件
systemctl daemon-reload
然后查看nginx状态
service nginx status
我这里报错了!! 提示错误信息Can’t open PID file /var/run/nginx.pid (yet?) after start:,原因是nginx主配置文件nginx.conf中 pid指令配置的pid路径 与 nginx systemd service文件中的配置PIDFile路径不一致导致的。
解决办法:
-
我用此方法并没有解决!将/etc/init.d/nginx(对应你的nginx service配置文件) 的pidfile 注释去掉,修改内容为logs/nginx.pid
或者将你nginx主配置文件nginx.conf的pid注释去掉并修改内容为你的nginx service配置文件的pidfile
总之两者pid路径一直即可解决。 -
用此方法解决了使用nginx -c的参数指定nginx.conf文件的位置。执行如下命令。
1. /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
2. systemctl daemon-reload
3. service nginx status 就没错了
如果想虚拟机外访问nginx记得关闭防火墙。centos有多种防火墙。
systemctl status firewalld.service — 查看防火墙状态
systemctl stop firewalld.service — 关闭防火墙
systemctl disable firewalld.service — 设置开机禁用防火墙
iptables防火墙的关闭!!!
servcie iptables stop --临时关闭防火墙
chkconfig iptables off --永久关闭防火墙
service iptables status --查看防火墙状态
最后一步:将nginx添加到环境变量
vim /etc/profile
在最后面写入如下代码
PATH=$PATH:/usr/local/nginx/sbin
export PATH
最后执行 source /etc/profile
至此,编译安装nginx完成!