新手学Linux----安装Nginx

Nginx安装环境


    nginx是C语言开发,建议在linux上运行,本教程使用Centos7.0作为安装环境。

  • gcc 
    安装nginx需要先将官网下载的源码进行编译,编译依赖gcc环境,如果没有gcc环境,需要安装gcc:
yum install gcc-c++ 
  • 1
  • pcre 
    PCRE(Perl Compatible Regular Expressions)是一个Perl库,包括 perl 兼容的正则表达式库。nginx的http模块使用pcre来解析正则表达式,所以需要在linux上安装pcre库。
yum install -y pcre pcre-devel
  • 1

    注:pcre-devel是使用pcre开发的一个二次开发库。nginx也需要此库。

  • zlib 
    zlib库提供了很多种压缩和解压缩的方式,nginx使用zlib对http包的内容进行gzip,所以需要在linux上安装zlib库。
yum install -y zlib zlib-devel
  • 1
  • openssl 
    OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及SSL协议,并提供丰富的应用程序供测试或其它目的使用。 
    nginx不仅支持http协议,还支持https(即在ssl协议上传输http),所以需要在linux安装openssl库。 
yum install -y openssl openssl-devel
  • 1

    

编译安装


    将nginx-1.8.0.tar.gz拷贝至linux服务器。解压:

//分配权限,不然的话没有权限解压
chmod -R 777 nginx-1.8.0.tar.gz
tar -zxvf nginx-1.8.0.tar.gz
cd nginx-1.8.0
  • 1
  • 2
  • 3
  • 4
  • 5

    创建Makefile,把下面的命令复制到命令行中就会自动创建Makefile:

./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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

    注意:上边将临时文件目录指定为/var/temp/nginx,需要在/var下创建temp及nginx目录 
    编译及安装,在上边的--prefix=/usr/local/nginx \指定了安装的目录:

make
make install
  • 1
  • 2

    nginx的可执行文件在nginx–>sbin下的nginx文件。

    

启动Nginx


    启动nginx,进入sbin目录./nginx

[root@niaoyun49938 sbin]# ./nginx
  • 1
  • 2

    判断是否启动成功,在浏览器中输入你的IP地址,nginx默认端口是80,出现下面的界面表示启动成功。

这里写图片描述

    查看nginx的进程:

[root@niaoyun49938 sbin]# ps -ef|grep nginx
root      93542      1  0 14:51 ?        00:00:00 nginx: master process ./nginx
nobody    93543  93542  0 14:51 ?        00:00:00 nginx: worker process
  • 1
  • 2
  • 3
  • 4

    93542是nginx的主进程的进程Id,93543是nginx的工作进程的进程Id。

    

停止Nginx


方式一,快速停止

./nginx -s stop
  • 1

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

方式二,完整停止

./nginx -s quit
  • 1

    完整停止(建议使用),此方式停止步骤是待nginx进程处理任务完毕进行停止。 
    

重启Nginx


方式一,先停止再启动

./nginx -s quit
./nginx
  • 1
  • 2

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

方式二,重新加载配置文件


./nginx -s reload
  • 1
  • 2
  • 3

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

    

开机自启动Nginx


编写shell脚本

    这里使用的是编写shell脚本的方式来处理,编辑nginx文件:

vi /etc/init.d/nginx
  • 1

    复制一下代码:

#!/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

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76

    :wq 保存并退出。

设置文件的访问权限

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

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

[root@niaoyun49938 init.d]# ./nginx status
./nginx: line 1: nx: command not found
./nginx: line 19: [: =: unary operator expected
nginx is stopped
[root@niaoyun49938 init.d]# ./nginx stop
./nginx: line 1: nx: command not found
./nginx: line 19: [: =: unary operator expected
Stopping nginx:                                            [FAILED]
[root@niaoyun49938 init.d]# ./nginx start
./nginx: line 1: nx: command not found
./nginx: line 19: [: =: unary operator expected
Starting nginx:                                            [  OK  ]
[root@niaoyun49938 init.d]# ./nginx restart
./nginx: line 1: nx: command not found
./nginx: line 19: [: =: unary operator expected
Stopping nginx:                                            [  OK  ]
Starting nginx:                                            [  OK  ]
[root@niaoyun49938 init.d]# 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

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

加入到rc.local文件中

vi /etc/rc.local
  • 1

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


猜你喜欢

转载自blog.csdn.net/a694704123b/article/details/79855605
今日推荐