您好,欢迎访问一九零五行业门户网

Linux服务器上安装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 syssys.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 0pidfile=/usr/local/nginx/logs/nginx.pidnginx=/usr/local/nginx/sbin/nginxprog=$(basename $nginx)nginx_c/usr/local/nginx/conf/nginx.conf[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginxlockfile=/var/lock/nginx.lockmake_dirs() {# make required directoriesuser=`nginx -v 2>&1 | grep configure arguments: | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`opti -v 2>&1 | grep 'configure arguments:'`for opt in $options; doif [ `echo $opt | grep '.*-temp-path'` ]; thenvalue=`echo $opt | cut -d = -f 2`if [ ! -d $value ]; then# echo creating $valuemkdir -p $value && chown -r $user $valuefifidone}start() {[ -x $nginx ] || exit 5[ -f $nginx_conf_file ] || exit 6make_dirsecho -n $starting $prog: daemon $nginx -c $nginx_conf_fileretval=$?echo[ $retval -eq 0 ] && touch $lockfilereturn $retval}stop() {echo -n $stopping $prog: killproc $prog -quitretval=$?echo[ $retval -eq 0 ] && rm -f $lockfilereturn $retval}restart() {configtest || return $?stopsleep 1start}reload() {configtest || return $?echo -n $reloading $prog: killproc $nginx -hupretval=$?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 instart)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 2esac
其中常量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教程有兴趣的朋友有所帮助。
其它类似信息

推荐信息