Linux安装Nginx

原创
2019/02/26 09:50
阅读数 158

Nginx安装环境

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

gcc

安装nginx需要先将官网下载的源码进行编译,编译依赖gcc环境,如果没有gcc环境,需要安装gcc:

yum install gcc-c++ 

pcre

PCRE(Perl Compatible Regular Expressions)是一个Perl库,包括 perl 兼容的正则表达式库。nginx的http模块使用pcre来解析正则表达式,所以需要在linux上安装pcre库。

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

zlib

zlib库提供了很多种压缩和解压缩的方式,nginx使用zlib对http包的内容进行gzip,所以需要在linux上安装zlib库。

yum install -y zlib zlib-devel

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
    
  2. 创建Makefile,把下面的命令复制到命令行中就会自动创建Makefile:

    ./configure  
     --group=nginx  
     --user=nginx  
     --prefix=/usr/local/nginx  
     --sbin-path=/usr/sbin/nginx  
     --conf-path=/etc/nginx/nginx.conf  
     --error-log-path=/var/log/nginx/error.log  
     --http-log-path=/var/log/nginx/access.log  
     --http-client-body-temp-path=/tmp/nginx/client_body  
     --http-proxy-temp-path=/tmp/nginx/proxy  
     --http-fastcgi-temp-path=/tmp/nginx/fastcgi  
     --pid-path=/var/run/nginx.pid  
     --lock-path=/var/lock/nginx  
     --with-http_stub_status_module  
     --with-http_ssl_module  
     --with-http_gzip_static_module  
     --with-pcre 
     --with-stream
    
    • 注意:上边将临时文件目录指定为/var/temp/nginx,需要在/var下创建temp及nginx目录
    • 编译及安装,在上边的--prefix=/usr/local/nginx \指定了安装的目录:
    make
    make install
    
    • nginx的可执行文件在nginx–>sbin下的nginx文件。
  3. 启动Nginx 启动nginx,进入sbin目录./nginx

    [root@niaoyun49938 sbin]# ./nginx
    

    判断是否启动成功,在浏览器中输入你的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
    

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

  4. 停止Nginx

    • 方式一,快速停止
      ./nginx -s stop
      

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

    • 方式二,完整停止
      ./nginx -s quit
      

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

  5. 重启Nginx

    • 方式一,先停止再启动
      ./nginx -s quit
      ./nginx
      

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

    • 方式二,重新加载配置文件
      ./nginx -s reload
      

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

开机自启动Nginx

编写shell脚本

  • 这里使用的是编写shell脚本的方式来处理,编辑nginx文件:
    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 保存并退出。

设置文件的访问权限

chmod a+x /etc/init.d/nginx   (a+x ==> all user can execute  所有用户可执行)
  • 这样在控制台就很容易的操作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]# 
  • 如果修改了nginx的配置文件nginx.conf,也可以使用上面的命令重新加载新的配置文件并运行,可以将此命令加入到rc.local文件中,这样开机的时候nginx就默认启动了

加入到rc.local文件中

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

  • chkconfig nginx on

小记

Nginx错误-[emerg] getpwnam (“nginx”) failed

出现这个问题的原因是没有用户被创建,创建相应的用户即可

[root@localhost nginx-1.11.2]# useradd -s /sbin/nologin -M nginx
[root@localhost nginx-1.11.2]# id nginx

Nginx的status错误403解决方案

导致 403 Forbidden错误的原因是配置文件中没有指明一个用户,需要的朋友可以参考下

403的问题都是权限问题,看看目录权限吧

Nginx转发TCP

nginx1.90对TCP协议的代理并不是默认开启的,需要在编译的时候配置 --with-stream 参数

stream {
    upstream proxy_card {
        # simple round-robin  转发IP和端口
        server 192.168.1.12:12340;
        server 192.168.1.13:12340;
        #check interval=3000 rise=2 fall=5 timeout=1000;
        #check interval=3000 rise=2 fall=5timeout=1000
        #check interval=3000 rise=2 fall=5timeout=1000
        #check_http_send "GET /HTTP/1.0\r\n\r\n";
        #check_http_expect_alive http_2xxhttp_3xx;
    }
    server {
        listen 12340; #监听端口
        proxy_pass proxy_card;  #转发请求
    }
}
展开阅读全文
加载中
点击引领话题📣 发布并加入讨论🔥
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部