nginx安装与插件

1,296 阅读4分钟

1.nginx安装

1.1 linux

#查看gcc版本 
gcc -v
#安装gcc 
yum -y install gcc
#pcre是一个perl库,包括perl兼容的正则表达式库,nginx的http模块使用pcre来解析正则表达式,所以需要安装pcre库
yum install -y pcre pcre-devel
#zlib安装
yum install -y zlib zlib-devel
#安装openssl
yum install -y openssl openssl-devel
#下载nginx安装包
wget http://nginx.org/download/nginx-1.15.8.tar.gz  

# 解压文件
tar -zxvf  nginx-1.15.8.tar.gz 
# 创建 /usr/local/download 文件夹
mkdir /usr/local/download
# 剪切文件夹到 /usr/local/download/ 下
mv nginx-1.15.8 /usr/local/download/
#切换到 /usr/local/download/nginx-1.15.8/下面
cd /usr/local/download/nginx-1.15.8/

# 执行命令
./configure
make
make install

#查看nginx是否正常安装
whereis nginx 

#切换到/usr/local/nginx安装目录
cd /usr/local/nginx/conf
#配置nginx的配置文件nginx.conf文件,主要也就是端口
vim nginx.conf

# 创建 错误日志文件夹
mkdir /var/log/nginx/

#nginx常用命令
#先进入nginx目录
cd /usr/local/nginx/sbin
#启动服务
./nginx
#测试配置是否正常
./nginx -t
#重新加载
./nginx -s reload
#关闭服务
./nginx -s stop

查看nginx服务是否启动成功
ps -ef | grep nginx

1.2 window

# 下载安装包 到d盘
http://nginx.org/en/download.html
# 解压

# 启动nginx 方式1 直接双击
# 启动nginx 方式2 cmd命令执行
# cd 进入文件夹 
cd d:
cd nginx-1.20.2 
nginx.exe # 启动nginx  注意 .exe可以忽略

# 查看端口情况
netstat -ano | findstr 0.0.0.0:80 
netstat -ano | findstr "80"

#启动服务
./nginx
#测试配置是否正常
./nginx -t
#重新加载
./nginx -s reload
#关闭服务
./nginx -s stop

2.nginx插件安装

1.https插件

如果未安装插件提示如下 ,未安装SSL模块--ngx_http_ssl_module

nginx: [emerg] the "ssl" parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/nginx.c

安装流程

# 1.先查看nginx原有的模块
/usr/local/nginx/sbin/nginx -V   #./nginx -V

# 2.在configure arguments:后面显示的参数如下:
--prefix=/usr/local/nginx --with-http_stub_status_module #则代表未安装SSL模块
# 那么安装好的参数应该是这样
yum -y install openssl openssl-devel  #非必须,安装ssl插件
#在原来下载的nginx 目录下执行 http_stub_status_module用于查看nginx当前状态
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module

# 3.make生成
make #这里不要进行make install,否则就是覆盖安装

# 4.然后备份原有已安装好的nginx
cp /usr/local/nginx/sbin/nginx  /usr/local/nginx/sbin/nginx.bak

# 5.然后将刚刚编译好的nginx覆盖掉原有的nginx(这个时候nginx要停止状态)
# 先停止nginx
/usr/local/nginx/sbin/nginx -s stop
# 再进行覆盖
cp ./objs/nginx /usr/local/nginx/sbin/
# 然后启动nginx,仍可以通过命令查看是否已经加入成功
/usr/local/nginx/sbin/nginx -V

# 6.配置信息指向-调整
# 如果无法停止nginx 可能配置指向已经变化,需要重现调整回原来自己配置的路径
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

2.echo插件

1.下载

# 1.下载
cd /usr/local/
wget https://gitee.com/mjsong/echo-nginx-module/repository/archive/master.zip
yum install unzip # 如果之前没有安装zip解压
unzip master.zip
mv echo-nginx-module-master echo-nginx-module

# 2.配置
#在原来下载的nginx 目录下执行
./configure --prefix=/usr/local/nginx --add-module=/usr/local/echo-nginx-module

# 3.make生成
make #这里不要进行make install,否则就是覆盖安装

# 4.然后备份原有已安装好的nginx
cp /usr/local/nginx/sbin/nginx  /usr/local/nginx/sbin/nginx.bak

# 5.然后将刚刚编译好的nginx覆盖掉原有的nginx(这个时候nginx要停止状态)
# 先停止nginx
/usr/local/nginx/sbin/nginx -s stop
# 再进行覆盖
cp ./objs/nginx /usr/local/nginx/sbin/
# 然后启动nginx,仍可以通过命令查看是否已经加入成功
/usr/local/nginx/sbin/nginx -V

3.集成goaccess

cd /usr/local/src/
# 下载
wget https://tar.goaccess.io/goaccess-1.3.tar.gz
# 解压
tar -xzvf goaccess-1.3.tar.gz $ cd goaccess-1.3/

./configure --enable-utf8 --enable-geoip=legacy

make && make install

如果安装过程 提示失败

# 执行 ./configure提示
 *** Missing development files for the GeoIP library 
#安装GeoIP-devel 
 yum install -y GeoIP-devel 
# 执行 ./configure提示2
 configure: error: *** Missing development libraries for ncursesw
#安装ncurses-devel
 yum install -y ncurses-devel

3.启动php插件服务

有些nginx部署php,php需要用到php-fpm(php进程管理器)

# 进入php文件夹
cd /usr/local/php
# 启动php服务
 ./sbin/php-fpm
 # 检查
 lsof -i:9000

4.nginx配置ssl证书

配置结构

image.png

docker-compose.yml

version: '3.1'
services:
  nginx:
    restart: always
    image: nginx
    ports:
      - 8081:8081
      - 443:443
    volumes:
      - ./conf.d/:/etc/nginx/conf.d
      - ./html/:/var/www/html/

www.soxstudio.top.conf 映射 8081 防止与当前的80项目冲突


server {
    listen       8081;
    server_name  www.soxstudio.top;
    # location / {
    #     root   /var/www/html;
    #     index  index.html index.htm;
    # }
    location = / {
        rewrite ^(.*) https://www.soxstudio.top/$1 permanent;
    }
}
server {
    listen 443;
    server_name localhost;
    ssl on;
    root html;
    index index.html index.htm;
    ssl_certificate   conf.d/cert/1_soxstudio.top_bundle.crt;
    ssl_certificate_key  conf.d/cert/2_soxstudio.top.key;
    ssl_session_timeout 5m;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    location / {
        root /var/www/html;
        index index.html index.htm;
    }
}

放置腾讯云生成的两个ssl证书文件在

nginx/conf.d/cert/

  • 1_soxstudio.top_bundle.crt
  • 2_soxstudio.top.key

启动

docker-compose -f docker-compose.yml up
测试访问:
https://www.soxstudio.top/

5.http升级https流程

  1. 部署安装nginx
  2. nginx安装 https模块插件
  3. nginx上传https证书到/usr/local/nginx/conf
  4. nginx配置证书
  5. 把后端接口与图片视频资源转发到nginx的https配置下

6.常见问题

Linux系统下ngnix使用HTTPS协议启动报错:

nginx: [emerg] the "ssl" parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/nginx.c

报错原因:

未安装SSL模块--ngx_http_ssl_module

解决方法:

1.先查看nginx原有的模块:

/usr/local/nginx/sbin/nginx -V (或./nginx -V

在configure arguments:后面显示的参数如下:

--prefix=/usr/local/nginx --with-http_stub_status_module ,则代表未安装SSL模块

2.那么安装好的参数应该是这样

yum -y install openssl openssl-devel (非必须,安装ssl插件)

在原来下载的nginx 目录下执行

./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module

 运行命令即可,

3.等配置完成后,运行命令:  make 这里不要进行make install,否则就是覆盖安装

4.然后备份原有已安装好的nginx:  

cp /usr/local/nginx/sbin/nginx  /usr/local/nginx/sbin/nginx.bak

5.然后将刚刚编译好的nginx覆盖掉原有的nginx(这个时候nginx要停止状态):  

  1. 先停止nginx

/usr/local/nginx/sbin/nginx -s stop

  1. 再进行覆盖

cp ./objs/nginx /usr/local/nginx/sbin/

然后启动nginx,仍可以通过命令查看是否已经加入成功

/usr/local/nginx/sbin/nginx -V

6.配置信息指向-调整

如果无法停止nginx 可能配置指向已经变化,需要重现调整回原来自己配置的路径 /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf