CentOS下载安装配置Nginx
1.准备工作,安装依赖包
首先安装这几个软件:GCC,PCRE(PerlCompatible Regular Expression),zlib,OpenSSL。
Nginx是C写的,需要用GCC编译;Nginx的Rewrite和HTTP模块会用到PCRE;Nginx中的Gzip用到zlib;
用命令“# gcc”,查看gcc是否安装;如果出现“gcc: no input files”信息,说明已经安装好了。否则,就需要用命令“# yum install gcc”,进行安装了!一路可能需要多次输入y,进行确认。安装好后,可以再用命令“#gcc”测试,或者用命令“# gcc -v”查看其版本号。同样方法,用如下命令安装PCRE,zlib,OpenSSL(其中devel,是develop开发包的意思):
# yum install gcc
# yum install -y pcre pcre-devel
# yum install -y zlib zlib-devel
# yum install -y openssl openssl-devel
2.下载并安装Nginx
1) 下载Nginx安装包
cd /usr/local/
wget http://Nginx.org/download/Nginx-1.10.2.tar.gz
2) 解压包
tar -zxvf Nginx-1.10.2.tar.gz
3) 进入目录
cd Nginx-1.10.2/
groupadd Nginx useradd-g Nginx Nginx -s /bin/false
5) 配置:
/configure --user=Nginx --group=Nginx--prefix=/usr/local/Nginx-1.10.2 --conf-path=/usr/local/Nginx-1.10.2/Nginx.conf--with-http_stub_status_module --with-http_ssl_module
6) 编译和安装
make&& make install
7) 检查是否安装成功
# cd /usr/local/Nginx/sbin
# ./Nginx -t
结果显示:
Nginx: the configuration file /usr/local/Nginx/conf/Nginx.conf Syntax is ok
Nginx: configuration file /usr/local/Nginx/conf/Nginx.conf test is successful
3.配置防火墙80端口
#修改防火墙配置:
#vi + /etc/sysconfig/iptables
#添加配置项
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
#重启防火墙
# serviceiptables restart
4.启动与关闭
启动:/usr/local/Nginx-1.10.2/sbin/Nginx
关闭:/usr/local/Nginx-1.10.2/sbin/Nginx–s stop
重启:/usr/local/Nginx-1.10.2/sbin/Nginx-s reload
ps -ef | grep Nginx
#停止进程
kill -QUIT 主进程号
#快速停止
kill -TERM 主进程号
#强制停止
pkill -9 Nginx
5.预览部署是否成功
#测试端口
netstat -na | grep 80
#浏览器中测试
6.设置Nginx服务开机自启动
1) 使用命令“# vi /etc/init.d/Nginx”,打开编辑器,输入如下内容:
#!/bin/bash
#chkconfig: - 9920
#description:NginxServer Contorl Script
PROG="/usr/local/Nginx-1.10.2/sbin/Nginx"
PIDF="/usr/local/Nginx-1.10.2/logs/Nginx.pid"
ok=`echo -e"\e[1;31m [ok] \e[0m"`
no=`echo -e"\e[1;31m [no] \e[0m"`
detection=`/usr/local/Nginx-1.10.2/sbin/Nginx-t 2>&1`
screen_1=`echo$detection | awk '{print $6,$7,$8}'`
screen_2=`echo$detection | awk '{print $13,$14,$15}'`
if ["$screen_1" = "Syntax is ok" ] && ["$screen_2" = "test is successful" ];
then
case "$1" in
start)
$PROG
echo "Nginx Is starting state$ok"
;;
stop)
kill -s QUIT $(cat $PIDF)
echo "Nginx Is closing state$ok"
;;
restart)
$0 stop
$0 start
echo "Nginx Is to restart state$ok"
;;
reload)
kill -s HUP $(cat $PIDF)
echo "Nginx Is overloaded state$ok"
;;
*)
echo "Usage: $0(start|stop|restart|reload)"
exit 1
esac
else
echo "Nginx check state $no "
echo "Please check the configurationfile"
echo "$detection"
fi
exit 0
保存退出后,再使用下面的命令,使其可执行
2) [root@localhost sbin]# chmod +x /etc/init.d/Nginx
3) [root@localhost sbin]# chkconfig --add Nginx #添加为系统服务
4) [root@localhost sbin]# chkconfig Nginx on
7. 通过服务启动\停止
Service Nginxstart
Service Nginxstop
Service Nginxrestart
8.遇到的问题
1)添加163源
1.首先备份/etc/yum.repos.d/CentOS-Base.repo
mv /etc/yum.repos.d/CentOS-Base.repo/etc/yum.repos.d/CentOS-Base.repo.backup
2.下载网易的repo文件
wget http://mirrors.163.com/.help/CentOS6-Base-163.repo
放入/etc/yum.repos.d/(操作前请做好相应备份)
mv CentOS6-Base-163.repo/etc/yum.repos.d/CentOS-Base.repo
3.修改CentOS-Media.repo,把 baseurl 改成baseurl=http://mirrors.163.com/centos/$releasever/os/$basearch/
vi /etc/yum.repos.d/CentOS-Media.repo
4.运行以下命令生成缓存
yum clean all
yum makecache
2)wget: 无法解析主机地址 “Nginx.org”
更改DNS
输入命令:vi /etc/resolv.conf
将DNS换成两组8888和8844
3)“cp: "conf/koi-win"与"/usr/local/nginx/conf/koi-win"为同一文件”的解决方法:
使用如下命令编译安装:
./configure--user=Nginx --group=Nginx--prefix=/usr/local/Nginx-1.10.2--conf-path=/usr/local/Nginx-1.10.2/Nginx.conf --with-http_stub_status_module --with-http_ssl_module
make&& make install
原文链接:https://www.f2er.com/centos/375726.html