CentOS7编译安装Nginx-1.20.1

新手小白需要掌握的Nginx部署技能

这里详细讲下如何在CentOS7环境下编译、安装、部署Nginx。

1.Nginx

维基百科的定义:

Nginx(发音同"engine X")是异步框架的网页服务器,也可以用作反向代理、负载平衡器和HTTP缓存。该软件由伊戈尔·赛索耶夫(Игорь Сысоев)创建并于2004年首次公开发布。2011年成立同名公司以提供支持。2019年3月11日,Nginx公司被F5 Networks以6.7亿美元收购。

Nginx是免费的开源软件,根据类BSD许可证的条款发布。一大部分Web服务器使用Nginx,通常作为负载均衡器。

2.系统依赖库安装

请使用root用户安装Nginx所需要的依赖库

# yum install -y gcc gcc-c++ pcre-devel openssl-devel

3.下载Nginx

请在官网地址http://nginx.org/en/download.html进行下载

$ mkdir bm
$ cd bm
$ wget http://nginx.org/download/nginx-1.20.1.tar.gz

4.编译Nginx

  • 解压编译
$ tar zxf nginx-1.20.1.tar.gz
$ cd nginx-1.20.1/
$ ./configure --prefix=/home/testerzhang/nginx --with-http_stub_status_module --with-http_ssl_module

如果你需要其他模块,比如http_v2_module模块

$ ./configure --prefix=/home/testerzhang/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module
  • 编译安装
$ make
$ make install

这时候我们就可以在安装目录查看到编译好的文件了。

  • 删掉安装目录的html文件夹下的文件
$ cd /home/testerzhang/nginx/html
$ rm *.html

5.打包编译好的Nginx目录

这里打包最原始的配置,当然如果你要提前修改Nginx配置文件,再打包也是可以的。

$ cd ~/
$ tar zcf nginx.tar.gz nginx

然后你就可以拷贝到你想要部署的机器。

6.新机器部署

  • 在你想要部署的目录,解压压缩包
$ tar zxf nginx.tar.gz
$ cd nginx
  • 修改配置文件
$ vim conf/nginx.conf

这里列举下需要修改的常规配置

# 由于部署的机器不是按照编译打包的账号来的,所以这里要配置实际部署机器的绝对路径。
error_log  /opt/testerzhang/nginx/logs/error.log;

http {
    ...

    # 设置访问日志信息
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /opt/testerzhang/nginx/logs/access.log  main;


    # 隐藏nginx版本
    server_tokens off;
    # 隐藏后端服务信息
    proxy_hide_header Server;
    ...


    # 这里设置端口80的端口配置,并自动重定向到https的网站地址。
    server {
        listen       80 ;
        server_name  www.xxx.top;

        access_log  /opt/testerzhang/nginx/logs/80.access.log  main;

				# 限制IP访问,前提对应配置的文件要存在
        #include blockip.conf;

        return 301 https://www.xxx.top;
    }


     server {
        listen       443 ssl ;
        server_name  www.qahome.top;

        access_log  /opt/testerzhang/nginx/logs/443.access.log  main;

			  # 限制IP访问,前提对应配置的文件要存在
        #include blockip.conf;

				# 添加同源策略,防止跨站脚本,禁止某些浏览器的内容类型探嗅
        add_header X-Frame-Options "SAMEORIGIN";
        add_header X-XSS-Protection "1; mode=block";
        add_header X-Content-Type-Options "nosniff";

    		# SSL证书的配置
        ssl_certificate      /opt/testerzhang/zhengshu/1_www.xxx.top_bundle.crt;
        ssl_certificate_key  /opt/testerzhang/zhengshu/2_www.xxx.top.key;

        ssl_protocols TLSv1.2;

        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;

        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
        ssl_prefer_server_ciphers on;

        # 根目录对应的静态文件目录
        location / {
            root   /opt/testerzhang/website;
            index  index.html index.htm;
        }

        # 转发后端请求
        #location /backend {
        #    proxy_set_header  Host  $host:$proxy_port;
        #    proxy_set_header  X-Real-IP  $remote_addr;
        #    proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
        #
        #    proxy_pass http://10.10.10.10:58080;
        #}

        ...
    }
}



7.启停命令

正常在编译后的安装目录,不需要指定配置文件,但是由于是部署新机器,可能不是同一个账号,你需要加上绝对路径的配置文件

  • 启动
$ ./nginx -c /opt/testerzhang/nginx/conf/nginx.conf
  • 停止
$ ./nginx -c /opt/testerzhang/nginx/conf/nginx.conf -s stop
  • 重新加载Nginx配置
$ ./nginx -c /opt/testerzhang/nginx/conf/nginx.conf -s reload

本文没有授权给任何组织、企业和个人转载,未经作者允许禁止转载!

欢迎关注我的公众号testerzhang,原创技术文章第一时间推送。

公众号二维码

updatedupdated2021-06-212021-06-21