Change the Web server to nginx

Source: Internet
Author: User
Tags tld

The temptation to change the Web serverNginx(Pronunciation [engine x]), a high-performance WEB server written by Russians. [Because in the linode360 solution, if Apache is used to achieve better performance, oom occurs frequently. To avoid Oom, only concurrent processing capability and memory configuration can be reduced.]

Built-in Debian LennyNginxIs 0.6x, and the latest version is 0.7x. Therefore, we decided to install it from the official website.

This article briefly introduces the complete installation process.

First, install the necessary packages. These packages will be used later:

$ sudo apt-get install libssl-dev libpcre3-dev build-essential$ sudo apt-get install php5 php5-cgi mysql-server mysql-client

Next download and installNginx(Note: The download address may change. For details, refer to http: // wiki.Nginx. Org/nginxinstall # stable ):

$ wget http://sysoev.ru/nginx/nginx-0.7.62.tar.gz$ tar -zxvf nginx-0.7.62.tar.gz$ cd nginx-0.7.62$ ./configure --prefix=/usr --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --pid-path=/var/run/nginx/nginx.pid  --lock-path=/var/lock/nginx.lock --user=nginx --group=nginx --with-http_ssl_module --with-http_flv_module --with-http_gzip_static_module --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/tmp/nginx/client/ --http-proxy-temp-path=/var/tmp/nginx/proxy/ --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ --with-md5-asm --with-md5=/usr/include --with-sha1-asm --with-sha1=/usr/include --with-http_stub_status_module$ make$ sudo make install

Install spawn-fcgi (a part of Lighttpd used to manage the PHP-CGI process)

$ wget http://www.lighttpd.net/download/spawn-fcgi-1.6.2.tar.gz$ tar -zxvf spawn-fcgi-1.6.2.tar.gz$ cd spawn-fcgi-1.6.2$ ./configure$ make$ sudo cp ./src/spawn-cgi /usr/bin/

CreateNginxAnd PHP-FastCGI initScript

$ sudo vi /etc/init.d/php-fastcgi$ sudo vi /etc/init.d/nginx$ sudo chmod +x /etc/init.d/php-fastcgi$ sudo chmod +x /etc/init.d/nginx

/Etc/init. d/NginxFileContent:

#! /bin/sh### BEGIN INIT INFO# Provides:          nginx# Required-Start:    $remote_fs $syslog# Required-Stop:     $remote_fs $syslog# Default-Start:     2 3 4 5# Default-Stop:      0 1 6# Short-Description: nginx init.d script for Ubuntu 8.10 and lesser versions.# Description:       nginx init.d script for Ubuntu 8.10 and lesser versions.### END INIT INFO#------------------------------------------------------------------------------# nginx - this script, which starts and stops the nginx daemon for ubuntu.## description:  Nginx is an HTTP(S) server, HTTP(S) reverse \#               proxy and IMAP/POP3 proxy server.  This \#               script will manage the initiation of the \#               server and its process state.## processname: nginx# config:      /usr/local/nginx/conf/nginx.conf# pidfile:     /acronymlabs/server/nginx.pid# Provides:    nginx## Author:  Jason Giedymin#          <jason.giedymin AT acronymlabs.com>.## Version: 1.0 01-Apr-2009 jason.giedymin AT gmail.com# Notes: nginx init.d script for Ubuntu 8.10 and lesser versions.##------------------------------------------------------------------------------#                               MIT X11 License#------------------------------------------------------------------------------## Copyright (c) 2009 Jason Giedymin, http://AcronymLabs.com## Permission is hereby granted, free of charge, to any person obtaining# a copy of this software and associated documentation files (the# "Software"), to deal in the Software without restriction, including# without limitation the rights to use, copy, modify, merge, publish,# distribute, sublicense, and/or sell copies of the Software, and to# permit persons to whom the Software is furnished to do so, subject to# the following conditions:## The above copyright notice and this permission notice shall be# included in all copies or substantial portions of the Software.## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.#------------------------------------------------------------------------------#------------------------------------------------------------------------------#                               Functions#------------------------------------------------------------------------------. /lib/lsb/init-functions#------------------------------------------------------------------------------#                               Consts#------------------------------------------------------------------------------PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/binDAEMON=/usr/sbin/nginxNAME=nginxDESCRIPTION="Nginx Server..."PIDSPATH=/var/run/nginxPS=$NAME                                #the process, which happens to be the NAMEPIDFILE=$NAME.pid                       #pid fileRUNAS=root                              #user to run asSCRIPT_OK=0                             #ala error codesSCRIPT_ERROR=1                          #ala error codesTRUE=1                                  #booleanFALSE=0                                 #booleanlockfile=/var/lock/subsys/nginxNGINX_CONF_FILE="/etc/nginx/nginx.conf"#------------------------------------------------------------------------------#                               Simple Tests#------------------------------------------------------------------------------#test if nginx is a file and executabletest -x $DAEMON || exit 0# Include nginx defaults if availableif [ -f /etc/default/nginx ] ; then        . /etc/default/nginxfi#set exit condition#set -e#------------------------------------------------------------------------------#                               Functions#------------------------------------------------------------------------------configtest() {        $DAEMON -t -c $NGINX_CONF_FILE}getPSCount() {        return `pgrep -f $PS | wc -l`}isRunning(){        pidof_daemon        PID=$?        if [ $PID -gt 0 ]; then                return 1        else                return 0        fi}status(){        isRunning        isAlive=$?        if [ "${isAlive}" -eq $TRUE ]; then                echo "$NAME found running with processes:  `pidof $PS`"        else                echo "$NAME is NOT running."        fi}removePIDFile(){        if [ -f $PIDSPATH/$NAME.pid ]; then                rm $PIDSPATH/$NAME.pid        fi}start() {        log_daemon_msg "Starting $DESCRIPTION"        isRunning        isAlive=$?        if [ "${isAlive}" -eq $TRUE ]; then                log_end_msg $SCRIPT_ERROR        else                start-stop-daemon --start --quiet --chuid $RUNAS --pidfile $PIDSPATH/$PIDFILE --exec $DAEMON                chmod 400 $PIDSPATH/$PIDFILE                log_end_msg $SCRIPT_OK        fi}stop() {        log_daemon_msg "Stopping $DESCRIPTION"        isRunning        isAlive=$?        if [ "${isAlive}" -eq $TRUE ]; then                start-stop-daemon --stop --quiet --pidfile $PIDSPATH/$PIDFILE                removePIDFile                log_end_msg $SCRIPT_OK        else                log_end_msg $SCRIPT_ERROR        fi}reload() {        configtest || return $?        log_daemon_msg "Reloading (via HUP) $DESCRIPTION"        isRunning        if [ $? -eq $TRUE ]; then                `killall -HUP $PS` #to be safe                log_end_msg $SCRIPT_OK        else                log_end_msg $SCRIPT_ERROR        fi}terminate() {        log_daemon_msg "Force terminating (via KILL) $DESCRIPTION"        PIDS=`pidof $PS` || true        [ -e $PIDSPATH/$PIDFILE ] && PIDS2=`cat $PIDSPATH/$PIDFILE`        for i in $PIDS; do                if [ "$i" = "$PIDS2" ]; then                        kill $i                        removePIDFile                fi        done        log_end_msg $SCRIPT_OK}pidof_daemon() {    PIDS=`pidof $PS` || true    [ -e $PIDSPATH/$PIDFILE ] && PIDS2=`cat $PIDSPATH/$PIDFILE`    for i in $PIDS; do        if [ "$i" = "$PIDS2" ]; then            return 1        fi    done    return 0}case "$1" in  start)        start        ;;  stop)        stop        ;;  restart|force-reload)        stop        start        ;;  reload)        $1        ;;  status)        status        ;;  configtest)        $1        ;;  terminate)        $1        ;;  *)        FULLPATH=/etc/init.d/$NAME        echo "Usage: $FULLPATH {start|stop|restart|force-reload|status|configtest|terminate}"        exit 1        ;;esacexit 0

/Etc/init. d/PHP-FastCGIFileContent:

#! /bin/sh### BEGIN INIT INFO# Provides:          php-fastcgi# Required-Start:    $all# Required-Stop:     $all# Default-Start:     2 3 4 5# Default-Stop:      0 1 6# Short-Description: Start and stop php-cgi in external FASTCGI mode# Description:       Start and stop php-cgi in external FASTCGI mode### END INIT INFO# Author: Kurt Zankl <[EMAIL PROTECTED]># Do NOT "set -e"PATH=/sbin:/usr/sbin:/bin:/usr/binDESC="php-cgi in external FASTCGI mode"NAME=php-fastcgiDAEMON=/usr/bin/php-cgiPIDFILE=/var/run/$NAME.pidSCRIPTNAME=/etc/init.d/$NAMEPHP_CONFIG_FILE=/etc/php5/cgi/php.ini# Exit if the package is not installed[ -x "$DAEMON" ] || exit 0# Read configuration variable file if it is present[ -r /etc/default/$NAME ] && . /etc/default/$NAME# Load the VERBOSE setting and other rcS variables. /lib/init/vars.sh# Define LSB log_* functions.# Depend on lsb-base (>= 3.0-6) to ensure that this file is present.. /lib/lsb/init-functions# If the daemon is not enabled, give the user a warning and then exit,# unless we are stopping the daemonif [ "$START" != "yes" -a "$1" != "stop" ]; then        log_warning_msg "To enable $NAME, edit /etc/default/$NAME and set START=yes"        exit 0fi# Process configurationexport PHP_FCGI_CHILDREN PHP_FCGI_MAX_REQUESTSDAEMON_ARGS="-q -b $FCGI_HOST:$FCGI_PORT -c $PHP_CONFIG_FILE"do_start(){        # Return        #   0 if daemon has been started        #   1 if daemon was already running        #   2 if daemon could not be started        start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON --test > /dev/null \                || return 1        start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON \                --background --make-pidfile --chuid $EXEC_AS_USER --startas $DAEMON -- \                $DAEMON_ARGS \                || return 2}do_stop(){        # Return        #   0 if daemon has been stopped        #   1 if daemon was already stopped        #   2 if daemon could not be stopped        #   other if a failure occurred        start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE > /dev/null # --name $DAEMON        RETVAL="$?"        [ "$RETVAL" = 2 ] && return 2        # Wait for children to finish too if this is a daemon that forks        # and if the daemon is only ever run from this initscript.        # If the above conditions are not satisfied then add some other code        # that waits for the process to drop all resources that could be        # needed by services started subsequently.  A last resort is to        # sleep for some time.        start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --exec $DAEMON        [ "$?" = 2 ] && return 2        # Many daemons don't delete their pidfiles when they exit.        rm -f $PIDFILE        return "$RETVAL"}case "$1" in  start)        [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"        do_start        case "$?" in                0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;                2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;        esac        ;;  stop)        [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"        do_stop        case "$?" in                0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;                2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;        esac        ;;  restart|force-reload)        log_daemon_msg "Restarting $DESC" "$NAME"        do_stop        case "$?" in          0|1)                do_start                case "$?" in                        0) log_end_msg 0 ;;                        1) log_end_msg 1 ;; # Old process is still running                        *) log_end_msg 1 ;; # Failed to start                esac                ;;          *)                # Failed to stop                log_end_msg 1                ;;        esac        ;;  *)        echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2        exit 3        ;;esac

CreateNginxConfigurationFile/Etc/Nginx/Nginx. Conf (and implement virtualhost)

User www-data; worker_processes 4; events {worker_connections 1024; Use epoll;} HTTP {include mime. types; default_type application/octet-stream; sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; gzip on; Include/etc/Nginx/Sites-enabled/*; # implementationNginxVirtualhost}

Create virtualhostFile/Etc/Nginx/Sites-available/domain. TLD

server {        listen       80;        server_name  domain.tld www.domain.tld;        access_log      /srv/www/domain.tld/logs/access.log;        location / {                root   /srv/www/domain.tld/public_html;                index  index.html index.htm index.php;        }        error_page   500 502 503 504  /50x.html;        location = /50x.html {                root   /usr/html;        }        location ~ \.php$ {                root           /srv/www/domain.tld/public_html;                fastcgi_pass    127.0.0.1:9000;                fastcgi_index  index.php;                fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;                include        fastcgi_params;        }}

Enable virualhost

$ sudo ln -s /etc/nginx/sites-available/domain.tld /etc/nginx/sites-enabled/domain.tld

Start PHP-FastCGI andNginx

$ sudo /etc/init.d/php-fastcgi start$ sudo /etc/init.d/nginx start

Configure PHP-FastCGI andNginxAuto Start

$ Sudo apt-Get install rcconf $ sudo rcconf # select PHP-FastCGI andNginx

In this case, the PHP configurationFileUse/etc/PhP5/cgi/PHP. ini.

Create a rewrite rule for WordPress: Modify/etc/Nginx/Sites-available/domain. TLD, add the following content to location:

location / {        root   /srv/www/weigblog.com/public_html;        index  index.html index.htm index.php;        if (!-e $request_filename) {                rewrite (.*) /index.php;        }}

Where:

-E is an existing directory orFile

-D indicates the existing Directory.

-F existsFile

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.