添加脚本用service启动nginx,脚本具体内容
时间: 2023-11-28 11:48:15 浏览: 43
可以创建一个名为 `nginx.sh` 的脚本,内容如下:
```
#!/bin/bash
# 检查 nginx 是否已经启动
if pgrep nginx > /dev/null
then
echo "Nginx is already running"
else
# 启动 nginx
echo "Starting Nginx..."
sudo service nginx start
fi
```
然后通过 `sudo chmod +x nginx.sh` 命令给脚本赋予执行权限。
最后,执行 `./nginx.sh` 即可启动 nginx,如果 nginx 已经启动,则会提示 "Nginx is already running"。
相关问题
nginx自动启动脚本
以下是一个简单的nginx自动启动脚本,可以在/etc/init.d/nginx文件中进行保存:
```
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /etc/nginx/nginx.conf
# config: /etc/sysconfig/nginx
# pidfile: /var/run/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
nginx="/usr/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/etc/nginx/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
configtest || return $?
stop
sleep 1
start
}
reload() {
configtest || return $?
echo -n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}
configtest() {
$nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart)
$1
;;
reload)
$1
;;
status)
rh_status
;;
configtest)
$1
;;
*)
echo $"Usage: $0 {start|stop|status|restart|reload|configtest}"
exit 2
esac
```
保存后,需要给该文件添加可执行权限:
```
chmod +x /etc/init.d/nginx
```
然后可以使用以下命令来启动、停止、重启、重新加载、检查状态或测试配置:
```
service nginx start
service nginx stop
service nginx restart
service nginx reload
service nginx status
service nginx configtest
```
此脚本适用于CentOS/Redhat等基于SysV的Linux发行版。对于其他发行版,可能需要稍作修改。
用shell脚本写一个nginx一键部署脚本
好的,以下是一个简单的Nginx一键部署脚本,使用前请确保已经安装了Nginx和相关依赖:
```bash
#!/bin/bash
# 安装依赖
yum install -y wget gcc-c++ make zlib-devel openssl-devel
# 下载Nginx源码并解压
wget http://nginx.org/download/nginx-1.21.1.tar.gz
tar -zxvf nginx-1.21.1.tar.gz
cd nginx-1.21.1
# 配置编译参数
./configure --prefix=/usr/local/nginx \
--with-pcre \
--with-http_ssl_module
# 编译并安装Nginx
make && make install
echo "Nginx安装完成!"
# 配置Nginx服务
cat << EOF > /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF
# 启动Nginx服务
systemctl daemon-reload
systemctl start nginx
systemctl enable nginx
echo "Nginx服务已启动!"
```
以上脚本会自动编译和安装Nginx,并配置为系统服务,并启动Nginx服务。使用时,只需要将脚本保存为.sh文件,然后使用root用户执行即可。
阅读全文