Nginx ("engine x") 是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器。Nginx是由Igor Sysoev为俄罗斯访问量第二的Rambler.ru站点开发的。其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名。
本文介绍的适用RHEL/CentOS 6 或以下版本,RHEL/CentOS 7 或更高版本也适用但略有不同,其安装可参考另一篇《RHEL/CentOS 7 系统上源码编译安装nginx,并隐藏替换nginx名称》。
下面就简单介绍下nginx的安装过程。
1. 安装依赖的软件包
安装C、C++编译器
# yum -y install gcc gcc-c++
如果报“UnicodeDecodeError: 'ascii' codec can't decode byte”这种python编码的问题,有可能是中文导致的。whereis python下找到lib目录,在/usr/lib/python2.6/site-packages 和 /usr/lib64/python2.6/site-packages下创建文件sitecustomize.py
# vi sitecustomize.py
文件内容如下:
import sys
sys.setdefaultencoding('gb2312')
再运行yum -y install gcc gcc-c++
安装其他依赖的软件
# yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel
下载substitutions,主要用于nginx反向代理时内容替换
# wget -c https://github.com/yaoweibin/ngx_http_substitutions_filter_module/archive/master.zip -O ngx_http_substitutions_filter_module-master.zip
# unzip ngx_http_substitutions_filter_module-master.zip
2. 下载安装nginx
# wget -c http://nginx.org/download/nginx-*.*.*.tar.gz
# tar -zxvf nginx-*.*.*.tar.gz
# cd nginx-*.*.*
# ./configure --with-http_ssl_module --add-module=../ngx_http_substitutions_filter_module-master
# make
# make install
nginx默认安装路径是在“/usr/local/nginx”目录下。
3. 将nginx加入开机启动服务
创建文件/etc/init.d/nginx
# vi /etc/init.d/nginx
文件内容如下:
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /etc/nginx/nginx.conf
# config: /etc/sysconfig/nginx
# pidfile: /var/run/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
pidfile=/usr/local/nginx/logs/nginx.pid
nginx="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/nginx.lock
make_dirs() {
# make required directories
user=`nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
options=`$nginx -V 2>&1 | grep 'configure arguments:'`
for opt in $options; do
if [ `echo $opt | grep '.*-temp-path'` ]; then
value=`echo $opt | cut -d "=" -f 2`
if [ ! -d "$value" ]; then
# echo "creating" $value
mkdir -p $value && chown -R $user $value
fi
fi
done
}
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
make_dirs
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
configtest || return $?
stop
sleep 1
start
}
reload() {
configtest || return $?
echo -n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}
force_reload() {
restart
}
configtest() {
$nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac
其中常量pidfile、nginx、NGINX_CONF_FILE、lockfile请根据实际路径设置。
执行以下命令
# chmod 755 /etc/init.d/nginx
# chkconfig --add nginx
# chkconfig nginx on
# mkdir -p /etc/nginx/conf.d
# cp /usr/local/nginx/conf/nginx.conf /etc/nginx/
nginx默认配置文件为 /usr/local/ningx/conf/nginx.conf
启动nginx:
# service nginx start
4. 增加防火墙规则
(假设nginx通过80和443端口对外提供服务):
# service iptables start
# //iptables -I INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
# iptables -I INPUT -p tcp --dport 80 -j ACCEPT
# iptables -I INPUT -p tcp --dport 443 -j ACCEPT
# service iptables save
# service iptables restart
# iptables --line-numbers -n -L
解决Linux路径下中文文件名乱码:
修改服务器字符集
详情参考文章
《Linux服务器乱码问题》。
安装convmv
# wget -c https://www.j3e.de/linux/convmv/convmv-1.15.tar.gz
# tar -zxvf convmv-1.15.tar.gz
# cd convmv-1.15
# make clean;
# make install;
# ./convmv -f GBK -t UTF-8 -r --nosmart --notest userfiles/*.*
表示 userfiles下的所有文件的文件名由GB2312转换为UTF-8
本文介绍的适用RHEL/CentOS 6 或以下版本,RHEL/CentOS 7 或更高版本也适用但略有不同,其安装可参考另一篇 《RHEL/CentOS 7 系统上源码编译安装nginx,并隐藏替换nginx名称》。