确保系统已更新:
yum update -y
安装必要的依赖:
yum install -y gcc gcc-c++ make pcre-devel zlib-devel openssl-devel
添加Nginx官方YUM仓库:
vi /etc/yum.repos.d/nginx.repo
添加以下内容:
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/6/$basearch/
gpgcheck=0
enabled=1
安装Nginx:
yum install -y nginx
启动Nginx并设置开机启动:
service nginx start
chkconfig nginx on
下载Nginx源码包(以1.20.1为例):
wget http://nginx.org/download/nginx-1.20.1.tar.gz
tar zxvf nginx-1.20.1.tar.gz
cd nginx-1.20.1
配置编译选项:
./configure --prefix=/usr/local/nginx \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-pcre
编译并安装:
make && make install
创建启动脚本:
vi /etc/init.d/nginx
添加以下内容:
#!/bin/sh
# chkconfig: - 85 15
# description: Nginx is a World Wide Web server.
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"
$nginx -s stop
echo " done"
;;
reload)
echo -n "Reloading Nginx"
$nginx -s reload
echo " done"
;;
restart)
$0 stop
$0 start
;;
*)
echo "Usage: $0 {start|stop|restart|reload}"
exit 1
;;
esac
设置权限并添加服务:
chmod +x /etc/init.d/nginx
chkconfig --add nginx
chkconfig nginx on
启动Nginx:
service nginx start
检查Nginx是否运行:
ps aux | grep nginx
检查端口监听:
netstat -tulnp | grep nginx
访问测试:
curl http://localhost
或在浏览器中访问服务器IP地址
主配置文件位置:
/etc/nginx/nginx.conf
/usr/local/nginx/conf/nginx.conf
常用命令:
service nginx start # 启动
service nginx stop # 停止
service nginx restart # 重启
service nginx reload # 重新加载配置
如果启用了防火墙,需要开放80端口:
iptables -I INPUT -p tcp --dport 80 -j ACCEPT
service iptables save
service iptables restart
端口冲突:如果Apache等服务器已占用80端口,需先停止这些服务
service httpd stop
chkconfig httpd off
SELinux阻止:可以临时关闭SELinux或设置正确权限
setenforce 0 # 临时关闭
启动失败:检查错误日志
tail -f /var/log/nginx/error.log # YUM安装
tail -f /usr/local/nginx/logs/error.log # 源码安装
按照以上步骤,您应该可以在CentOS 6.5 64位系统上成功安装并运行Nginx服务器。