linux(centos7.X) 下 一键编译安装 nginx

本文提供了一个适用于 CentOS Linux 7.2 的一键安装 Nginx 的 Bash 脚本,并详细介绍了从下载到配置的全过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

自己写的一个一键编译安装nginx的sh  CentOS Linux release 7.2.1511 (Core) 下亲测有效

#!/bin/bash
echo "---------------- download nginx -------------------";
if [ ! -d "/usr/local/download"  ];then
  mkdir -p /usr/local/download
fi
cd /usr/local/download
wget http://nginx.org/download/nginx-1.20.0.tar.gz
tar -zxvf nginx-1.20.0.tar.gz
cd nginx-1.20.0
echo "---------------- install gcc Dev -------------------";
yum install -y gcc gcc-c++
yum install -y pcre pcre-devel
yum install -y zlib zlib-devel
yum install -y openssl openssl-devel
echo "---------------- add user --------------------------";
groupadd nginx
useradd -g nginx nginx
echo "---------------- install... --------------------------";
./configure \
--prefix=/usr/local/nginx \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx.pid \
--lock-path=/var/run/nginx.lock \
--http-client-body-temp-path=/var/cache/nginx/client_temp \
--http-proxy-temp-path=/var/cache/nginx/proxy_temp \
--http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \
--http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \
--http-scgi-temp-path=/var/cache/nginx/scgi_temp \
--user=nginx \
--group=nginx \
--with-file-aio \
--with-threads \
--with-http_addition_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_mp4_module \
--with-http_random_index_module \
--with-http_realip_module \
--with-http_secure_link_module \
--with-http_slice_module \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-http_sub_module \
--with-http_v2_module \
--with-mail \
--with-mail_ssl_module \
--with-stream \
--with-stream_realip_module \
--with-stream_ssl_module \
--with-stream_ssl_preread_module
make && make install
echo "---------------- nginx -v  ------------------------------";
mkdir -p /var/cache/nginx
chown -R nginx:nginx /usr/local/nginx
/usr/local/nginx/sbin/nginx -v

上面sh下载链接 https://download.csdn.net/download/BookNoteY/18881146

下载的nginx版本可自由修改 如果觉得有用点个赞吧!

常用命令说明

# 重置配置文件
/usr/local/nginx -s reload 
# 启动 nginx
/usr/local/nginx
# 或
/usr/local/nginx -c /配置文件路径
# 快速关闭nginx
/usr/local/nginx -s stop
# 正常关闭
/usr/local/nginx -s quit

下面是脚本一步步流程介绍

查看当前服务器 系统内核

unane -a 

查看centos 版本

cat /etc/redhat-release

nginx 官网下载页

http://nginx.org/en/download.html

下载个稳定版本就ok

当前最新1.18 下载地址

http://nginx.org/download/nginx-1.18.0.tar.gz

随便建立一个目录 mkdir service

 

mkdir -p /usr/local/download && cd /usr/local/download
wget http://nginx.org/download/nginx-1.18.0.tar.gz

yum安装 编译文件&三方库

安装 Gcc 编译器

yum install -y gcc gcc-c++

安装正则表达式PCRE库

yum install -y pcre pcre-devel

安装 zlib压缩库

yum install -y zlib zlib-devel

安装openssl开发库

yum install -y openssl openssl-devel

解压目录 并进入目录

tar -axv -f nginx-1.18.0.tar.gz && cd nginx-1.18.0

创建 nginx 用户

groupadd nginx
useradd nginx -g nginx

生成make 文件  make参数说明(https://cloud.tencent.com/developer/article/1619507

cd /usr/local/download/nginx-1.20.0

--prefix=/usr/local/nginx \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx.pid \
--lock-path=/var/run/nginx.lock \
--http-client-body-temp-path=/var/cache/nginx/client_temp \
--http-proxy-temp-path=/var/cache/nginx/proxy_temp \
--http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \
--http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \
--http-scgi-temp-path=/var/cache/nginx/scgi_temp \
--user=nginx \
--group=nginx \
--with-file-aio \
--with-threads \
--with-http_addition_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_mp4_module \
--with-http_random_index_module \
--with-http_realip_module \
--with-http_secure_link_module \
--with-http_slice_module \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-http_sub_module \
--with-http_v2_module \
--with-mail \
--with-mail_ssl_module \
--with-stream \
--with-stream_realip_module \
--with-stream_ssl_module \
--with-stream_ssl_preread_module
make && make install
	'/usr/local/nginx/conf/scgi_params.default'
test -f '/usr/local/nginx/conf/nginx.conf' \
	|| cp conf/nginx.conf '/usr/local/nginx/conf/nginx.conf'
cp conf/nginx.conf '/usr/local/nginx/conf/nginx.conf.default'
test -d '/var/run' \
	|| mkdir -p '/var/run'
test -d '/var/log/nginx' \
	|| mkdir -p '/var/log/nginx'
test -d '/usr/local/nginx/html' \
	|| cp -R html '/usr/local/nginx'
test -d '/var/log/nginx' \
	|| mkdir -p '/var/log/nginx'
make[1]: Leaving directory `/usr/local/download/nginx-1.20.0'

如上面所示代表成功

修改nginx文件夹宿主并查看版本

chown -R nginx:nginx /usr/local/nginx
/usr/local/nginx/sbin/nginx -v

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值