在CentOS 6上部署Nginx
时间:2020-01-09 10:38:08 来源:igfitidea点击:
说明
Nginx设计用于高性能和高效的系统资源使用。
Nginx的另一个好处是易于配置和使用。配置文件易于理解,并使用普通英语。这意味着精简和优化配置所花的时间更少,部署应用程序的时间也更多。
大多数站点使用Nginx作为代理,提供静态内容,然后将请求转发到托管动态内容的Apache服务器。但是,最新版本的Nginx现在可以处理FastCGI,从而可以托管由PHP,Python和许多其他功能支持的动态内容。
安装Nginx
截至本文发表之日,Nginx不在CentOS的默认存储库中。这给我们留下了两个选择:从源代码构建它或者添加Nginx存储库以使用YUM进行安装。
从存储库安装
- 导航到/etc/yum.repos.d
- 为Nginx创建一个仓库配置文件
vi nginx.repo
- 将以下行添加到nginx.conf
[nginx] name=nginx repo baseurl=http://nginx.org/packages/centos/$releasever/$basearch/ gpgcheck=0 enabled=1
- 保存更改并退出文本编辑器。
- 安装Nginx
yum install nginx
从源代码安装Nginx
- 确保在系统上安装了必需的开发包。他们将需要编译源文件。
yum groupinstall "development tools"
- 在wget中从Nginx网站下载最新版本。本教程撰写时的最新版本是1.5.8.
wget http://nginx.org/download/nginx-1.5.8.tar.gz
- 解压缩下载的文件。
tar -xvf nginx-1.5.8.tar.gz
- 导航到新的nginx目录
cd nginx-1.5.8
- 要启用重写模块,请安装PCRE开发包。
yum install pcre-devel
- 要启用GZip功能,请安装zlib开发包。
yum install zlib-devel
- 准备要在服务器上编译的源文件。
./configure
- 从源文件编译Nginx
make install
- 将新的Nginx二进制文件复制到/ usr / sbin
cp /usr/local/nginx/sbin/nginx /usr/sbin/nginx
- 在/ etc中创建Nginx目录
mkdir -p /etc/nginx
- 将nginx.conf模板复制到/ etc / nginx
cp /usr/local/nginx/conf/nginx.conf /etc/nginx/nginx.conf
- 将mime.types模板复制到/ etc / nginx
cp /usr/local/gninx/conf/mime.types /etc/nginx
- 创建Nginx服务帐户
useradd --shell /sbin/nologin nginx
- 创建Nginx日志目录
mkdir -p /var/log/nginx
- 创建一个Nginx启动脚本文件。
touch /etc/init.d/nginx
- 在文本编辑器中打开启动脚本,并添加以下几行
#!/bin/sh
#
# nginx Startup script for nginx
#
# chkconfig: - 85 15
# processname: nginx
# config: /etc/nginx/nginx.conf
# config: /etc/sysconfig/nginx
# pidfile: /var/run/nginx.pid
# description: nginx is an HTTP and reverse proxy server
#
### BEGIN INIT INFO
# Provides: nginx
# Required-Start: $local_fs $remote_fs $network
# Required-Stop: $local_fs $remote_fs $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: start and stop nginx
### END INIT INFO
# Source function library.
. /etc/rc.d/init.d/functions
if [ -f /etc/sysconfig/nginx ]; then
. /etc/sysconfig/nginx
fi
prog=nginx
nginx=${NGINX-/usr/sbin/nginx}
conffile=${CONFFILE-/etc/nginx/nginx.conf}
lockfile=${LOCKFILE-/var/lock/subsys/nginx}
pidfile=${PIDFILE-/var/run/nginx.pid}
SLEEPMSEC=100000
RETVAL=0
start() {
echo -n $"Starting $prog: "
daemon --pidfile=${pidfile} ${nginx} -c ${conffile}
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch ${lockfile}
return $RETVAL
}
stop() {
echo -n $"Stopping $prog: "
killproc -p ${pidfile} ${prog}
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}
}
reload() {
echo -n $"Reloading $prog: "
killproc -p ${pidfile} ${prog} -HUP
RETVAL=$?
echo
}
upgrade() {
oldbinpidfile=${pidfile}.oldbin
configtest -q || return 6
echo -n $"Staring new master $prog: "
killproc -p ${pidfile} ${prog} -USR2
RETVAL=$?
echo
/bin/usleep $SLEEPMSEC
if [ -f ${oldbinpidfile} -a -f ${pidfile} ]; then
echo -n $"Graceful shutdown of old $prog: "
killproc -p ${oldbinpidfile} ${prog} -QUIT
RETVAL=$?
echo
else
echo $"Upgrade failed!"
return 1
fi
}
configtest() {
if [ "$#" -ne 0 ] ; then
case "" in
-q)
FLAG=
;;
*)
;;
esac
shift
fi
${nginx} -t -c ${conffile} $FLAG
RETVAL=$?
return $RETVAL
}
rh_status() {
status -p ${pidfile} ${nginx}
}
# See how we were called.
case "" in
start)
rh_status >/dev/null 2>&1 && exit 0
start
;;
stop)
stop
;;
status)
rh_status
RETVAL=$?
;;
restart)
configtest -q || exit $RETVAL
stop
start
;;
upgrade)
upgrade
;;
condrestart|try-restart)
if rh_status >/dev/null 2>&1; then
stop
start
fi
;;
force-reload|reload)
reload
;;
configtest)
configtest
;;
*)
echo $"Usage: $prog {start|stop|restart|condrestart|try-restart|force-reload|upgrade|reload|status|help|configtest}"
RETVAL=2
esac
exit $RETVAL
- 在文本编辑器中打开Nginx配置文件,然后修改突出显示的区域以匹配系统。
user nginx;
# Set value to number of process cores
worker_processes 2;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
gzip on;
include /etc/nginx/conf.d/*.conf;
}
- 通过启动Nginx守护程序来测试安装
service nginx start
配置Nginx
- 打开Nginx配置文件
vi /etc/nginx/nginx.conf
- 修改worker_process值以反映服务器中安装的处理器核心数量。
worker_processes 2
- 要启用gzip压缩,请找到以下行:
#gzip on
并取消注释
gzip on
- 保存更改并退出文本编辑器。
- 重新启动Nginx守护程序使更改生效。
service nginx restart
- 将Nginx配置为在重启后自动启动。
chkconfig nginx on
配置默认网站
- 打开默认的网站配置文件。
vi /etc/nginx/conf.d/default.conf
- 要设置监听端口,请找到以下行并修改其值:
listen 80;
- 通过找到以下行并将localhost替换为服务器名称来设置网站的DNS主机名。
server_name localhost;
- 默认的网站根目录是/ usr / share / nginx / html。要对其进行更改,请找到以下几行,并将突出显示的值替换为所需的文件路径。
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
- 要修改默认索引文件,请添加或者替换索引旁边列出的值。
- 保存更改并退出文本编辑器。
配置防火墙以允许HTTP访问
- 运行以下命令以允许通过IPTables进行HTTP访问。
iptables -A INPUT -m state --state NEW -P tcp --dport 80 -j ACCEPT
- 要永久保存防火墙规则,请运行以下命令。
/sbin/service iptables save

