新手安装nginx步骤及常见的问题解决方案

安装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)

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值