添加EPEL仓库:
rpm -Uvh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
安装Nginx:
yum install nginx -y
启动Nginx服务:
service nginx start
设置开机启动:
chkconfig nginx on
安装编译依赖:
yum groupinstall "Development Tools" -y
yum install pcre-devel zlib-devel openssl-devel -y
下载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-http_gzip_static_module
编译并安装:
make && make install
创建启动脚本:
cp /usr/local/nginx/sbin/nginx /usr/sbin/
启动Nginx:
nginx
检查Nginx是否运行:
ps aux | grep nginx
检查80端口是否监听:
netstat -tulnp | grep 80
在浏览器访问服务器IP地址,应该能看到Nginx欢迎页面
主配置文件位置:
/etc/nginx/nginx.conf
/usr/local/nginx/conf/nginx.conf
重新加载配置(无需重启):
service nginx reload
# 或
nginx -s reload
检查配置语法:
nginx -t
如果启用了防火墙,需要开放80端口:
iptables -I INPUT -p tcp --dport 80 -j ACCEPT
service iptables save
端口冲突:如果Apache等服务器占用了80端口,需要先停止它们
service httpd stop
chkconfig httpd off
SELinux阻止:可以暂时禁用SELinux
setenforce 0
或永久禁用(编辑/etc/selinux/config
)
启动失败:检查错误日志
tail -n 50 /var/log/nginx/error.log
# 或源码安装的日志路径
tail -n 50 /usr/local/nginx/logs/error.log
按照以上步骤,您应该可以在CentOS 6.6上快速搭建起Nginx服务器。