源码安装nginx目录
编译安装
1.解决依赖关系
yum -y install gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel //安装编译的依赖
2.下载nginx源码包
yum -y install wget
wget http://nginx.org/download/nginx-1.19.5.tar.gz
3.解压nginx源码包并编译开始安装
tar xzf nginx-1.19.5.tar.gz
cd nginx-1.19.5
./configure --prefix=/srv/nginx
make && make install
nginx大部分常用模块,编译时./configure --help以–without开头的都默认安装。
–prefix=PATH : 指定nginx的安装目录。如 /srv/nginx
4.启动
/srv/nginx/sbin/nginx -c /srv/nginx/conf/nginx.conf //nginx启动并选择要启动的配置文件
kill -15 `cat /srv/nginx/logs/nginx.pid` //nginx启动后可以根据它启动的pid来进行结束进程
/srv/nginx/sbin/nginx -s reload -c /srv/nginx/conf/nginx.conf //nginx启动后重启
5.编写启动脚本
翻到最后
导入编译完安装包
1.下载编译完的源码包并解压
cd /srv && curl -o nginx.tar.gz http://182.92.165.158/nginx.tar.gz //切换到srv目录下开始下载
cd /srv && tar xzf nginx.tar.gz //解压
2.配置log目录和web目录
mkdir -p /data/web/www
mkdir -p /data/log/nginx/
echo "test" > /data/log/nginx/index.html
3.启动脚本使用
wget http://182.92.165.158/nginx_install.sh
bash nginx_install start //使用参数有:start|stop|reload|status|test
启动脚本
#!/bin/bash
pid="/srv/nginx/logs/nginx.pid"
ng_b=`netstat -nlpt | grep 80`
ng_a=$?
install() {
cd /
mkdir /srv
mkdir -p /data/web/www
mkdir -p /data/log/nginx/
echo "test" > /data/web/www/index.html
cd /srv && curl -o nginx.tar.gz http://182.92.165.158/nginx.tar.gz
tar xzf /srv/nginx.tar.gz
echo "nginx安装完毕"
}
start() {
if [ $ng_a == 0 ]; then
echo "端口被占用无法启动"
read -p "是否需要停止nginx服务(y/n)" yn
if [ $yn == "y" -o $yn == "Y" ]; then
stop
elif [ $yn == "n" -o $yn == "N" ]; then
echo "命令退出"
else
echo "请输入(y/n)"
fi
stop
elif [ $ng_a == 1 ]; then
/srv/nginx/sbin/nginx -c /srv/nginx/conf/nginx.conf
echo "开始启动"
netstat -nlpt | grep 80 &>/dev/null
if [ $? == 0 ]; then
echo "nginx启动成功"
else
echo "nginx启动失败"
fi
else
echo "nginx服务器有问题请检查"
fi
}
stop() {
if [ $ng_a == 0 ]; then
kill -15 `cat $pid`
echo "开始停止"
sleep 1s
netstat -nlpt | grep 80 &>/dev/null
if [ $? == 0 ]; then
echo "nginx停止失败"
else
echo "nginx停止成功"
fi
elif [ $ng_a == 1 ]; then
echo "nginx尚未启动无法停止"
else
echo "nginx服务器有问题请检查"
fi
}
reload() {
if [ $ng_a == 0 ]; then
/srv/nginx/sbin/nginx -s reload -c /srv/nginx/conf/nginx.conf
echo "开始重启"
netstat -nlpt | grep 80 &>/dev/null
if [ $? == 0 ]; then
echo "nginx重启成功"
else
echo "nginx重启失败"
fi
elif [ $ng_a == 1 ]; then
echo "nginx尚未启动无法重启"
read -p "是否需要启动nginx服务(y/n)" ynn
if [ $ynn == "y" -o $ynn == "Y" ]; then
start
elif [ $ynn == "n" -o $ynn == "N" ]; then
echo "命令退出"
else
echo "请输入(y/n)"
fi
else
echo "nginx服务器有问题请检查"
fi
}
test() {
echo "开始监测nginx配置文件"
/srv/nginx/sbin/nginx -c /srv/nginx/conf/nginx.conf -t &>/dev/null
if [ $? == 0 ]; then
echo -e "\033[32m nginx配置文件正常 \033[0m"
elif [ $? == 1 ]; then
echo "\033[31m nginx配置文件有问题 \033[0m"
/srv/nginx/sbin/nginx -c /srv/nginx/conf/nginx.conf -t
else
echo "\033[31m nginx请检查配置文件 \033[0m"
fi
}
case $1 in
install)
install
;;
start)
start
;;
stop)
stop
;;
reload)
reload
;;
status)
echo $ng_b
;;
test)
test
;;
*)
echo "start|stop|reload|status|test"
;;
esac