玩转Linux系统【五】安装Nginx搭建网站

本文涉及的产品
.cn 域名,1个 12个月
简介: Nginx("engine x")是一款是由俄罗斯的程序设计师Igor Sysoev所开发高性能的 Web和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器。在高连接并发的情况下,Nginx是Apache服务器不错的替代品。

image.png

安装下载

下载

下载后上传至CentOS下的/usr/local目录下

  • wget直接下载

    $ cd /usr/local
    $ wget http://nginx.org/download/nginx-1.17.1.tar.gz

安装

  1. 解压安装包

    $ tar -zxvf nginx-1.17.1.tar.gz

    注意:nginx被解压到了/usr/local/nginx-1.17.1 目录下(不要把压缩包解压到/usr/local/nginx目录下,或者将解压后的目录重命名为nginx,因为nginx会默认安装到/usr/local/nginx目录下)

  2. 安装前准备

    安装前先安装nginx所需的依赖库,如果缺少依赖库,可能会安装失败

    $ yum install gcc-c++
    $ yum install pcre
    $ yum install pcre-devel
    $ yum install zlib 
    $ yum install zlib-devel
    $ yum install openssl
    $ yum install openssl-devel
  3. 进入nginx-1.17.1目录,并执行以下配置命令

    $ cd nginx-1.17.1
    $ ./configure

    configure操作会检测当前系统环境,以确保能成功安装nginx,如果出错,请检查上述安装前依赖包是否已经安装

    如果出现如下信息表示你需要安装依赖库

    • gcc库未安装提示

      checking for OS
           + Linux 3.10.0-123.el7.x86_64 x86_64
          checking for C compiler ... not found
          ./configure: error: C compiler cc is not found
    • PCRE库未安装提示

       ./configure: error: the HTTP rewrite module requires the PCRE library.
          You can either disable the module by using --without-http_rewrite_module
          option, or install the PCRE library into the system, or build the PCRE library
          statically from the source with nginx by using --with-pcre=<path> option.
    • zlib库未安装提示

       ./configure: error: the HTTP gzip module requires the zlib library.
          You can either disable the module by using --without-http_gzip_module
          option, or install the zlib library into the system, or build the zlib library
          statically from the source with nginx by using --with-zlib=<path> option.

    如果没有其他错误,则可进行下一步

  4. 执行make安装

    注意:下面2步会将nginx安装到/usr/local/nginx目录下,所以请勿占用nginx目录命名
    # make
    $ make install

    如果上述2步操作未报错,你可以切换到上一级目录,会发现nginx目录已经存在了

    $ cd ..
    $ cd nginx
    $ ls
  5. 配置nginx开机启动

    切换到/lib/systemd/system/目录,创建nginx.service文件vim nginx.service

    $ cd /lib/systemd/system/
    $ vim nginx.service

    添加如下内容

    [Unit]
    Description=nginx 
    After=network.target 
       
    [Service] 
    Type=forking 
    ExecStart=/usr/local/nginx/sbin/nginx
    ExecReload=/usr/local/nginx/sbin/nginx reload
    ExecStop=/usr/local/nginx/sbin/nginx quit
    PrivateTmp=true 
       
    [Install] 
    WantedBy=multi-user.target

    退出并保存文件,执行如下名使nginx开机启动

    $ systemctl enable nginx.service
    # 启动nginx
    $ systemctl start nginx.service
    
    # 结束nginx
    $ systemctl stop nginx.service
    
    # 重启nginx
    $ systemctl restart nginx.service
  6. 验证是否安装成功

    # 你要将80端口添加到防火墙中
    $ firewall-cmd --zone=public --add-port=80/tcp --permanent
    #重新加载
    $ firewall-cmd --reload
    
    在浏览器访问如下地址
    http://192.168.2.204/

    image.png

云上环境ECS

让外网通过ip+端口的方式访问

以下内容是基于我提供了一个7777端口的服务,需要开启一个7777端口的web服务后进行,或者将下方的7777端口改为nginx默认主页的80端口也可以

  • 进入服务器安全组
    image.png

    image.png

  • 添加7777端口为0.0.0.0/0

    image.png

  • 外网通过ip + 端口的方式访问,查看效果

    image.png

Nginx域名http版配置

  1. nginx配置ssl

    # 进入/usr/local/nginx/conf/目录
    $ vim nginx.conf
    
    # 在最后一行大括号结束之前,加入以下配置,并将it235换成你的域名
        # http域名配置
        server {
            listen 80;
            server_name www.it235.com it235.com;
            
            # 域名默认映射到 http://127.0.0.1:7777
            location / {
                   proxy_pass http://127.0.0.1:7777;
                   proxy_redirect off;
                   proxy_set_header Host $host;
                   proxy_set_header X-Real-IP $remote_addr;
                   proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                   proxy_set_header Upgrade $http_upgrade;
                   proxy_set_header Connection "upgrade";
    
                   root html;
                   index index.html index.html;
            }
        
        }
  2. 重新加载nginx配置文件

    # 验证nginx配置文件是否正确
    $ /usr/local/nginx/sbin/nginx -t
    nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
    nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
      
    # 重新加载nginx配置文件,重新加载才会生效
    $ /usr/local/nginx/sbin/nginx -s reload
  3. 阿里云安全组配置80端口为0.0.0.0/0
  4. 还需要去做域名解析

    • A记录
      主要关注A记录,设置到一个具体的服务器IP上(一定是公网IP,192.168.1.3)
    • CNAME记录

SSL证书(https)配置

  1. 提供SSL证书服务
    在配置了SSL证书后,重新启动报如下错误,所以需要提前安装SSL模块

    [root@iz2zehvxttbua2f45dp7ihz sbin]# ./nginx -s reload
    nginx: [emerg] the "ssl" parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/object.nginx.conf:14

    表示当前Nginx缺少SSL模块,我们可以查看nginx版本

    [root@iz2zehvxttbua2f45dp7ihz sbin]# ./nginx -V
    nginx version: nginx/1.17.1
    built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC) 
    configure arguments:

    如果带有SSL模块,在上面输出的信息最后一行应该有如下参数,如下图

    --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module

    image.png

    那应该怎么添加中呢?请看如下操作

  2. 添加SSL模块

    • 切换到源码安装包,就是带版本的那个nginx文件夹

      # cd /usr/local/nginx-1.17.1
    • 执行如下命令

      # ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
    • 配置完成后执行如下命令

      # make
      # 切忌,只执行make,不要执行make install,否则会覆盖
    • 停止服务,备份原来的nginx.sh文件

      # systemctl stop nginx.service
      # cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak
    • 将源码包中的nginx文件覆盖到正在使用的nginx执行文件

      # cp -f /usr/local/nginx-1.17.1/objs/nginx /usr/local/nginx/sbin/nginx
    • 重启并查看版本

      # systemctl start nginx.service
      # /usr/local/nginx/sbin/nginx -V

      image.png

  • 阿里云免费证书
    申请后可以下载,然后上传到nginx目录下做映射
    image.png

    image.png

  • Let's Encrypt免费证书,一次申请只能使用3个月,到期后需要手动续期
  • Nginx配置证书

    # 进入/usr/local/nginx/conf/目录
    $ vim nginx.conf
    
    # 在最后一行大括号结束之前,加入以下配置,并将it235换成你的域名
        # 所有http的请求,统一发到https请求上
        server {
            listen 80;
            server_name www.it235.com *.it235.com;
            rewrite ^(.*)$  https://$host$1 permanent;
        }
    
        # 未带www的请求,统一分发到https://www上
        server {
            listen 80;
            listen 443 ssl;
            server_name  it235.com;
            return 301 https://www.it235.com$request_uri;
        }
    
        # https 请求处理
        server {
            listen 443 default_server ssl;
            server_name  www.it235.com;
            ssl_certificate /etc/letsencrypt/live/it235.com/fullchain.pem;
            ssl_certificate_key /etc/letsencrypt/live/it235.com/privkey.pem;
    
            large_client_header_buffers 4 16k;
            client_max_body_size 30m;
            client_body_buffer_size 128k;
    
            # 域名默认映射到 http://127.0.0.1:7777
            location / {
                   proxy_pass http://127.0.0.1:7777;
                   proxy_redirect off;
                   proxy_set_header Host $host;
                   proxy_set_header X-Real-IP $remote_addr;
                   proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                   proxy_set_header Upgrade $http_upgrade;
                   proxy_set_header Connection "upgrade";
    
                   root html;
                   index index.html index.html;
    
            }
        }
    # 验证nginx配置文件是否正确
    $ /usr/local/nginx/sbin/nginx -t
    
    # 重新加载nginx配置文件,重新加载才会生效
    $ /usr/local/nginx/sbin/nginx -s reload
  • 最终效果,访问https://www.it235.com要能成功,并且加上锁
目录
相关文章
|
8天前
|
NoSQL 关系型数据库 MySQL
linux服务器重启php,nginx,redis,mysql命令
linux服务器重启php,nginx,redis,mysql命令
16 1
|
1月前
|
Ubuntu 应用服务中间件 Linux
Linux Centos7 ubuntu 安装nginx,脚本一键安装nginx
Linux Centos7 ubuntu 安装nginx,脚本一键安装nginx
52 2
|
15天前
|
应用服务中间件 Linux 网络安全
LINUX安装nginx详细步骤
LINUX安装nginx详细步骤
|
25天前
|
应用服务中间件 Linux nginx
蓝易云 - Linux使用pid文件结束nginx
以上就是使用pid文件结束nginx进程的方法。
14 0
|
1月前
|
Linux 应用服务中间件 开发工具
centos linux 通过yum安装nginx
centos linux 通过yum安装nginx
129 0
|
2月前
|
Ubuntu 应用服务中间件 Linux
蓝易云 - Linux学习之Ubuntu20中OpenResty的nginx目录里内容和配置文件
你可以根据你的需要修改这个配置文件,例如增加新的服务器块,位置块,修改监听的端口等。修改完配置文件后,你需要重载Nginx配置,可以使用 `/usr/local/openresty/nginx/sbin/nginx -s reload`命令来实现。
28 0
|
2月前
|
Linux Shell 数据库
linux系统 安装、管理程序
linux系统 安装、管理程序
|
2月前
|
应用服务中间件 Linux nginx
蓝易云 - linux查看正在运行的nginx在哪个文件夹当中
请注意,这些步骤可能需要root权限才能执行。如果你没有root权限,你可能需要使用 `sudo`命令。
34 0
|
2月前
|
Linux Docker 容器
Linux系统安装微信和企微(debian)
Linux系统安装微信和企微(debian)
363 0
|
2月前
|
负载均衡 网络协议 应用服务中间件
【亮剑】在Linux中构建高可用性和高性能网络服务的负载均衡工具HAProxy、Nginx和Keepalived。
【4月更文挑战第30天】本文介绍了在Linux中构建高可用性和高性能网络服务的负载均衡工具HAProxy、Nginx和Keepalived。HAProxy是一个高性能的开源TCP和HTTP负载均衡器,适合处理大量并发连接;Nginx是一个多功能Web服务器和反向代理,支持HTTP、HTTPS和TCP负载均衡,同时提供缓存和SSL功能;Keepalived用于监控和故障切换,通过VRRP实现IP热备份,保证服务连续性。文中详细阐述了如何配置这三个工具实现负载均衡,包括安装、配置文件修改和启动服务,为构建可靠的负载均衡系统提供了指导。