: This article describes how to install nginx on a Linux server. if you are interested in the PHP Tutorial, refer to it. 1. install the dependent software package
Install C, C ++ compiler
# yum -y install gcc gcc-c++
If the python encoding problem "UnicodeDecodeError: 'ascii 'codec can't decode byte" is reported, it may be caused by Chinese characters. Find the lib directory under whereis python and create the file sitecustomize. py under/usr/lib/python2.6/site-packages and/usr/lib64/python2.6/site-packages.
# vi sitecustomize.py
The file content is as follows:
import syssys.setdefaultencoding('gb2312')
Run yum-y install gcc-c ++ again
Install other dependent software
# yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel
Download substitutions, which is mainly used for content replacement during nginx Reverse proxy
# 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. download and install 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. add nginx to the startup service
Create a file/etc/init. d/nginx
# vi /etc/init.d/nginx
The file content is as follows:
#!/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/nginx"prog=$(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
Set the constants pidfile, nginx, NGINX_CONF_FILE, and lockfile according to the actual path.
Run the following command:
# 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/
The default nginx configuration file is/usr/local/ningx/conf/nginx. conf.
Start nginx:
# service nginx start
4. add firewall rules
(Assume that nginx provides external services through ports 80 and 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
View firewall settings
# iptables --line-numbers -n -L
Fix Chinese file name garbled characters in the Linux path:
Modify server character set
For more information, see Linux server garbled text.
Install 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/*.*
Indicates that all file names under userfiles are converted from GB2312 to UTF-8
The preceding section describes how to install nginx on a Linux server, including some content. if you are interested in the PHP Tutorial.