Nginx Installation Manual

Nginx Installation Manual

<!--[if !supportLists]--> <!--[endif]--> nginx installation environment

Nginx is developed in C language. It is recommended to run on linux . This tutorial uses Centos6.5 as the installation environment.

<!--[if !supportLists]--><!--[endif]-->gcc

To install nginx , you need to compile the source code downloaded from the official website first. The compilation depends on the gcc environment. If there is no gcc environment, you need to install gcc : yum install gcc-c++

<!--[if !supportLists]--><!--[endif]-->PCRE

PCRE (Perl Compatible Regular Expressions) is a Perl library that includes a perl -compatible regular expression library. The http module of nginx uses pcre to parse regular expressions, so the pcre library needs to be installed on linux .

yum install -y pcre pcre-devel

Note: pcre-devel is a secondary development library developed using pcre . nginx also requires this library.

<!--[if !supportLists]--><!--[endif]-->zlib

The zlib library provides many ways to compress and decompress. nginx uses zlib to gzip the content of the http package , so the zlib library needs to be installed on linux .

yum install -y zlib zlib-devel

 

<!--[if !supportLists]--><!--[endif]-->openssl

OpenSSL is a powerful secure socket layer cryptographic library, including major cryptographic algorithms, commonly used key and certificate encapsulation management functions and SSL protocols, and provides a wealth of applications for testing or other purposes.

Nginx not only supports the http protocol, but also supports https (that is , transmitting http over the ssl protocol ), so you need to install the openssl library in linux .

yum install -y openssl openssl-devel

 

<!--[if !supportLists]--> <!--[endif]--> Compile and install

Copy nginx-1.8.0.tar.gz to the linux server.

 

Unzip:

tar -zxvf nginx-1.8.0.tar.gz

cd nginx-1.8.0

 

<!--[if !supportLists]-->1、 <!--[endif]-->configure

./configure --help query detailed parameters (refer to the appendix part of this tutorial: nginx compilation parameters)

 

The parameter settings are as follows:

./configure \

--prefix=/usr/local/nginx \

--pid-path=/var/run/nginx/nginx.pid \

--lock-path=/var/lock/nginx.lock \

--error-log-path=/var/log/nginx/error.log \

--http-log-path=/var/log/nginx/access.log \

--with-http_gzip_static_module \

--http-client-body-temp-path=/var/temp/nginx/client \

--http-proxy-temp-path=/var/temp/nginx/proxy \

--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \

--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \

--http-scgi-temp-path=/var/temp/nginx/scgi

 

Note: The temporary file directory is specified as /var/temp/nginx above, and the temp and nginx directories need to be created under /var

 

 

<!--[if !supportLists]--> 2.  <!--[endif]--> Compile and install

make

make  install

 

安装成功查看安装目录

 

 

 

<!--[if !supportLists]--><!--[endif]-->启动nginx

cd /usr/local/nginx/sbin/

./nginx

 

查询nginx进程:

 

 

15098nginx主进程的进程id15099nginx工作进程的进程id

 

注意:执行./nginx启动nginx,这里可以-c指定加载的nginx配置文件,如下:

./nginx -c /usr/local/nginx/conf/nginx.conf

如果不指定-cnginx在启动时默认加载conf/nginx.conf文件,此文件的地址也可以在编译安装nginx时指定./configure的参数(--conf-path= 指向配置文件(nginx.conf))

 

<!--[if !supportLists]--><!--[endif]-->停止nginx

方式1,快速停止:

cd /usr/local/nginx/sbin

./nginx -s stop

此方式相当于先查出nginx进程id再使用kill命令强制杀掉进程。

 

方式2,完整停止(建议使用)

cd /usr/local/nginx/sbin

./nginx -s quit

此方式停止步骤是待nginx进程处理任务完毕进行停止。

 

 

<!--[if !supportLists]--><!--[endif]-->重启nginx

方式1,先停止再启动(建议使用):

nginx进行重启相当于先停止nginx再启动nginx,即先执行停止命令再执行启动命令。

如下:

./nginx -s quit

./nginx

 

方式2,重新加载配置文件:

nginx的配置文件nginx.conf修改后,要想让配置生效需要重启nginx,使用-s reload不用先停止nginx再启动nginx即可将配置信息在nginx中生效,如下:

./nginx -s reload

 

<!--[if !supportLists]--><!--[endif]-->测试

nginx安装成功,启动nginx,即可访问虚拟机上的nginx

 

 

到这说明nginx上安装成功。

 

<!--[if !supportLists]--><!--[endif]-->开机自启动nginx

<!--[if !supportLists]-->7.1 <!--[endif]-->编写shell脚本

这里使用的是编写shell脚本的方式来处理

 

vi /etc/init.d/nginx  (输入下面的代码)

 

 

#!/bin/bash

# nginx Startup script for the Nginx HTTP Server

# it is v.0.0.2 version.

# chkconfig: - 85 15

# description: Nginx is a high-performance web and proxy server.

#              It has a lot of features, but it's not for everyone.

# processname: nginx

# pidfile: /var/run/nginx.pid

# config: /usr/local/nginx/conf/nginx.conf

nginxd=/usr/local/nginx/sbin/nginx

nginx_config=/usr/local/nginx/conf/nginx.conf

nginx_pid=/var/run/nginx.pid

RETVAL=0

prog="nginx"

# Source function library.

. /etc/rc.d/init.d/functions

# Source networking configuration.

. /etc/sysconfig/network

# Check that networking is up.

[ ${NETWORKING} = "no" ] && exit 0

[ -x $nginxd ] || exit 0

# Start nginx daemons functions.

start() {

if [ -e $nginx_pid ];then

   echo "nginx already running...."

   exit 1

fi

   echo -n $"Starting $prog: "

   daemon $nginxd -c ${nginx_config}

   RETVAL=$?

   echo

   [ $RETVAL = 0 ] && touch /var/lock/subsys/nginx

   return $RETVAL

}

# Stop nginx daemons functions.

stop() {

        echo -n $"Stopping $prog: "

        killproc $nginxd

        RETVAL=$?

        echo

        [ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /var/run/nginx.pid

}

# reload nginx service functions.

reload() {

    echo -n $"Reloading $prog: "

    #kill -HUP `cat ${nginx_pid}`

    killproc $nginxd -HUP

    RETVAL=$?

    echo

}

# See how we were called.

case "$1" in

start)

        start

        ;;

stop)

        stop

        ;;

reload)

        reload

        ;;

restart)

        stop

        start

        ;;

status)

        status $prog

        RETVAL=$?

        ;;

*)

        echo $"Usage: $prog {start|stop|restart|reload|status|help}"

        exit 1

esac

exit $RETVAL

 

 

 

:wq  保存并退出

 

<!--[if !supportLists]-->7.2 <!--[endif]-->设置文件的访问权限

 

chmod a+x /etc/init.d/nginx   (a+x ==> all user can execute  所有用户可执行)

 

 

这样在控制台就很容易的操作nginx了:查看Nginx当前状态、启动Nginx、停止Nginx、重启Nginx

 

如果修改了nginx的配置文件nginx.conf,也可以使用上面的命令重新加载新的配置文件并运行,可以将此命令加入到rc.local文件中,这样开机的时候nginx就默认启动了

 

<!--[if !supportLists]-->7.3 <!--[endif]-->加入到rc.local文件中

vi /etc/rc.local

 

加入一行  /etc/init.d/nginx start    保存并退出,下次重启会生效。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326348012&siteId=291194637