Linux 安装 Nginx

安装依赖

安装 gcc
[root@t2 local]# yum install gcc-c++
 
安装 PCRE pcre-devel
[root@t2 local]# yum install -y pcre pcre-devel
 
安装 zlib
[root@t2 local]# yum install -y zlib zlib-devel
 
安装 Open SSL
[root@t2 local]# yum install -y openssl openssl-devel

安装 Nginx

下载

[root@t2 nginx]# wget http://nginx.org/download/nginx-1.23.0.tar.gz

解压

创建文件夹
[root@t2 local]# mkdir nginx

[root@t2 nginx]# cd nginx

解压缩包
[root@t2 nginx]# tar -xvf nginx-1.23.0.tar.gz

安装

进入nginx目录
[root@t2 local]# cd /usr/local/nginx

进入目录
[root@t2 nginx-1.23.0]# cd nginx-1.23.0 

编译 执行命令 考虑到后续安装ssl证书 添加两个模块  如不需要直接执行./configure即可
[root@t2 nginx-1.23.0]# ./configure --with-http_stub_status_module --with-http_ssl_module

执行make命令(要是执行不成功请检查最开始安装的四个依赖有没有安装成功)
[root@t2 nginx-1.23.0]# make

执行make install命令
[root@t2 nginx-1.23.0]# make install

启动

[root@t2 local]# cd /usr/local/nginx/sbin

默认配置文件启动
[root@t2 sbin]# ./nginx

指定配置文件启动
[root@t2 sbin]# ./nginx -c  /usr/local/nginx/conf/nginx.conf

在浏览器中输入服务器 ip 即可看见 Nginx 启动成功.

停止重启

[root@t2 local]# cd /usr/local/nginx/sbin

停止指令
[root@t2 sbin]# ./nginx -s stop

[root@t2 sbin]# ./nginx -s quit

重启命令
[root@t2 sbin]# ./nginx -s reload

查看nginx进程
[root@t2 sbin]# ps -ef|grep nginx

设置开机自启动

#编辑
[root@t2 local]# vim /etc/rc.local
 
最底部增加这一行
/usr/local/nginx/sbin/nginx

配置 SSL 证书

先申请好证书,在/usr/local/nginx/conf/目录下创建文件夹 cert:

[root@t2 conf]# mkdir cert

查看上传的证书
[root@t2 cert]# ll
-rw-r----- 1 root root 4101 Jul  3 17:13 tansci.com_bundle.crt
-rw-r----- 1 root root 4101 Jul  3 17:13 tansci.com_bundle.pem
-rw-r----- 1 root root 1012 Jul  3 17:13 tansci.com.csr
-rw-r----- 1 root root 1702 Jul  3 17:13 tansci.com.key

上传证书文件至 /usr/local/nginx/conf/cert 目录下。

修改 nginx.conf 文件:

    # HTTPS server
    server {
        listen       443 ssl;
        server_name  www.tansci.com;

        # 证书配置
        ssl_certificate      cert/tansci.com_bundle.crt;
        ssl_certificate_key  cert/tansci.com.key;
        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;
        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers  on;

        location / {
            root   /usr/web;
            index  index.html index.htm;
            try_files $uri $uri/ /index.html;
        }

        # 静态文件代理
        location /static {
            root /usr/web;
            autoindex on;
        }

        # api 接口代理
        location /api/ {
            proxy_pass http://localhost:8080/;
            add_header 'Access-Control-Allow-Origin' '*';
            add_header 'Access-Control-Allow-Credentials' 'true';
            add_header 'Access-Control-Allow-Methods' 'GET, POST';
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_set_header X-Forwarded-Port $server_port;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header REMOTE-HOST $remote_addr;
        }
    }

欢迎关注

Linux 安装 Nginx

Linux 安装 Nginx


原文始发于微信公众号(全栈客):Linux 安装 Nginx

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/162571.html

(0)
小半的头像小半

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!