CentOS 8 中安装 Nginx 到指定目录并配置反向代理
系统平台: CentOS 8 64位
1. 安装编译工具及库文件
yum install -y gcc gcc-c++;
yum install -y zlib zlib-devel;
yum install -y libtool;
yum install -y automake autoconf make;
yum install -y openssl openssl-devel;
yum install -y perl perl-devel perl-ExtUtils-Embed;
yum install -y redhat-rpm-config.noarch;
yum install -y libxml2 libxslt libxml2-devel libxslt-devel;
yum install -y gd gd-devel;
yum install -y pcre pcre-devel;
yum install -y geoipupdate.x86_64 libmaxminddb-devel;
2. 安装 Nginx
2.1 下载 Nginx 安装包
下载 Nginx 地址:
http://nginx.org/en/download.html
cd /software/nginx/nginx
wget http://nginx.org/download/nginx-1.20.2.tar.gz
2.2 解压 Nginx 安装包
tar -zxvf nginx-1.20.2.tar.gz
2.3 进入安装包目录
cd nginx-1.20.2
2.4 编译安装
指定安装路径: /software/nginx/nginx/nginx
CFLAGS="-fPIC" ./configure --prefix=/software/nginx/nginx/nginx \
--with-select_module --with-poll_module --with-threads --with-file-aio \
--with-http_random_index_module \
--with-http_sub_module \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-http_auth_request_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_image_filter_module \
--with-http_mp4_module \
--with-http_perl_module \
--with-http_realip_module \
--with-http_secure_link_module \
--with-http_slice_module \
--with-http_v2_module \
--with-http_xslt_module \
--with-mail \
--with-mail_ssl_module \
--with-stream_geoip_module \
--with-stream_realip_module \
--with-stream_ssl_module \
--with-stream_ssl_preread_module \
--with-pcre --with-pcre-jit --with-debug
make
make install
其中下面两个模块直接用 yum 源没有安装成功, Centos7 因该没有问题的, 或者直接下载相应的包直接编译安装
yum install -y perftools;
yum install GeoIP GeoIP-devel GeoIP-update;
--with-http_geoip_module \
--with-google_perftools_module \
2.5 查看 Nginx 版本
/software/nginx/nginx/nginx/sbin/nginx -v
2.6 启动 Nginx
/software/nginx/nginx/nginx/sbin/nginx
2.7 Nginx 其他关于启动的命令
/software/nginx/nginx/nginx/sbin/nginx -s reload # 重新载入配置文件
/software/nginx/nginx/nginx/sbin/nginx -s reopen # 重启 Nginx
/software/nginx/nginx/nginx/sbin/nginx -s stop # 停止 Nginx
2.8 浏览器查看
3. 配置 Nginx 反向代理
3.1 设置配置文件
vim /software/nginx/nginx/nginx/conf/nginx.conf
文件添加内容
location /test {
proxy_pass http://localhost:8080/test;
index index.html index.htm index.jsp;
}
文件内容
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#设置日志级别
error_log logs/nginx_error.log crit;
#pid logs/nginx.pid;
pid nginx.pid;
#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 512321;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
location /test {
proxy_pass http://localhost:8080/test;
index index.html index.htm index.jsp;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
3.2 检查配置文件 nginx.conf
/software/nginx/nginx/nginx/sbin/nginx -t
3.3 重新载入配置文件
/software/nginx/nginx/nginx/sbin/nginx -s reload
4 设置环境变量
这里使用这个文件
~/.bashrc
(/root/.bashrc)
修改文件
vim ~/.bashrc
添加内容
alias nginx='/software/nginx/nginx/nginx/sbin/nginx'
使文件生效
source ~/.bashrc
效果
5. 一键部署 Nginx
将 nginx 部署过程编写一个 Linux 脚本.
#!/bin/bash
#######################################################
# v: nginx 版本
# p: 安装路径
# bashrc_line: 环境变量中插入的行数
# bashrc_name: nginx 在环境变量中启动的值
#######################################################
version="1.20.2"
install_path="/software/nginx"
bashrc_line=8
bashrc_name="nginx"
# 定义错误输出
function errorEcho() {
echo -e "\033[31;40m$1\033[0m"
}
# 定义提示输出
function tipEcho() {
echo -e "\033[34;40m$1\033[0m"
}
# 创建文件夹
function creatDir() {
if [ ! -d "$1" ]; then
mkdir -p "$1"
tipEcho "创建 $1 文件夹"
fi
}
# 参数设置
while getopts "v:p:l:n:" arg; do
case $arg in
# 用 $OPTARG 取参数值
# 用 $OPTIND 得到索引
v)
version=$OPTARG
;;
p)
install_path=$OPTARG
;;
l)
bashrc_line=$OPTARG
;;
n)
bashrc_name=$OPTARG
;;
?)
errorEcho "There is no such parameter!"
tipEcho " -v nginx version. Default 1.20.2"
tipEcho " -p nginx install path. Default /software/nginx. note: set absolute path"
tipEcho " -l Number of rows inserted into the environment variable. ~/.bashrc file. Default 8"
tipEcho " -n Name in nginx startup environment variable. ~/.bashrc file. Default nginx"
exit 0
;;
esac
done
# 有报错的退出脚本
set -e
# 安装编译相应的库
tipEcho "Start installing dependent packages..."
yum install -y gcc gcc-c++
yum install -y zlib zlib-devel
yum install -y libtool
yum install -y automake autoconf make
yum install -y openssl openssl-devel
yum install -y gd-devel
yum install -y perl perl-devel perl-ExtUtils-Embed
yum install -y redhat-rpm-config.noarch
yum install -y pcre*
yum install -y libxml2 libxslt libxml2-devel libxslt-devel
yum install -y gd gd-devel
yum install -y pcre pcre-devel
yum install -y geoipupdate.x86_64 libmaxminddb-devel
tipEcho "Installation is complete..."
# 创建文件夹
creatDir "$install_path"/nginx
# 切换路径
cd "$install_path"/nginx
# 下载安装包
tipEcho "Download install package"
wget http://nginx.org/download/nginx-"$version".tar.gz
# 解压 Nginx 安装包
tar -zxvf nginx-"$version".tar.gz
# 进入安装包目录
cd nginx-1.20.2
# 创建文件夹
creatDir "$install_path"/nginx/nginx
# 编译
tipEcho "start compile...."
CFLAGS="-fPIC" ./configure --prefix="$install_path"/nginx/nginx \
--with-select_module --with-poll_module --with-threads --with-file-aio \
--with-http_random_index_module \
--with-http_sub_module \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-http_auth_request_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_image_filter_module \
--with-http_mp4_module \
--with-http_perl_module \
--with-http_realip_module \
--with-http_secure_link_module \
--with-http_slice_module \
--with-http_v2_module \
--with-http_xslt_module \
--with-mail \
--with-mail_ssl_module \
--with-stream_geoip_module \
--with-stream_realip_module \
--with-stream_ssl_module \
--with-stream_ssl_preread_module \
--with-pcre --with-pcre-jit --with-debug
make
make install
tipEcho "Compilation completed...."
# 有报错的继续执行
set +e
tipEcho "查看 Nginx 版本"
"$install_path"/nginx/nginx/sbin/nginx -v
# 重新加载 nginx.conf 文件
"$install_path"/nginx/nginx/sbin/nginx -c "$install_path"/nginx/nginx/conf/nginx.conf
# 检查并启动 nginx
tipEcho "启动 nginx"
"$install_path"/nginx/nginx/sbin/nginx -t
"$install_path"/nginx/nginx/sbin/nginx -s reload
# 添加环境变量
tipEcho "Add environment variable"
sed -i "$bashrc_line i alias $bashrc_name='$install_path/nginx/nginx/sbin/nginx'" ~/.bashrc
# 使环境变量生效, 由于执行脚本属于子 shell, 故在父 shell 中无效, 所以此行代码需要在控制台重新执行
# shellcheck disable=SC1090
source ~/.bashrc
运行代码并查看
bash nginx.sh -v 1.20.2 -p /software/nginx -l 8 -n nginx && source ~/.bashrc