Table of Contents
Centos7 yum源安装nginx
官网包安装文档
nginx的版本有Mainline version【主线版本】、Stable version【稳定版本】、Legacy versions【旧版本】
本示例介绍Centos7 yum源安装nginx的Mainline version【主线版本】和Stable version【稳定版本】,如Debian、Ubuntu等请自行查看官网安装文档
– 安装先决条件:
sudo yum install yum-utils
- 设置yum源仓库,创建名为/etc/yum.repos.d/nginx.repo的文件并添加如下内容
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
- 默认情况下,yum仓库使用的是Stable version【稳定版本】,如果想使用Mainline version【主线版本】,请运行以下命令
sudo yum-config-manager --enable nginx-mainline
- 安装nginx,运行以下命令:
sudo yum install nginx
Centos7 源代码编译nginx
编译安装nginx
本示例编译nginx-1.17.8的版本
nginx在编译的时候需要安装依赖包:pcre、zlib、openssl【openssl依赖perl
】
– 更新系统和基本软件安装wget、gcc、gcc-c++
yum update
yum install wget
yum install gcc
yum install gcc-c++
- pcre安装
wget https://ftp.pcre.org/pub/pcre/pcre-8.44.tar.gz
tar -zxvf 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 -zxvf zlib-1.2.11.tar.gz
cd zlib-1.2.11
./configure
make
make install
- 安装perl【目的是安装openssl】
wget https://www.cpan.org/src/5.0/perl-5.30.1.tar.gz
tar -zxvf perl-5.30.1.tar.gz
cd perl-5.30.1
./Configure -de
make
make test
make install
- 安装openssl
yum remove openssl
wget https://www.openssl.org/source/openssl-1.1.1d.tar.gz
tar -zxvf openssl-1.1.1d.tar.gz
cd openssl-1.1.1d
./config shared --prefix=/usr/local/openssl --openssldir=/usr/local/ssl
make && make install
ln -s /usr/local/openssl/bin/openssl /usr/bin/openssl
ln -s /usr/local/lib64/libssl.so.1.1 /usr/lib64/libssl.so.1.1
ln -s /usr/local/lib64/libcrypto.so.1.1 /usr/lib64/libcrypto.so.1.1
- 编译安装nginx
wget http://nginx.org/download/nginx-1.17.8.tar.gz
tar -zxvf nginx-1.17.8.tar.gz
cd nginx-1.17.8
./configure --with-http_ssl_module --with-openssl=/usr/local/openssl
make && make install
设置nginx.service
目的是为了可以使用systemctl对nginx.service进行控制【开机自启、重启、关闭、开启】
– 创建文件创建名为/lib/systemd/system/nginx.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-online.target
[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID
[Install]
WantedBy=multi-user.target
- 如下为操作nginx.service命令
#启动服务
systemctl start nginx.service
#查看日志
journalctl -f -u nginx.service
#重启
systemctl restart nginx.service
#重载
systemctl reload nginx.service
#停止
systemctl stop nginx.service
发表回复