安装nginx方法1:
yum源安装。
首先得在官网www.nginx.org里的download里找到想要下载的版本并复制好链接,然后在linux系统里进入yum源里编辑nginx下载地址具体操作如下: cd /etc/yum.repos.d/ vim nginx.repo
写入
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enbled=1
保存退出。
然后yum -y install nginx
yum -y install pcre pcre-devel
现已安装好nginx,顺便写个自动检测nginx是否正常工作的脚本 可写入自动任务。
#!/bin/bash
alive=`netstat -pant |awk '/0.0.0.0:80/&&/LISTEN/'|wc -l`
if[ $alive -eq 1];then
exit 0
else
exit 1
fi
方法二,源码编译安装:
安装依赖包
# yum -y install gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel
gcc-c++ nginx的编译依赖gcc环境,如果没有gcc环境,则需要安装gcc-c++。
pcre pcre-devel pcre是一个perl库,包括perl兼容的正则表达式库。nginx使用pcre来解析正则表达式。
zlib zlib-devel zlib库提供了很多种压缩和解压缩的方式,nginx 使用 zlib 对 http 包的内容进行 gzip。
openssl openssl-devel nginx支持https协议,需要openssl。
在官网找到要下载的版本链接, 比如: wget http://nginx.org/download/nginx-1.14.2.tar.gz
解压缩: tar -zxf nginx-1.14.2.tar.gz
#cd nginx-1.14.2
#./configure --prefix=/usr/local/nginx
-prefix指定了nginx的安装目录是/usr/local/nginx
【此时如何报错 Error: Multilib version problems found. This often means that the root cause is something else and multilib version checking is just pointing out that there is a problem
可运行:
yum -y install pcre-devel
yum -y install openssl openssl-devel】
#make
#make install
启动nginx
#cd /usr/local/nginx/sbin/
#./nginx
【如果在使用systemctl start nginx 或service nginx start 系统命令时报错:
systemctl start nginx Failed to start nginx.service: Unit not found
解决方法: 在cd /etc/init.d/里建一个脚本进程 vim nginx 写入代码如下:
#!/bin/bash
#
# chkconfig: - 85 15
# description: Nginx is a World Wide Web server.
# processname: nginx
nginx=/usr/local/nginx/sbin/nginx
conf=/usr/local/nginx/conf/nginx.conf
case $1 in
start)
echo -n "Starting Nginx"
$nginx -c $conf
echo " done"
;;
stop)
echo -n "Stopping Nginx"
killall -9 nginx
echo " done"
;;
test)
$nginx -t -c $conf
;;
reload)
echo -n "Reloading Nginx"
ps auxww | grep nginx | grep master | awk '{print $2}' | xargs kill -HUP
echo " done"
;;
restart)
$0 stop
$0 start
;;
show)
ps -aux|grep nginx
;;
*)
echo -n "Usage: $0 {start|restart|reload|stop|test|show}"
;;
esac
然后保存退出。
设置执行权限:chmod +x /etc/init.d/nginx
注册成服务:chkconfig
-add
nginx
设置开机启动:chkconfig nginx on
将文件写入内存: source nginx
退出重新登入即可实现系统命令
systemctl start nginx
service nginx start
service nginx reload
】
防火墙允许80端口
#firewall-cmd --add-service=http
验证是否安装成功可打开浏览器,在地址栏输入http://服务器的ip地址,显示如下图:
表示安装成功。
停止、重载nginx
停止nginx
#cd /usr/local/nginx/sbin/
#./nginx -s stop
重载nginx(改了配置文件后使配置生效)
#cd /usr/local/nginx/sbin/
#./nginx -s reload
需要nginx处于运行状态下才能执行该命令,否则会报错:
nginx: [error] open() "/usr/local/nginx/logs/nginx.pid" failed (2: No such file or directory)
卸载nginx
先停止nginx,然后删除nginx的安装目录(/usr/local/nginx)