01 Linux安装Nginx及配置开机启动项




第一  安装Nginx

参考链接

环境介绍:

    Linux :CentOS6.5 X64

关闭防火墙和selinux

1 关闭防火墙

[root@fydb02 ~]# service iptables stop

[root@fydb02 ~]# chkconfig iptables off


2 关闭selinux

[root@fydb02 ~]# sed -i "s/SELINUX=enforcing/SELINUX=disabled/" /etc/selinux/config
[root@fydb02 ~]# setenforce 0



一、准备nginx的安装环境

      yum -y install gcc gcc-c++ autoconf automake make

      yum -y install zlib zlib-devel openssl openssl--devel pcre pcre-devel 


下面正式开始


 ---------------------------------------------------------------------------

一般我们都需要先装pcre, zlib,前者为了重写rewrite,后者为了gzip压缩。

跟Windows安装软件一样,可以自定义目录,这里以/soft  为例

一:Nginx安装

[root@user ~]# cd  /soft

1.安装PCRE

ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/ 下载最新的 PCRE 源码包,使用下面命令下载编译和安装 PCRE 包:

[root@user ~]#  cd /soft

[root@user ~]#  wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.40.tar.gz 

[root@user ~]# tar -zxvf pcre-8.40.tar.gz

[root@user ~]# cd pcre-8.40

[root@user ~]# ./configure

[root@user ~]# make

[root@user ~]#make install

2.安装zlib库

http://zlib.net/zlib-1.2.11.tar.gz 下载最新的 zlib 源码包,使用下面命令下载编译和安装 zlib包:

[root@user ~]# cd /soft

[root@user ~]#wget http://zlib.net/zlib-1.2.11.tar.gz

注意: 如果下载提示找不到路径,请更改版本号再试

[root@user ~]#tar -zxvf zlib-1.2.11.tar.gz

[root@user ~]# cd zlib-1.2.11

[root@user ~]#./configure

[root@user ~]#make

[root@user ~]#make install

3.安装ssl(某些vps默认没装ssl)

[root@user ~]#cd /soft

[root@user ~]#wget http://www.openssl.org/source/openssl-fips-2.0.14.tar.gz 

[root@user ~]#tar -zxvf openssl-fips-2.0.14.tar.gz

[root@user ~]#cd openssl-fips-2.0.14

[root@user ~]#./config 

[root@user ~]#make

[root@user ~]#make install

[root@user ~]#yum -y install openssl openssl-devel

4.安装nginx

 Nginx 一般有两个版本,分别是稳定版和开发版,您可以根据您的目的来选择这两个版本的其中一个。

 注意:nginx的默认安装路径是 /usr/local/nginx  其运行文件在其/usr/local/nginx/sbin目录下面

[root@user ~]#  cd /soft

[root@user ~]# wget http://nginx.org/download/nginx-1.4.2.tar.gz

[root@user ~]# tar -zxvf nginx-1.4.2.tar.gz

[root@user ~]# cd nginx-1.4.2

[root@user ~]# ./configure 

[root@user ~]#  make

[root@user ~]#  make install

如果直接执行  ./configure 则nginx安装到默认路径,自定义安装步骤如下

注意:先添加nginx用户和组

[root@user ~]#groupadd -r nginx

[root@user ~]#useradd -s /sbin/nologin -g nginx -r nginx

配置 实例1 (后面的配置都是基于此实例编写的)

[root@user ~]#./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-http_stub_status_module

---------------------------个人推荐做法---------------补充实例2  start-----------------------------------------------

配置 实例2

[root@user ~]#./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_flv_module --with-http_gzip_static_module --with-http_stub_status_module


说明:

--prefix指定安装目录

如果执行下面命令:

[root@user ~]# ./configure  --prefix=/usr/local/nginx

编译后会在/usr/local/nginx目录下生成如下目录

....conf 配置文件 

 ... html网页文件

 ...logs  日志文件  pid文件会在此目录中

 ...sbin  主要二进制程序

------------------------------------------------------补充实例2  end---------------------------------------------------

5.启动验证

 确保系统的 80 端口没被其他程序占用

[root@user ~]#nginx

可能会报错:nginx: [emerg] mkdir() "/var/tmp/nginx/client" failed (2: No such file or directory)

原因:没有/var/tmp/nginx/client这样的目录(自定义安装配置中设置了该路径)

[root@user ~]#mkdir -pv /var/tmp/nginx/client

[root@user ~]#nginx

[root@user ~]#ps aux|grep nginx


通过浏览器访问IP


安装成功!!!!!


第二 配置Nginx开机启动项

参考链接

在添加nginx服务之后,大家会希望开机伴随启动nginx,避免手动路径输入启动;

nginx官方提供了启动脚本:https://www.nginx.com/resources/wiki/start/topics/examples/redhatnginxinit/

我这里使用centos系统,首先,进入/etc/init.d/目录,新添加nginx文件,把上述连接中的内容粘贴到nginx文件中,

[root@fydb02 /]# cd /etc/init.d/

[root@fydb02 init.d]# vi 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 0

nginx="/usr/sbin/nginx"
prog=$(basename $nginx)

NGINX_CONF_FILE="/etc/nginx/nginx.conf"

[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx

lockfile=/var/lock/nginx.lock

make_dirs() {
   # make required directories
   user=`$nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
   if [ -z "`grep $user /etc/passwd`" ]; then
       useradd -M -s /bin/nologin $user
   fi
   options=`$nginx -V 2>&1 | grep 'configure arguments:'`
   for opt in $options; do
       if [ `echo $opt | grep '.*-temp-path'` ]; then
           value=`echo $opt | cut -d "=" -f 2`
           if [ ! -d "$value" ]; then
               # echo "creating" $value
               mkdir -p $value && chown -R $user $value
           fi
       fi
   done
}

start() {
    [ -x $nginx ] || exit 5
    [ -f $NGINX_CONF_FILE ] || exit 6
    make_dirs
    echo -n $"Starting $prog: "
    daemon $nginx -c $NGINX_CONF_FILE
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}

stop() {
    echo -n $"Stopping $prog: "
    killproc $prog -QUIT
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}

restart() {
    configtest || return $?
    stop
    sleep 1
    start
}

reload() {
    configtest || return $?
    echo -n $"Reloading $prog: "
    killproc $nginx -HUP
    RETVAL=$?
    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" in
    start)
        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 2
esac

注意:根据实际情况修改路径

     第一:nginx="/usr/sbin/nginx"
     这里修改成你nginx安装时启动文件放在的路径,即编译时指定的--sbin-path路径
  第二:NGINX_CONF_FILE="/etc/nginx/nginx.conf"

     nginx配置文件路径,即编译时指定的--conf-path路径

    第三: lockfile=/var/lock/nginx.lock     --lock-path路径

  


然后使用chkconfid进行管理,如下

先将nginx服务加入chkconfig管理列表
[root@fydb02 init.d]#chkconfig --add /etc/init.d/nginx
设置开机启动
[root@fydb02 init.d]#chkconfig nginx on


加完上述之后,就可以使用service对nginx进行启动,停止和重启等操作了。

[root@fydb02 init.d]#service nginx start
[root@fydb02 init.d]#service nginx stop
[root@fydb02 init.d]#service nginx reload
[root@fydb02 init.d]#service nginx restart


===============以下情况尚未遇到==========================================

如果在 service nginx start时伴随提示useradd使用有误等提示,那么可以编辑 刚才创建的nginx文件,

定位到 start()方法,把里面的 make_dirs那行注释掉,保存退出,重新启动一次就OK了。


[Note]:

如果在 service nginx start时,出现env: /etc/init.d/nginx: No such file or directory 问题,是因为,

Linux和Window文本文件的行结束标志不一样,Linux下是以"\n"表示回车换行,Windows下是以"\r\n"表示回车换行;

为了解决这错误,Linux提供了两种文本格式的转换命令,dos2unix和unix2dos命令;

dos2unix命令把"\r\n"转换成"\n", unix2dos命令把"\n"转换成"\r\n"。

如果系统没有这两个命令,可以使用#yum -y install dos2unix unix2dos 安装,然后使用#dos2unix /etc/init.d/nginx 即可;

再重新启动service nginx start就OK了。

[root@fydb02 conf]# nginx -V
nginx version: nginx/1.4.2
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-18) (GCC) 
TLS SNI support enabled
configure arguments: --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-http_stub_status_module

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值