編譯安裝nginx
首先創(chuàng)建一個目錄押框,用來存放腳本
[root@localhost ~]# mkdir /nginx
[root@localhost ~]# cd /nginx/
[root@localhost nginx]# chmod +x nginx.sh //賦予腳本的執(zhí)行權限
下載安裝nginx的安裝包
可以去nginx官網下載相應版本的tar包
[root@localhost nginx]# wget https://nginx.org/download/nginx-1.20.1.tar.gz
在同級目錄創(chuàng)建一個放安裝包的目錄
[root@localhost nginx]# mkdir packages/
[root@localhost packages]# ls
nginx-1.20.1.tar.gz
創(chuàng)建腳本文件
[root@localhost nginx]# touch nginx.sh
編寫腳本
[root@localhost nginx]# cat nginx.sh
#!/bin/bash
route=/usr/local
server=/usr/lib/systemd/system
id nginx &>/dev/null //判斷是否有nginx用戶肠仪,沒有就創(chuàng)建用戶
if [ $? -ne 0 ];then
useradd -r -M -s /sbin/nologin nginx
fi
yum -y install pcre-devel pcre gcc gcc-c++ openssl-devel zlib zlib-devel make vim wget openssl openssl-devel gd-devel //解決依賴關系
mkdir /var/log/nginx
chown -R nginx.nginx /var/log/nginx
if [ ! -d $route/nginx-1.20.1 ];then
tar xf packages/nginx-1.20.1.tar.gz -C $route //解壓到/usr/local
fi
cd $route/nginx-1.20.1
if [ ! -d $route/nginx ];then //若沒有nginx目錄就進行進行編譯
./configure --prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-debug \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_image_filter_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--http-log-path=/var/log/nginx/access.log \
--error-log-path=/var/log/nginx/error.log
make && make install
fi
cd $server //配置service文件使用systemctl控制nginx
cat > nginx.service << EOF
[Unit]
Description=Nginx server daemon
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecStop=/usr/local/nginx/sbin/nginx -s quit
ExecReload=/bin/kill -HUP \$MAINPID
[Install]
WantedBy=multi-user.target
EOF
echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh //配置環(huán)境變量
bash /etc/profile.d/nginx.sh //重新加載生效
systemctl daemon-reload
systemctl enable --now nginx.service
if [ $? -eq 0 ];then
echo "nginx is ok"
else
echo "nginx is error"
fi
開機自啟成功就輸出nginx is ok,否則就輸出nginx is error。