SagerXiao's Blog

Restart of an old programmer

配置Nginx和uWsig服务开机自动启动

1.配置Nginx服务开机自动启动

1.1创建 Nginx 开机启动脚本

add auto run
1
vi /etc/init.d/nginx

将以下内容写到该脚本中

auto start scripts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/bin/bash
nginx=/usr/sbin/nginx
conf=/etc/nginx/nginx.conf

case $1 in
    start)
        echo -n "Starting Nginx"
        $nginx -c $conf
        echo " done"
    ;;

    stop)
        echo -n "Stopping Nginx"
        $nginx -s stop
        echo " done"
    ;;

    test)
        $nginx -t -c $conf
    ;;

    reload)
        echo -n "Reloading Nginx"
        $nginx -s reload
        echo " done"
    ;;

    restart)
        $0 stop
        $0 start
    ;;

    show)
        ps -aux|grep nginx
    ;;

    *)
        echo -n "Usage: $0 {start|restart|reload|stop|test|show}"
    ;;

esac

1.2为 nginx.sh 脚本设置可执行属性

executable
1
chmod +x /etc/init.d/nginx

1.3添加 Nginx 为系统服务(开机自动启动)

add to sysconfig
1
2
chkconfig --add nginx
chkconfig nginx on

1.4启动 Nginx

start service
1
service nginx start

1.5在不停止 Nginx 服务的情况下平滑变更 Nginx 配置

修改 /usr/local/webserver/nginx/conf/nginx.conf 配置文件后,请执行以下命令检查配置文件是否正确:

test config and reload
1
2
service nginx test
service nginx reload

2.配置uWsgi服务开机自动启动

2.1创建 uWsgi 开机启动脚本

add auto run
1
vi /etc/init.d/uwsgi

将以下内容写到该脚本中

auto start scripts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#!/bin/bash
uwsgi=/usr/bin/uwsgi
api_conf=/etc/uwsgi/apps-enabled/project-api.ini
web_conf=/etc/uwsgi/apps-enabled/project-web.ini

case $1 in
    start)
        echo -n "Starting uWsgi"
        nohup $uwsgi -i $api_conf >/var/log/uwsgi/project-api.log 2>&1 &
        nohup $uwsgi -i $web_conf >/var/log/uwsgi/project-web.log 2>&1 &
        echo " done"
    ;;

    stop)
        echo -n "Stopping uWsgi"
        killall -9 uwsgi
        echo " done"
    ;;

    restart)
        $0 stop
        $0 start
    ;;

    show)
        ps -ef|grep uwsgi
    ;;

    *)
        echo -n "Usage: $0 {start|restart|stop|show}"
    ;;

esac

2.2为 uwsgi 脚本设置可执行属性

executable
1
chmod +x /etc/init.d/uwsgi

2.3添加 uWsgi 为系统服务(开机自动启动)

add to sysconfig
1
2
chkconfig --add uwsgi
chkconfig uwsgi on

2.4启动 uWsgi

start service
1
service uwsgi start

Comments