Home  >  Article  >  Backend Development  >  Install nginx on Linux server

Install nginx on Linux server

WBOY
WBOYOriginal
2016-08-08 09:23:02742browse

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

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_C/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' -`
opti -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

以上就介绍了Linux服务器上安装nginx,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:PHP study notes 01Next article:PHP study notes 01