centos 7 安装 nginx-1.11.10(腾讯云)

在centos 7 下安装 nginx-1.11 前需要先切换到root环境,通过命令 su root 切换,然后再输入密码, 如果不能切换需要把下载的nginx文件夹给予777的权限

bash
#su root 密码输入

下载nginx-1.11.10的压缩包文件到根目录,官网下载地址:nginx.org/download/nginx-1.11.10.tar.gz

#yum update
#wget nginx.org/download/nginx-1.11.10.tar.gz

解压tar.gz压缩包文件,进去nginx-1.11.10

#tar -xzvf nginx-1.11.10.tar.gz 
#cd nginx-1.11.10

进去后如果发现有configure这个文件,说明这个源码包安装前需要先进行配置,主要是为了检查当前的环境是否满足要安装软件的依赖关系,如果没有这个文件说明是二进制包,解压后直接使用不用configure

#./configure

通过安装前的配置检查,发现有报错  检查中发现一些依赖库没有找到,这时候需要先安装nginx的一些依赖库

#yum -y install pcre* 
#yum -y install gcc-c++        
#yum -y install zlib*
#yum -y install openssl

再次进行检查操作 ./configure 没发现报错显示,接下来进行编译并安装的操作

#make && make install

查看nginx安装后在的目录,可以看到已经安装到 /usr/local/nginx 目录了

#whereis nginx
$nginx: /usr/local/nginx

启动nginx服务

#cd /usr/local/nginx/sbin/
#./nginx

nginx默认使用80端口,若启动报端口错误,则kill80端口的进程,或者nginx配置使用其他端口

通过ping localhost 或在浏览器直接访问,ping成功或出现下面页面则安装成功。

命令:curl localhost/index.html 如果返回html代码也代表安装成功。

[root@VM_0_14_centos sbin]# curl localhost/index.html

安装好后,当系统重启时每次都要去手动启动nginx服务,所以需要把nginx加入开机自启动,参考nginx设置开机启动:http://zixuephp.net/article-206.html

创建开机启动命令脚本文件:

#vi /etc/init.d/nginx
在这个nginx文件中插入一下启动脚本代码,启动脚本代码来源网络复制
#! /bin/bash
# chkconfig: - 85 15
PATH=/usr/local/nginx
DESC="nginx daemon"
NAME=nginx
DAEMON=$PATH/sbin/$NAME
CONFIGFILE=$PATH/conf/$NAME.conf
PIDFILE=$PATH/logs/$NAME.pid
scriptNAME=/etc/init.d/$NAME
set -e
[ -x "$DAEMON" ] || exit 0
do_start() {
$DAEMON -c $CONFIGFILE || echo -n "nginx already running"
}
do_stop() {
$DAEMON -s stop || echo -n "nginx not running"
}
do_reload() {
$DAEMON -s reload || echo -n "nginx can't reload"
}
case "$1" in
start)
echo -n "Starting $DESC: $NAME"
do_start
echo "."
;;
stop)
echo -n "Stopping $DESC: $NAME"
do_stop
echo "."
;;
reload|graceful)
echo -n "Reloading $DESC configuration..."
do_reload
echo "."
;;
restart)
echo -n "Restarting $DESC: $NAME"
do_stop
do_start
echo "."
;;
*)
echo "Usage: $scriptNAME {start|stop|reload|restart}" >&2
exit 3
;;
esac
exit 0

设置所有人都有对这个启动脚本nginx文件的执行权限

#chmod a+x /etc/init.d/nginx

把nginx加入系统服务中

#chkconfig --add nginx

把服务设置为开机启动

#chkconfig nginx on

reboot重启系统生效

#启动nginx服务
#systemctl start nginx.service
#停止nginx服务
#systemctl stop nginx.service
#重启nginx服务
 
#同时也可以通过下面的命令进行服务重启,停止操作
#service nginx restart
#service nginx start
#service nginx stop

如果服务启动的时候出现 Restarting nginx daemon: nginxnginx: [error] open() "/usr/local/nginx/logs/nginx.pid" failed (2: No such file or directory)
nginx not running 的错误,通过nginx -c 参数指定配置文件即可解决

#/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
如果服务启动中出现 nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) 的错误,可以先通过service nginx stop 停止服务,再启动就好。

猜你喜欢

转载自www.cnblogs.com/jicp/p/8973543.html