CentOS7安装nginx及踩坑

版权声明:非诚勿扰 https://blog.csdn.net/kaikai0803/article/details/84789392

一、安装准备

首先由于nginx的一些模块依赖一些lib库,所以在安装nginx之前,必须先安装这些lib库,这些依赖库主要有g++、gcc、openssl-devel、pcre-devel和zlib-devel 所以执行如下命令安装

$   yum install gcc-c++  
$   yum install pcre pcre-devel  
$   yum install zlib zlib-devel  
$   yum install openssl openssl--devel  

或者

$ yum install -y gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel

二、安装Nginx

首先进入/usr/local目录

$   cd /usr/local  

从官网下载最新版的nginx

$   wget http://nginx.org/download/nginx-1.11.4.tar.gz  

wget命令找不到的话

yum -y install wget

解压nginx压缩包

$   tar -zxvf nginx-1.7.4.tar.gz  

会产生一个nginx-1.11.4 目录,这时进入nginx-1.11.4目录

$   cd  nginx-1.11.4  

接下来安装,使用–prefix参数指定nginx安装的目录,make、make install安装

$   ./configure
$   make  &&  make install      

没有报错,则顺利安装完成

常用命令

cd /usr/local/nginx/sbin/		# 进入nginx可执行文件所在目录
./nginx 						# 启动
./nginx -s stop				# 停止
./nginx -s quit					# 退出
./nginx -s reload				# 重启

[root@localhost etc]# nginx -h
nginx version: nginx/1.11.4
Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]

Options:
  -?,-h         : this help
  -v            : show version and exit
  -V            : show version and configure options then exit
  -t            : test configuration and exit
  -T            : test configuration, dump it and exit
  -q            : suppress non-error messages during configuration testing
  -s signal     : send signal to a master process: stop, quit, reopen, reload
  -p prefix     : set prefix path (default: /usr/local/nginx/)
  -c filename   : set configuration file (default: conf/nginx.conf)
  -g directives : set global directives out of configuration file

开机自启

创建文件

vi /etc/rc.local

写入

/usr/local/nginx/sbin/nginx

更改文件权限

chmod 755 /etc/rc.local

访问不到

关闭防火墙

systemctl stop firewalld.service

或者开放端口,重启防火墙

firewall-cmd --zone=public --add-port=80/tcp --permanent
systemctl restart firewalld.service

关闭防火墙开机自启

systemctl disable firewalld.service #禁止firewall开机启动
firewall-cmd --state #查看默认防火墙状态(关闭后显示not running,开启后显示running)

nginx添加环境变量

方法一:
直接运行命令

export PATH=$PATH:/usr/local/nginx/sbin

使用这种方法,只会对当前会话有效,也就是说每当登出或注销系统以后,PATH 设置就会失效,只是临时生效。

方法二:
执行vi ~/.bash_profile
修改文件中PATH一行,将/usr/local/nginx/sbin加入到PATH=$PATH:$HOME/bin一行之后
这种方法只对当前登录用户生效

方法三:
修改/etc/profile文件使其永久性生效,并对所有系统用户生效,在文件末尾加上如下两行代码

PATH=$PATH:/usr/local/nginx/sbin
export PATH

最后:执行 命令source /etc/profile使其修改生效,执行完可通过echo $PATH命令查看是否添加成功。

猜你喜欢

转载自blog.csdn.net/kaikai0803/article/details/84789392
今日推荐