Nginx Centos7 平台安装

238 阅读1分钟

1. yum 安装

1. nginx仓库添加

# 创建 nginx.repo
touch /etc/yum.repos.d/nginx.repo

# 内容如下
[nginx]
name=nginx repo
baseurl=https://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1

2. 安装

yum install nginx -y

3. 参考

www.nginx.com/resources/w…

2. 编译安装

1. 升级 gcc

# 安装gcc套件的仓库
yum -y install centos-release-scl

# 安装gcc8
yum -y install devtoolset-8-gcc devtoolset-8-gcc-c++ devtoolset-8-binutils
# 开启gcc8
echo "source /opt/rh/devtoolset-8/enable" >>/etc/profile
source /etc/profile

# 测试gcc版本
gcc --version

2. 下载 nginx

# 下载
wget http://nginx.org/download/nginx-1.16.1.tar.gz -O /usr/local/src/

# 解压
tar -zxf nginx-1.16.1.tar.gz

3. 安装依赖

# 安装 pcre
wget https://mirrors.gigenet.com/OSDN//sfnet/p/pc/pcre/pcre/8.44/pcre-8.44.tar.gz
tar -zxf pcre-8.44.tar.gz
cd pcre-8.44
./configure
make && make install

# 安装 zlib
wget http://zlib.net/zlib-1.2.11.tar.gz
tar -zxf zlib-1.2.11.tar.gz
cd zlib-1.2.11
./configure
make && make install

# 安装 openssl
wget http://www.openssl.org/source/openssl-1.1.1g.tar.gz
tar -zxf openssl-1.1.lg.tar.gz
cd openssl-1.1.lg
./Configure linux-x86_64 --prefix/usr
make && make install

4. 编译安装

# 进入安装目录
cd /usr/local/src/nginx-1.16.1

# configure
./configure --prefix=/usr/local/nginx \
            --with-pcre=../../pcre-8.44 \
            --with-zlib=../../zlib-1.2.11 \
            --with-http_ssl_module \
            --with-stream

# 安装
make && make install

# 启动
/usr/local/nginx/sbin/nginx

# 停止
/usr/local/nginx/sbin/nginx -s stop

# 安全停止
/usr/local/nginx/sbin/nginx -s quit

# 重新加载配置
/usr/local/nginx/sbin/nginx -s reload

# 加入环境变量
vim /etc/profile
export PATH=$PATH:/usr/local/nginx/sbin/

5. Service脚本

[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-onlin.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/bin/sh -c "/bin/kill -s HUP $(/bin/cat /usr/local/nginx/logs/nginx.pid)"
ExecStop=/bin/sh -c "/bin/kill -s TERM $(/bin/cat /var/run/nginx.pid)"

[Install]
WantedBy=multi-user.target

# 启动 nginx
systemctl start nginx

# 关闭 nginx
systemctl stop nginx

# 重启 nginx
systemctl restart nginx

# 开机自启
systemctl enable nginx

7. 参考

docs.nginx.com/nginx/admin…