nginx-1.22.1源码安装

[TOC]

nginx-1.22.1源码安装 编写总结日期 2018/4/3 作者:请叫我曾哥 目的:懒 1.环境参数:Linux:Centos7,Nginx:1.22.1源码安装
注意事项:nginx.conf配置log_format main日志打印格式如果使用多域名方式请务必在你的日志后面添加 main这样才能引用指定的日志打印格式,否则则会使用nginx默认格式

安装shell脚本
vi nginx-install.sh

#!/bin/bash

# 定义变量
NGINX_VERSION="1.22.1"
NGINX_URL="http://nginx.org/download/nginx-${NGINX_VERSION}.tar.gz"
NGINX_DIR="/data/nginx-${NGINX_VERSION}"

# 1. 切换目录
echo "切换到 /opt/ 目录..."
cd /opt/
if [ $? -ne 0 ]; then
    echo "错误:切换目录失败"
    exit 1
fi

# 2. 下载 Nginx
echo "正在下载 Nginx..."
wget $NGINX_URL
if [ $? -ne 0 ]; then
    echo "错误:下载 Nginx 失败,请检查链接是否正确或网络是否正常。"
    exit 1
fi

# 3. 安装依赖
echo "正在安装依赖..."
yum install pcre pcre-devel openssl openssl-devel gcc zlib zlib-devel -y
if [ $? -ne 0 ]; then
    echo "错误:安装依赖失败"
    exit 1
fi

# 4. 创建 Nginx 用户和组
echo "正在创建 Nginx 用户和组..."
groupadd www && useradd www -s /sbin/nologin -M -g www
if [ $? -ne 0 ]; then
    echo "错误:创建 Nginx 用户和组失败"
    exit 1
fi

# 5. 解压 Nginx
echo "正在解压 Nginx..."
tar xf nginx-${NGINX_VERSION}.tar.gz
if [ $? -ne 0 ]; then
    echo "错误:解压 Nginx 失败"
    exit 1
fi
cd nginx-${NGINX_VERSION}
mkdir -p ${NGINX_DIR}/conf.d
mkdir -p ${NGINX_DIR}/tmp/proxy/
if [ $? -ne 0 ]; then
    echo "错误:创建目录失败"
    exit 1
fi

# 6. 编译软件
echo "正在编译 Nginx..."
./configure \
--prefix=${NGINX_DIR} \
--sbin-path=${NGINX_DIR}/sbin/nginx \
--conf-path=${NGINX_DIR}/conf/nginx.conf \
--error-log-path=${NGINX_DIR}/log/error.log \
--http-log-path=${NGINX_DIR}/log/access.log \
--pid-path=${NGINX_DIR}/nginx.pid \
--lock-path=${NGINX_DIR}/nginx.lock \
--user=www \
--group=www \
--with-http_ssl_module \
--with-http_flv_module \
--with-http_stub_status_module \
--with-http_gzip_static_module  \
--http-proxy-temp-path=${NGINX_DIR}/tmp/proxy/ \
--http-fastcgi-temp-path=${NGINX_DIR}/tmp/fcgi/ \
--http-uwsgi-temp-path=${NGINX_DIR}/tmp/uwsgi \
--http-scgi-temp-path=${NGINX_DIR}/tmp/scgi \
--with-pcre \
--with-file-aio \
--with-http_stub_status_module \
--with-http_realip_module
if [ $? -ne 0 ]; then
    echo "错误:配置 Nginx 失败"
    exit 1
fi

make && make install
if [ $? -ne 0 ]; then
    echo "错误:编译和安装 Nginx 失败"
    exit 1
fi

# 7. 编辑 nginx.conf 配置文件
echo "正在编辑 Nginx 配置文件..."
cat << EOF > ${NGINX_DIR}/conf/nginx.conf
user  www;
worker_processes  auto;
error_log  ${NGINX_DIR}/log/error.log;
pid        ${NGINX_DIR}/nginx.pid;

events {
    use epoll;
    worker_connections  20480;
}

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

    log_format  main  '[$time_local] $remote_addr "$http_x_forwarded_for" $remote_port $remote_user '
                      '$host $upstream_addr $upstream_status $upstream_response_time $upstream_cache_status '
                      '"$request" $status $request_time $body_bytes_sent "$http_referer" "$http_user_agent" ';

    access_log  ${NGINX_DIR}/log/access.log  main;

    ##隐藏banner头信息
    server_tokens off;
    ##隐藏后端服务器指定header状态
    proxy_hide_header X-Powered-By;
    proxy_hide_header Server;
	
    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    types_hash_max_size 2048;
    proxy_connect_timeout   300; 
    proxy_send_timeout      300; 
    proxy_read_timeout      300;

    client_header_buffer_size 256k;
    large_client_header_buffers 4 256k;
    client_max_body_size 1024m;
    client_body_buffer_size 1024m;
    underscores_in_headers on;

    gzip  on;
    gzip_disable "MSIE [1-6]\.(?!.*SV1)";
    gzip_min_length 1k;
    gzip_buffers 4 16k;
    gzip_http_version 1.1;
    gzip_comp_level 2;
    gzip_types text/plain application/javascript text/css application/xml;
    gzip_vary on;

include ${NGINX_DIR}/conf.d/*.conf;
}

EOF
if [ $? -ne 0 ]; then
    echo "错误:编辑 Nginx 配置文件失败"
    exit 1
fi

# 8. 编辑域名或访问文件
cat << EOF > ${NGINX_DIR}/conf.d/test.conf
server {  
    listen       80;
    server_name  127.0.0.1; 
    location / {   
        root   ${NGINX_DIR}/html;
        index  index.html index.htm;
    }  

    error_page   500 502 503 504  /50x.html;
    location = /50x.html { 
        root   ${NGINX_DIR}/html;
    }  
}
EOF
if [ $? -ne 0 ]; then
    echo "错误:编辑 test.conf 文件失败"
    exit 1
fi

# 9. 放置前端代码
echo "请将前端代码放置到 ${NGINX_DIR}/html/ 目录下。"

# 10. 授权
echo "正在设置权限..."
chown -R www.www ${NGINX_DIR}/
if [ $? -ne 0 ]; then
    echo "错误:设置权限失败"
    exit 1
fi
chmod -R 755 ${NGINX_DIR}/
chmod -R 644 ${NGINX_DIR}/conf
if [ $? -ne 0 ]; then
    echo "错误:设置文件权限失败"
    exit 1
fi

# 11. 启动 Nginx
echo "正在启动 Nginx..."
${NGINX_DIR}/sbin/nginx
if [ $? -ne 0 ]; then
    echo "错误:启动 Nginx 失败"
    exit 1
fi

# 12. 添加环境变量
echo "正在添加环境变量..."
cat > /etc/profile.d/nginx.sh << EOF
PATH=\$PATH:${NGINX_DIR}/sbin
export PATH
EOF
source /etc/profile.d/nginx.sh
if [ $? -ne 0 ]; then
    echo "错误:添加环境变量失败"
    exit 1
fi

# 13. 添加开机启动
echo "正在添加开机启动..."
cat >  /usr/lib/systemd/system/nginx.service << EOF
[Unit]
Description=nginx
After=network.target

[Service]
Type=forking
ExecStart=${NGINX_DIR}/sbin/nginx
ExecReload=${NGINX_DIR}/sbin/nginx -s reload
ExecStop=${NGINX_DIR}/sbin/nginx -s quit
PrivateTmp=true

[Install]
WantedBy=multi-user.target
EOF
if [ $? -ne 0 ]; then
    echo "错误:添加开机启动配置失败"
    exit 1
fi

systemctl enable nginx.service
if [ $? -ne 0 ]; then
    echo "错误:设置开机启动失败"
    exit 1
fi

# 13.3 杀死nginx重启nginx
echo "正在重启 Nginx..."
pkill -9 nginx
if [ $? -ne 0 ]; then
    echo "错误:杀死 Nginx 进程失败"
    exit 1
fi
ps aux | grep nginx
systemctl start nginx
if [ $? -ne 0 ]; then
    echo "错误:启动 Nginx 服务失败"
    exit 1
fi

# 13.4 查看服务状态
echo "查看 Nginx 服务状态..."
systemctl status nginx.service
if [ $? -ne 0 ]; then
    echo "错误:查看 Nginx 服务状态失败"
    exit 1
fi

echo "Nginx 安装完成!"

1 切换目录

cd /opt/

2.Nginx下载,下载地址:

wget http://nginx.org/download/nginx-1.22.1.tar.gz

3.依赖安装

yum install pcre pcre-devel openssl openssl-devel gcc zlib zlib-devel -y

4.创建Nginx用户和组

groupadd www && useradd www -s /sbin/nologin -M -g www

5.解压Nginx

tar xf nginx-1.22.1.tar.gz
cd nginx-1.22.1
mkdir -p /data/nginx-1.22.1/conf.d
mkdir -p /data/nginx-1.22.1/tmp/proxy/

6.编译软件

./configure \
--prefix=/data/nginx-1.22.1 \
--sbin-path=/data/nginx-1.22.1/sbin/nginx \
--conf-path=/data/nginx-1.22.1/conf/nginx.conf \
--error-log-path=/data/nginx-1.22.1/log/error.log \
--http-log-path=/data/nginx-1.22.1/log/access.log \
--pid-path=/data/nginx-1.22.1/nginx.pid \
--lock-path=/data/nginx-1.22.1/nginx.lock \
--user=www \
--group=www \
--with-http_ssl_module \
--with-http_flv_module \
--with-http_stub_status_module \
--with-http_gzip_static_module  \
--http-proxy-temp-path=/data/nginx-1.22.1/tmp/proxy/ \
--http-fastcgi-temp-path=/data/nginx-1.22.1/tmp/fcgi/ \
--http-uwsgi-temp-path=/data/nginx-1.22.1/tmp/uwsgi \
--http-scgi-temp-path=/data/nginx-1.22.1/tmp/scgi \
--with-pcre \
--with-file-aio \
--with-http_stub_status_module \
--with-http_realip_module

特殊说明该模块slb代理之后日志获取真实IP地址 --with-http_realip_module # 启用realip模块

make && make install

7 编辑nginx.conf配置文件

echo "" > /data/nginx-1.22.1/conf/nginx.conf
cat << EOF > /data/nginx-1.22.1/conf/nginx.conf
user  www;
worker_processes  auto;
error_log  /data/nginx-1.22.1/log/error.log;
pid        /data/nginx-1.22.1/nginx.pid;

events {
    use epoll;
    worker_connections  20480;
}

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

    log_format  main  '[$time_local] $remote_addr "$http_x_forwarded_for" $remote_port $remote_user '
                      '$host $upstream_addr $upstream_status $upstream_response_time $upstream_cache_status '
                      '"$request" $status $request_time $body_bytes_sent "$http_referer" "$http_user_agent" ';

    access_log  /data/nginx-1.22.1/log/access.log  main;

    ##隐藏banner头信息
    server_tokens off;
    ##隐藏后端服务器指定header状态
    proxy_hide_header X-Powered-By;
    proxy_hide_header Server;
	
    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    types_hash_max_size 2048;
    proxy_connect_timeout   300; 
    proxy_send_timeout      300; 
    proxy_read_timeout      300;

    client_header_buffer_size 256k;
    large_client_header_buffers 4 256k;
    client_max_body_size 1024m;
    client_body_buffer_size 1024m;
    underscores_in_headers on;

    gzip  on;
    gzip_disable "MSIE [1-6]\.(?!.*SV1)";
    gzip_min_length 1k;
    gzip_buffers 4 16k;
    gzip_http_version 1.1;
    gzip_comp_level 2;
    gzip_types text/plain application/javascript text/css application/xml;
    gzip_vary on;

include /data/nginx-1.22.1/conf.d/*.conf;
}

EOF

8 编辑域名或者访问文件

cat << EOF > /data/nginx-1.22.1/conf.d/test.conf
server {  
    listen       80;
    server_name  公网ip; 
    location / {   
        root   /data/nginx-1.22.1/html;
        index  index.html index.htm;
    }  

    error_page   500 502 503 504  /50x.html;
    location = /50x.html { 
        root   /data/nginx-1.22.1/html;
    }  
}
EOF

9放置前端代码

mv xxx /data/nginx-1.22.1/html/

10 授权

chown -R www.www /data/nginx-1.22.1/
chmod -R 755 /data/nginx-1.22.1/
chmod -R 644 /data/nginx-1.22.1/conf 

11 启动

/data/nginx-1.22.1/sbin/nginx

12 添加环境变量

##12.1 编辑/etc/profile

cat > /etc/profile.d/nginx.sh << EOF
PATH=$PATH:/data/nginx-1.22.1/sbin
export PATH
EOF

12.2 使配置立即生效

source /etc/profile.d/nginx.sh

13 添加开机启动

13.1 添加开机启动

cat >  /usr/lib/systemd/system/nginx.service << EOF
[Unit]
Description=nginx
After=network.target

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

[Install]
WantedBy=multi-user.target

##[Unit]:服务的说明
##Description:描述服务
##After:描述服务类别
##[Service]服务运行参数的设置
##Type=forking是后台运行的形式
##ExecStart为服务的具体运行命令
##ExecReload为重启命令
##ExecStop为停止命令
##PrivateTmp=True表示给服务分配独立的临时空间
##注意:[Service]的启动、重启、停止命令全部要求使用绝对路径
##[Install]运行级别下服务安装的相关设置,可设置为多用户,即系统运行级别为3
EOF

13.2 设置开机自动启动

systemctl enable nginx.service

13.3 杀死nginx重启nginx

pkill -9 nginx
ps aux | grep nginx
systemctl start nginx

13.4 查看服务状态

systemctl status nginx.service