一文解决Linux安装Nginx以及常用的配置

服务器:CentOS 8.0 64bit

1.安装编译⼯具
[root@VM-16-6-centos local]# yum install -y gcc gcc-c++
2.安装PCRE
# 1.下载
[root@VM-16-6-centos local]# wget http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz
# 2.解压
[root@VM-16-6-centos local]# tar -zxvf pcre-8.35.tar.gz
# 3.进⼊pcre⽬录
[root@VM-16-6-centos local]# cd pcre-8.35
# 4.配置
[root@VM-16-6-centos pcre-8.35]# ./configure
# 5.编译安装
[root@VM-16-6-centos pcre-8.35]# make && make install
3.安装SSL库
[root@VM-16-6-centos local]# wget http://www.openssl.org/source/openssl-1.0.1j.tar.gz
[root@VM-16-6-centos local]# tar -zxvf openssl-1.0.1j.tar.gz
[root@VM-16-6-centos local]# cd openssl-1.0.1j
[root@VM-16-6-centos openssl-1.0.1j]# ./config
[root@VM-16-6-centos openssl-1.0.1j]# make && make install
4.安装zlib库
[root@VM-16-6-centos local]# wget http://www.zlib.net/fossils/zlib-1.2.11.tar.gz
[root@VM-16-6-centos local]# tar -zxvf zlib-1.2.11.tar.gz
[root@VM-16-6-centos local]# cd zlib-1.2.11
[root@VM-16-6-centos zlib-1.2.11]# ./configure
[root@VM-16-6-centos zlib-1.2.11]# make && make install
5.安装Nginx
[root@VM-16-6-centos local]# wget http://nginx.org/download/nginx-1.16.1.tar.gz
[root@VM-16-6-centos local]# tar -zxvf nginx-1.16.1.tar.gz
[root@VM-16-6-centos local]# mkdir -p server/nginx
[root@VM-16-6-centos local]# cd nginx-1.16.1
[root@VM-16-6-centos nginx-1.16.1]# ./configure --prefix=/usr/local/server/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/usr/local/pcre-8.35
[root@VM-16-6-centos nginx-1.16.1]# make && make install

# 如果在配置过程中出现了以下错误:
./configure: error: SSL modules require the OpenSSL library.
You can either do not enable the modules, or install the OpenSSL library
into the system, or build the OpenSSL library statically from the source
with nginx by using --with-openssl=<path> option.
# 执⾏:yum -y install openssl openssl-devel
6.配置Nginx
[root@VM-16-6-centos local]# vim /usr/local/server/nginx/conf/nginx.conf
7.启动/关闭Nginx
[root@VM-16-6-centos local]# cd /usr/local/server/nginx/sbin
# 启动
[root@VM-16-6-centos sbin]# ./nginx
# 关闭
[root@VM-16-6-centos sbin]# ./nginx -s stop
8.配置开机自启

8.1 先创建开机自启脚本

cd /etc/systemd/system
vi nginx.service

# 内容:
[Unit]
Description=nginx service
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true

[Install]
WantedBy=multi-user.target

8.2 设置开机自启动

systemctl enable nginx

8.3 启动nginx服务

systemctl start nginx.service

8.4 重新启动服务

systemctl restart nginx.service

8.5 查看服务当前状态

systemctl status nginx.service

8.6 停止开机自启动

systemctl disable nginx.service
扩展:
9.Nginx 服务器 SSL 证书安装部署

9.1 下载服务商提供的SSL证书

9.2 上传至Nginx服务器的conf目录下

9.3 编辑 Nginx 根目录下的 conf/nginx.conf 文件

# 修改内容
server {
        #SSL 访问端口号为 443
        listen 443 ssl; 
        #填写绑定证书的域名
        server_name chenxp.top; 
        #证书文件名称
        ssl_certificate chenxp.top_bundle.crt; 
        #私钥文件名称
        ssl_certificate_key chenxp.top.key; 
        ssl_session_timeout 5m;
        #请按照以下协议配置
        ssl_protocols TLSv1.2 TLSv1.3; 
        #请按照以下套件配置,配置加密套件,写法遵循 openssl 标准。
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE; 
        ssl_prefer_server_ciphers on;
        location / {
           #网站主页路径。此路径仅供参考,具体请您按照实际目录操作。
           #例如,您的网站运行目录在/etc/www下,则填写/etc/www。
            root html; 
            index  index.html index.htm;
        }
    }

9.4 在 Nginx 根目录下,通过执行以下命令验证配置文件问题。

[root@VM-16-6-centos sbin]# ./nginx -t

9.5 重启Nginx,即可使用 https访问

[root@VM-16-6-centos sbin]# ./nginx -s reload
10.配置gzip压缩

10.1 编辑 Nginx 根目录下的 conf/nginx.conf 文件

[root@VM-16-6-centos ~]# cd /usr/local/server/nginx/conf
[root@VM-16-6-centos conf]# vim nginx.conf

#在http{}下添加以下内容
#是否启动gzip压缩,on代表启动,off代表开启
gzip  on; 
#需要压缩的常见静态资源
gzip_types text/plain application/javascript   application/x-javascript text/css application/xml text/javascript application/x-httpd-php;
#由于nginx的压缩发生在浏览器端而微软的ie6很坑爹,会导致压缩后图片看不见所以该选项是禁止ie6发生压缩
gzip_disable "MSIE [1-6]\.";
#如果文件大于1k就启动压缩
gzip_min_length 1k;
#以16k为单位,按照原始数据的大小以4倍的方式申请内存空间,一般此项不要修改
gzip_buffers 4 16k;
#压缩的等级,数字选择范围是1-9,数字越小压缩的速度越快,占用cpu也越大
gzip_comp_level 3;

10.2 重启Nginx服务

[root@VM-16-6-centos conf]# cd /usr/local/server/nginx/sbin
[root@VM-16-6-centos sbin]# ./nginx -t
[root@VM-16-6-centos sbin]# ./nginx -s reload

10.3 检验是否开启成功

通过控制台查看打开响应头中的Content-Encoding选项,如果出现gzip,则开启成功。

image-20220524123921195

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿四i

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值