示例服务器操作系统是centos7,最小化安装,只关闭selinux 一、yum安装nginx服务 A、基础条件设置 nginx服务的yum源设置
a. 安装wget软件
yum install wget -y
b. 备份和修改基础仓库到163
cd /etc/yum.repos.d/ && mv CentOS-Base.repo CentOS-Base.repo.back
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.163.com/.help/CentOS7-Base-163.repo
c. 安装epel仓库和修改epel仓库地址到阿里云
yum install epel-release -y
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
d. 可选:不使用epel源安装nginx,使用nginx.org的官方源,劣势是速度慢,优势是nginx版本新。手动建立/etc/yum.repos.d/nginx.repo
cat >> /etc/yum.repos.d/nginx.repo <<EOF
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
EOF
B、正式安装nginx服务 nginx服务安装 yum install nginx openssl openssl-devel zlib zlib-devel pcre pcre-devel -y
二、编译安装nginx服务 基础条件设置和yum安装相同 正式安装nginx服务 1、下载nginx源码包 wget -O /usr/local/src/nginx-1.17.10.tar.gz http://nginx.org/download/nginx-1.17.10.tar.gz
2、安装依赖包 yum -y install openssl openssl-devel zlib zlib-devel pcre pcre-devel
3、添加运行nginx服务的用户 useradd -s /sbin/nologin -M nginx
4、解压nginx源代码,并且切换目录到解压后的nginx目录中
cd /usr/local/src
tar zxvf nginx-1.17.10.tar.gz
cd nginx-1.17.10
5、编译安装,在示例中,只with了部份模块 ./configure --prefix=/usr/local/nginx-1.17.10 --user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre --with-http_flv_module make -j 4 make install
6、nginx可执行命令做软链接 ln -s /usr/local/nginx-1.17.10/sbin/nginx /usr/local/sbin/nginx
三、nginx服务启动命令或启动脚本 a. 检查nginx配置 nginx -t
b. 启动nginx nginx 或者 systemctl start nginx
3、重启nginx nginx -s reload 或者 systemctl restart nginx
4、关闭nginx nginx -s stop 或者 systemctl stop nginx
5、nginx 的systemctl 控制脚本位置 (如果希望nginx服务在操作系统重启后随机启动。修改命令是:systemctl enable nginx) /usr/lib/systemd/system/nginx.service
注: systemctl的启动脚本在yum安装后会自动生成,编译安装想使用systemctl命令启动nginx服务需要手动写启动脚本,可以参考yum安装nginx的启动脚本