linux环境安装nginx

4 篇文章 0 订阅

步骤:

1. 安装所需环境

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

  yum install gcc-c++

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

  yum install -y pcre pcre-devel

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

  yum install -y zlib zlib-devel

d)安装OpenSSL:OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及 SSL 协议,并提供丰富的应用程序供测试或其它目的使用。nginx 不仅支持 http 协议,还支持 https(即在ssl协议上传输http),所以需要在 Centos 安装 OpenSSL 库

  yum install -y openssl openssl-devel

2. 官网下载安装包 https://nginx.org/en/download.html

   也可以wget命令下载:wget https://nginx.org/download/nginx-1.16.1.tar.gz

3. 将安装包上传到linux服务器,解压

  tar -zxvf nginx-1.16.1.tar.gz

4. 配置、编译、安装

  cd nginx-1.16.1 (进入解压目录,建议解压到/usr/local/nginx)

  -- ./configure (使用默认配置,没有https)

  ./configure --prefix=/usr/local/nginx --conf-path=/usr/local/nginx/nginx.conf --with-http_stub_status_module --with-http_ssl_module(添加https)

  -- 若使用./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module则会报错,添加参数--conf-path=/usr/local/nginx/nginx.conf就ok了

  make (编译)

  make install (安装)

5. 启动nginx

  cd /usr/local/nginx/sbin/

  ./nginx

6. 浏览器输入服务器ip(默认端口为80),访问到nginx起始页,说明nginx安装成功!

 

 7. 其他常用命令

  ./nginx -s stop    等待nginx处理任务完毕,停止nginx进程

  ./nginx -s quit    先查出进程ID,再使用kill命令强制杀掉进程

  ./nginx -s reload   不用重启nginx,使配置文件生效(若修改了nginx.conf)

  ps -ef | grep nginx    查询 nginx 进程

  whereis nginx    查询nginx的安装路径

  ./nginx -t   检查nginx.cnf的语法是否正确

 

补充:

nginx代理静态资源做静态资源服务器时出现403 forbidden:

 

1.nginx.config的user与启动用户不一致:

解决:user root;

改为一致

2.权限问题

解决:修改代理目录的读写权限

chmod -R 775 /目录

3.SELinux设置为开启状态(enabled)的原因

查看当前selinux的状态: /usr/sbin/sestatus

将SELINUX=enforcing 修改为 SELINUX=disabled 状态

4.浏览目录的权限问题

location / {

root /rool/data;

autoindex on;# 开启浏览目录的权限,根据情况选择是否开启

}

 

配置开机启动:

进入到/etc/init.d/目录下,创建文件touch nginx
vi 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: /usr/local/nginx/logs/nginx.pid
    # config: /usr/local/nginx/conf/nginx.conf
     
    nginxd=/usr/local/nginx/sbin/nginx #nginx安装启动文件路径
    nginx_config=/usr/local/nginx/conf/nginx.conf #nginx安装配置文件路径
    nginx_pid=/usr/local/nginx/logs/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

 

保存nginx文件并赋权:

chmod 755 nginx

为nginx加上service相关命令权限: 

chkconfig --add /etc/init.d/nginx

加完这个之后,就可以使用service对nginx进行启动,重启等操作了。

service nginx start
service nginx stop
service nginx restart

最后设置开机自动启动

chkconfig nginx on

reboot测试
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值