linux 下 nginx 服务安装及配置,开机自动启动

最近经常需要安装linux服务器,经过网上查找整理资料,以备后用。

模块依赖性Nginx需要依赖下面3个包

1. gzip 模块需要 zlib 库 ( 下载: http://www.zlib.net/ )

2. rewrite 模块需要 pcre 库 ( 下载: http://www.pcre.org/ )

3. ssl 功能需要 openssl 库 ( 下载: http://www.openssl.org/ )

Nginx包下载: http://nginx.org/en/download.html

依赖包安装顺序依次为:openssl、zlib、pcre, 然后安装Nginx包.

第一步: 下载安装所需包

openssl-fips-2.0.2.tar.gz

zlib-1.2.7.tar.gz

pcre-8.21.tar.gz

nginx-1.2.6.tar.gz

第二步:依次安装openssl-fips-2.0.2.tar.gz, zlib-1.2.7.tar.gz, pcre-8.21.tar.gz, nginx-1.2.6.tar.gz

1.安装openssl-fips-2.0.2.tar.gz

[root@localhost mrms]# tar -zxvf openssl-fips-2.0.2.tar.gz

 

[root@localhost mrms]# cd openssl-fips-2.0.2

 

[root@localhost openssl-fips-2.0.2]# ./config

 

[root@localhost openssl-fips-2.0.2]# make

 

[root@localhost openssl-fips-2.0.2]# make install

2.安装zlib-1.2.7.tar.gz

[root@localhost mrms]# tar -zxvf zlib-1.2.7.tar.gz

 

[root@localhost mrms]# cd zlib-1.2.7

 

[root@localhost zlib-1.2.7]# ./configure

 

[root@localhost zlib-1.2.7]# make

 

[root@localhost zlib-1.2.7]# make install

3.安装pcre-8.21.tar.gz

[root@localhost mrms]# tar -zxvf pcre-8.21.tar.gz

 

[root@localhost mrms]# cd pcre-8.21

 

[root@localhost pcre-8.21]# ./configure

 

[root@localhost pcre-8.21]# make

 

[root@localhost pcre-8.21]# make install

 4.安装 nginx-1.2.6.tar.gz

[root@localhost mrms]# tar -zxvf nginx-1.2.6.tar.gz

 

[root@localhost mrms]# cd nginx-1.2.

[root@localhost nginx-1.2.6]# ./configure --with-pcre=../pcre-8.21 --with-zlib=../zlib-1.2.7 --with-openssl=../openssl-fips-2.0.2

我主要使用上面的命令,进行简单安装。根据加载不同 下面四条命令同上面,列出来更参考,主要是启用的功能不同,同时你还要有相应的组件哦,我目前只用到pcre,zlib,openssl 这三个。

./configure --with-http_stub_status_module --with-http_realip_module  --with-pcre=../pcre-8.38 --add-module=../ngx_cache_purge-2.3 --prefix=/usr/local/nginx --with-zlib=../zlib-1.2.8 --with-openssl=../openssl-fips-2.0.13   --add-module=../nginx_upstream_check_module-master/

./configure --with-pcre=../pcre-8.39 --add-module=../ngx_cache_purge-2.1 --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module  --with-zlib=../zlib-1.2.8 --with-openssl=../openssl-fips-2.0.13

  ./configure --with-pcre=../pcre-8.39 --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module  --with-zlib=../zlib-1.2.8 --with-openssl=../openssl-fips-2.0.13

 ./configure --with-pcre=../pcre-8.39 --add-module=../ngx_cache_purge-2.3 --prefix=/usr/local/nginx --with-http_stub_status_module  --with-zlib=../zlib-1.2.8 --with-openssl=../openssl-fips-2.0.13

[root@localhost nginx-1.2.6]# make

[root@localhost nginx-1.2.6]# make install

至此Nginx的安装完成!

第三步:检测是否安装成功

[root@localhost nginx-1.2.6]# cd  /usr/local/nginx/sbin

[root@localhost sbin]# ./nginx –t

出现如下所示提示,表示安装成功

 

启动nginx

[root@localhost sbin]# ./nginx

查看端口

[root@localhost sbin]# netstat -ntlp

结果如下

第四步:设置开机自动启动

首先,在linux系统的/etc/init.d/目录下创建nginx文件,使用如下命令:

vi /etc/init.d/nginx

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

 

注意修改其中相关个性化路径设置,调整成自己的相应的路径:
nginxd=/usr/local/nginx/sbin/nginx
nginx_config=/usr/local/nginx/conf/nginx.conf
nginx_pid=/var/run/nginx.pid

接着,设置文件的访问权限:

chmod a+x /etc/init.d/nginx    ;(a+x参数表示 ==> all user can execute  所有用户可执行)
 最后将ngix加入到rc.local文件中,这样开机的时候nginx就默认启动了
vi /etc/rc.local

在脚本后面添加:

/etc/init.d/nginx start  

保存,退出,可以重启测试了。

 

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 创作都市 设计师:CSDN官方博客 返回首页