liunx 环境下安装Nginx
新服务器安装中可能遇到的问题(可跳过)
- 显示Loading mirror speeds from cached hostfile;无可用安装包
方法一: 更新 yum
yum -y update
方法二: 更新可用 yum 源,这里更新为 163源
cd /etc/yum.repos.d
mv CentOS-Base.repo CentOS-Base.repo.backup
wget http://mirrors.163.com/.help/CentOS6-Base-163.repo
mv CentOS6-Base-163.repo CentOS-Base.repo
yum clean all
- yum 安装失败;(出现repodata/repomd.xml: [Errno 14] HTTP Error 404 - Not Found)
方法: 重装yyum -y install wgetum (建议提前安装 wget)
- 安装wget
yum -y install wget- 删除老仓库文件
cd /etc/yum.repos.d
rm * . *- 卸载yum 包和安装组件
rpm -qa yum yum-3.4.3-150.el7.centos.noarch
rpm -qa | grep yum | xargs rpm -e --nodeps
rpm -qa yum- 下载最新的rpm 包 ,wget 下载(http://mirrors.163.com/centos/7/os/x86_64/Packages/)
wget yum-最新版.centos.noarch.rpm
wget yum-metadata-parser-最新版.x86_64.rpm
wget yum-plugin-fastestmirror-最新版.noarch.rpm- 安装yum
rpm -ivh yum*- 查看是否安装成功
rpm -qa yum- 导入证书
rpm --import http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-7- 添加阿里源
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo- 清除缓存,生成新的缓存
yum clean all
yum makecache
- 不支撑 SSL;nginx: [emerg] https protocol requires SSL support in /usr/local/nginx/conf/nginx.conf:50
- 重新执行 ./confgure 文件,指定可访问 ssl (之前带的参数自行补充)
./configure --with-http_ssl_module- 重新编译 nginx文件(不要 make install,会重新安装)
make- 用新生成的nginx(objs目录下)替换原来的nginx执行文件
cp ./objs/nginx /usr/local/nginx/sbin/- 检查nginx 启动情况
./nginx -t
安装前准备
- 安装gcc 编译器
yum install gcc-c++
- 查看是否安装openssl,Openssl编译需要perl
rpm -qal |grep openssl
yum install -y openssl openssl-devel
- 安装pcre包
yum install -y pcre pcre-devel
- 安装zlib包
yum install -y zlib zlib-devel
安装nginx
- 在/usr/local/下创建文件nginx文件
mkdir /usr/local/nginx
- 下载 nginx安装包上传 Linux,或wget下载(https://nginx.org/download/)
wget https://nginx.org/download/nginx-1.19.9.tar.gz
- 解压并进入nginx目录
tar -zxvf nginx-1.19.9.tar.gz
cd nginx-1.19.9
- 使用nginx的配置,可指定参数
./configure
- 指定安装目录 ,nginx默认安装位置 /usr/local
./configure
–prefix=你想要安装的目录
–sbin-path=/你想要安装的目录/nginx
–conf-path=/你想要安装的目录/nginx.conf
–pid-path=/你想要安装的目录/nginx.pid
–with-http_ssl_module
–with-pcre=/usr/local/pcre-8.38
–with-zlib=/usr/local/zlib-1.2.11
–with-openssl=/usr/local/openssl-1.0.1t
- 编译安装
make
make install
- 启动nginx ; ./nginx执行执行 可执行文件nginx
cd sbin
./nginx
- 查看是否启动成功
ps -ef|grep nginx
- 网页访问IP即可出现nginx的欢迎界面 ,默认端口为 80
nginx 相关命令
./nginx 启动nginx
./nginx -s stop 杀掉nginx进程
./nginx -s reload 重加载nginx
nginx -V 查看安装编译参数
liunx彻底卸载nginx
- 检查nginx是否运行
ps -ef | grep nginx
- 停止nginx服务
./nginx -s stop
netstat -lntp
- 查找并删除nginx相关文件
whereis nginx
find / -name nginx
rm -rf 文件夹
- yum 清理nginx相关关系
yum remove nginx
设置 service启动nginx的命令
- 在/etc/init.d/目录下添加 nginx默认启动脚本
#!/bin/bash
#Startup script for the nginx Web Server
#chkconfig: 2345 85 15
nginx=/usr/local/nginx/sbin/nginx
conf=/usr/local/nginx/conf/nginx.conf
case $1 in
start)
echo -n “Starting Nginx”
$nginx -c $conf
echo " done."
;;
stop)
echo -n “Stopping Nginx”
killall -9 nginx
echo " done."
;;
test)
$nginx -t -c $conf
echo “Success.”
;;
reload)
echo -n “Reloading Nginx”
ps auxww | grep nginx | grep master | awk ‘{print $2}’ | xargs kill -HUP
echo " done."
;;
restart)
$nginx -s reload
echo “reload done.”
;;
*)
echo “Usage: $0 {start|restart|reload|stop|test|show}”
;;
esac
- 验证nginx启动命令
service nginx stop
service nginx start