程子的博客

想得到从未得到的东西,就要去做从未做过的事

aaaaa
  menu
46 文章
23633 浏览
0 当前访客
ღゝ◡╹)ノ❤️

Linux 安装 nginx

Linux 安装 nginx

1. 安装依赖库

yum -y install make zlib zlib-devel gcc-c++ libtool  openssl openssl-devel

2. 安装正则表达式库

#下载 pcre 安装包到当前目录
wget https://jaist.dl.sourceforge.net/project/pcre/pcre/8.44/pcre-8.44.tar.gz
#解压缩
tar -xvf pcre-8.42.tar.gz
#进入目录
cd pcre-8.44 
#安装编译
./configure
make && make install
#查看 pcre 版本
pcre-config --version

3. 安装nginx

#下载 nginx 安装包到当前目录
wget https://nginx.org/download/nginx-1.16.1.tar.gz
#解压缩
tar -xvf nginx-1.16.1.tar.gz
#进入目录
cd nginx-1.16.1
#编译安装
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/usr/local/pcre/pcre-8.44
make && make install
#查看版本
/usr/local/nginx/sbin/nginx -v

4. 配置

#创建用户组
/usr/sbin/groupadd nginx
#在用户组创建用户
/usr/sbin/useradd -g nignx nignx
  • 配置conf
cd /usr/local/nginx

#创建 conf.d 文件夹(存放配置)
mkdir conf.d
#创建 cert 文件夹(存放ssl证书)
mkdir cert

cd /usr/local/nginx/conf
#配置 nginx.conf
vim nginx.conf
#打开 user 注解, 用户改为 nginx
#在 server 上添加引入 conf 文件
include /usr/local/nginx/conf/conf.d/*.conf;
  • 新增test.conf 配置
cd conf.d
vim test.conf

#添加配置
server {
        listen       80;
        server_name  chengzime.com.cn;
        rewrite ^(.*)$ https://$host$1 permanent;
}
server {
        listen 443 ssl;  # ssl on; 报错时把 ssl 写在 443 后面
        server_name chengzime.com.cn;
        ssl_certificate /usr/local/epod/nginx/conf/cert/4546333_chengzime.com.cn.pem;
        ssl_certificate_key /usr/local/epod/nginx/conf/cert/4546333_chengzime.com.cn.key;
        ssl_session_timeout 5m;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;  #按照这个套件配置
        ssl_prefer_server_ciphers on;

  
        location / {
            root   /usr/local/chengzi/text;
            index  index.html index.htm;
        }


        location ^~ /api { 
	  proxy_pass http://localhost:8080;
	  proxy_set_header Host $host;
          proxy_set_header  X-Real-IP  $remote_addr;
          proxy_set_header X-Forwarded-For $remote_addr;
          #proxy_set_header Host $host:$server_port;
          proxy_set_header X-Forwarded-Proto $scheme;
          proxy_set_header X-Forwarded-Port $server_port;
        }
}

5. 启动

cd /usr/local/nginx/sbin/
#验证脚本是否正确
./nginx -t
#表示正确
[root@iZ sbin]# ./nginx -t
nginx: the configuration file /usr/local/epod/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/epod/nginx/conf/nginx.conf test is successful

#运行nginx
./nginx
#重新加载配置
./nginx -s reload
#停止nginx
./nginx -s stop

标题:Linux 安装 nginx
作者:chengzime
地址:https://chengzime.com.cn/articles/2020/09/25/1601019067874.html