nginxcentos

Centos下nginx的安装

2022-08-31  本文已影响0人  升职哦

Centos下nginx的安装

ps: 源码包下载、编译安装统一制定安装在/usr/local/src/路径下。

操作命令推荐使用全路径,方便以后ps查看

一、编译安装

1.安装编辑工具及库文件

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

2.安装前置

1).pcre安装(rewrite模块)

PCRE库支持正则表达式。如果我们在配置文件nginx.conf中使用了正则表达式,那么在编译Nginx时就必须把PCRE库编译进Nginx,因为Nginx的HTTP模块需要靠它来**解析正则表达式**。另外,pcre-devel是使用PCRE做二次开发时所需要的开发库,包括头文件等,这也是编译Nginx所必须使用的。

pcre官网下载地址:https://ftp.pcre.org/pub/pcre/

# 下载
cd /usr/local/src/
wget https://ftp.pcre.org/pub/pcre/pcre-8.35.tar.gz
tar zxvf pcre-8.35.tar.gz
# 编译安装
cd pcre-8.35/
./configure
make && make install

3.nginx安装

nginx 官网下载地址:https://nginx.org/download/

nginx 编译安装路径 /usr/local/nginx

# 下载
cd /usr/local/src/
wget https://nginx.org/download/nginx-1.9.9.tar.gz
tar xvf nginx-1.9.9.tar.gz 
cd nginx-1.9.9
# 编译安装
./configure \
--prefix=/usr/local/nginx \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-pcre=/usr/local/src/pcre-8.35
make && make install

编译成功后会在/usr/local/nginx路径下生成相关文件,目录结构如下:

tree nginx
nginx
├── client_body_temp [error opening dir]
├── conf
│   ├── fastcgi.conf
│   ├── fastcgi.conf.default
│   ├── fastcgi_params
│   ├── fastcgi_params.default
│   ├── koi-utf
│   ├── koi-win
│   ├── mime.types
│   ├── mime.types.default
│   ├── nginx.conf
│   ├── nginx.conf.default
│   ├── scgi_params
│   ├── scgi_params.default
│   ├── uwsgi_params
│   ├── uwsgi_params.default
│   └── win-utf
├── fastcgi_temp [error opening dir]
├── html
│   ├── 50x.html
│   └── index.html
├── logs
│   ├── access.log
│   ├── error.log
│   └── nginx.pid
├── proxy_temp [error opening dir]
├── sbin
│   └── nginx
├── scgi_temp [error opening dir]
└── uwsgi_temp [error opening dir]

4.开放端口

防火墙开放80端口

# 查询防火墙已开放端口
firewall-cmd --list-port
# 设置开放80端口
firewall-cmd --zone=public --add-port=80/tcp --permanent
# 重启防火墙,生效设置
firewall-cmd --reload

二、常用命令

1.nginx命令

# 启动-通过配置文件
./nginx -c conf/nginx.conf
# 启动命令-全路径,方便ps查看
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

# 停止
./nginx -s stop

# 重启
./nginx -s reload

2.nginx 域名配置(include方式)

0).目录结构

conf
├── fastcgi.conf
├ ...
├ ...
├── nginx.conf
├── nginx.conf.default
├ ...
├ ...
├── vhost-local
│   ├── 10.56.225.63.conf
│   └── consul.****.com.conf
└── win-utf

1).nginx.conf

#user  work_memory;
worker_processes  auto;
worker_rlimit_nofile 65535;

events {
    use epoll;
    multi_accept on;
    worker_connections  65535;
}


http {
    
    include         mime.types;
    default_type    application/octet-stream;

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    server_tokens off;

    keepalive_timeout  30;

    client_header_buffer_size 32k;
    client_header_timeout 30;

    client_max_body_size 100m;
    client_body_timeout 30;

    large_client_header_buffers 4 32k;
    send_timeout 30;

    # HttpGzip
    gzip on;
    gzip_min_length 1k;
    gzip_buffers 4 16k;
    gzip_http_version 1.1;
    gzip_comp_level 3;
    gzip_types text/plain application/x-javascript text/css application/xml;
    gzip_vary on;
    gzip_disable "MSIE [1-6]\.";

    # proxy
    proxy_buffer_size 16k;
    proxy_buffers 4 32k;
    proxy_busy_buffers_size 64k;
    proxy_temp_file_write_size 64k;
    proxy_set_header   Host             $host:$server_port;
    proxy_set_header   X-Real-IP        $remote_addr;
    proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;

    server {
        listen       80;
        server_name  localhost;

        location / {
            root   html;
            index  index.html index.htm;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
        
        # include 扫描指定路径
    include     vhost-local/*.conf;

}

2).consul.xxxx.com.conf

upstream consul {
    server 10.1.22.11:18300;
    #check interval=3000 rise=2 fall=5 timeout=1000 type=http;
    keepalive 16;
}

server {
    listen                 80;
    server_name            consul.xxxx.com;

    access_log             logs/access.log;
    error_log              logs/error.log;

    proxy_connect_timeout 60s;

    location / {
        proxy_pass         http://consul;
        proxy_set_header   Host             $host:$server_port;
        proxy_set_header   X-Real-IP        $remote_addr;
        proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
        proxy_http_version 1.1;
        proxy_set_header Connection "";
        client_max_body_size 10m;
    }

}

3. windows nginx.bat脚本

@echo off
rem 提供Windows下nginx的启动,重启,关闭功能
 
echo ==================begin========================
 
cls 
::ngxin 所在的盘符
set NGINX_PATH=G:
 
::nginx 所在目录
set NGINX_DIR=G:\nginx-1.15.12\
color 0a 
TITLE Nginx 管理程序增强版
 
CLS 
 
echo. 
echo. ** Nginx 管理程序  *** 
echo. *** create 2017-09-22 *** 
echo. 
 
:MENU 
 
echo. ***** nginx 进程list ****** 
::tasklist|findstr /i "nginx.exe"
tasklist /fi "imagename eq nginx.exe"
 
echo. 
 
    if ERRORLEVEL 1 (
        echo nginx.exe不存在
    ) else (
        echo nginx.exe存在
    )
 
echo. 
::*************************************************************************************************************
echo. 
    echo.  [1] 启动Nginx  
    echo.  [2] 关闭Nginx  
    echo.  [3] 重启Nginx 
    echo.  [4] 刷新控制台  
    echo.  [5] 重新加载Nginx配置文件
    echo.  [6] 检查测试nginx配置文件
    echo.  [7] 查看nginx version
    echo.  [0] 退 出 
echo. 
 
echo.请输入选择的序号:
set /p ID=
    IF "%id%"=="1" GOTO start 
    IF "%id%"=="2" GOTO stop 
    IF "%id%"=="3" GOTO restart 
    IF "%id%"=="4" GOTO MENU
    IF "%id%"=="5" GOTO reloadConf 
    IF "%id%"=="6" GOTO checkConf 
    IF "%id%"=="7" GOTO showVersion 
    IF "%id%"=="0" EXIT
PAUSE 
 
::*************************************************************************************************************
::启动
:start 
    call :startNginx
    GOTO MENU
 
::停止
:stop 
    call :shutdownNginx
    GOTO MENU
 
::重启
:restart 
    call :shutdownNginx
    call :startNginx
    GOTO MENU
 
::检查测试配置文件
:checkConf 
    call :checkConfNginx
    GOTO MENU
 
::重新加载Nginx配置文件
:reloadConf 
    call :checkConfNginx
    call :reloadConfNginx
    GOTO MENU
    
::显示nginx版本
:showVersion 
    call :showVersionNginx
    GOTO MENU   
    
    
::*************************************************************************************
::底层
::*************************************************************************************
:shutdownNginx
    echo. 
    echo.关闭Nginx...... 
    taskkill /F /IM nginx.exe > nul
    echo.OK,关闭所有nginx 进程
    goto :eof
 
:startNginx
    echo. 
    echo.启动Nginx...... 
    IF NOT EXIST "%NGINX_DIR%nginx.exe" (
        echo "%NGINX_DIR%nginx.exe"不存在
        goto :eof
     )
 
    %NGINX_PATH% 
    cd "%NGINX_DIR%" 
 
    IF EXIST "%NGINX_DIR%nginx.exe" (
        echo "start '' nginx.exe"
        start "" nginx.exe
    )
    echo.OK
    goto :eof
    
 
:checkConfNginx
    echo. 
    echo.检查测试 nginx 配置文件...... 
    IF NOT EXIST "%NGINX_DIR%nginx.exe" (
        echo "%NGINX_DIR%nginx.exe"不存在
        goto :eof
     )
 
    %NGINX_PATH% 
    cd "%NGINX_DIR%" 
    nginx -t -c conf/nginx.conf
 
    goto :eof
    
::重新加载 nginx 配置文件
:reloadConfNginx
    echo. 
    echo.重新加载 nginx 配置文件...... 
    IF NOT EXIST "%NGINX_DIR%nginx.exe" (
        echo "%NGINX_DIR%nginx.exe"不存在
        goto :eof
     )
 
    %NGINX_PATH% 
    cd "%NGINX_DIR%" 
    nginx -s reload
 
    goto :eof
    
::显示nginx版本
:showVersionNginx
    echo. 
    %NGINX_PATH% 
    cd "%NGINX_DIR%" 
    nginx -V
上一篇下一篇

猜你喜欢

热点阅读