Linux 서버에 nginx 설치

WBOY
풀어 주다: 2016-08-08 09:23:02
원래의
703명이 탐색했습니다.

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教程有兴趣的朋友有所帮助。

관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!