Ubuntu-Nginx安装并设置开机自启

为了安装更快和一次成功,先更换源:

cp /etc/apt/sources.list /etc/apt/sources.list.old
vim /etc/apt/source.list
添加东北大学源:
deb-src http://mirror.neu.edu.cn/ubuntu/ xenial main restricted #Added by software-properties
deb http://mirror.neu.edu.cn/ubuntu/ xenial main restricted
deb-src http://mirror.neu.edu.cn/ubuntu/ xenial restricted multiverse universe #Added by software-properties
deb http://mirror.neu.edu.cn/ubuntu/ xenial-updates main restricted
deb-src http://mirror.neu.edu.cn/ubuntu/ xenial-updates main restricted multiverse universe #Added by software-properties
deb http://mirror.neu.edu.cn/ubuntu/ xenial universe
deb http://mirror.neu.edu.cn/ubuntu/ xenial-updates universe
deb http://mirror.neu.edu.cn/ubuntu/ xenial multiverse
deb http://mirror.neu.edu.cn/ubuntu/ xenial-updates multiverse
deb http://mirror.neu.edu.cn/ubuntu/ xenial-backports main restricted universe multiverse
deb-src http://mirror.neu.edu.cn/ubuntu/ xenial-backports main restricted universe multiverse #Added by software-properties
deb http://archive.canonical.com/ubuntu xenial partner
deb-src http://archive.canonical.com/ubuntu xenial partner
deb http://mirror.neu.edu.cn/ubuntu/ xenial-security main restricted
deb-src http://mirror.neu.edu.cn/ubuntu/ xenial-security main restricted multiverse universe #Added by software-properties
deb http://mirror.neu.edu.cn/ubuntu/ xenial-security universe
deb http://mirror.neu.edu.cn/ubuntu/ xenial-security multiverse
更新
apt-get update
安装依赖:gcc、g++依赖库
apt-get install build-essential
apt-get install libtool
安装 pcre依赖库(http://www.pcre.org/
sudo apt-get install libpcre3 libpcre3-dev
安装 zlib依赖库(http://www.zlib.net
apt-get install zlib1g-dev
安装ssl依赖库
apt-get install openssl
安装Nginx
wget http://nginx.org/download/nginx-1.13.8.tar.gz
tar -zxvf nginx-1.13.8.tar.gz
cd nginx-1.13.8/
./configure --prefix=/usr/local/nginx
sudo make
sudo make install
启动
#启动
/usr/local/nginx/sbin/nginx
#启动并指定配置文件
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
#关闭nginx
/usr/local/nginx/sbin/nginx -s stop
/usr/local/nginx/sbin/nginx -s quit
#重新加载配置
/usr/local/nginx/sbin/nginx -s reload
#查看版本
/usr/local/nginx/sbin/nginx  -v
查看nginx进程
ps -ef|grep nginx

开机自启

在/etc/init.d/目录下创建名为nginx的文件:
sudo touch /etc/init.d/nginx
编辑如下内容:
#!/bin/bash

set -e
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DESC="nginx daemon"
NAME=nginx
DAEMON=/usr/local/nginx/sbin/$NAME
SCRIPTNAME=/etc/init.d/$NAME


# If the daemon file is not found, terminate the script.
test -x $DAEMON || exit 0

d_start() {
        $DAEMON || echo -n " already running"
}

d_stop() {
        $DAEMON –s quit || echo -n " not running"
}

d_reload() {
        $DAEMON –s reload || echo -n " could not reload"
}

case "$1" in
    start)
    echo -n "Starting $DESC: $NAME"
    d_start
    echo "."
    ;;
stop)
    echo -n "Stopping $DESC: $NAME"
    d_stop
    echo "."
    ;;
reload)
    echo -n "Reloading $DESC configuration..."
    d_reload
    echo "reloaded."
    ;;
restart)
    echo -n "Restarting $DESC: $NAME"
    d_stop
# Sleep for two seconds before starting again, this should give the
# Nginx daemon some time to perform a graceful stop.
    sleep 2
    d_start
    echo "."
    ;;
*)
    echo "Usage: $SCRIPTNAME {start|stop|restart|reload}" >&2
    exit 3
    ;;
esac
exit 0
脚本授权
chmod +x /etc/init.d/nginx
加入开机启动
sysv-rc-conf
界面说明(上下键切换到nginx选项,空格选中,q保存退出)
运行级别说明:
S   表示开机后就会运行的服务
0   表示关机
1   表示单用户模式  (类似windows的安全模式)
2   表示无网络服务的多用户模式
3   表示多用户模式
4   系统预留(暂没使用)
5   表示多用户图形模式
6   表示重启
其他命令(以下代码未测试)
#设置开机启动nginx
sudo update-rc.d –f nginx defaults
#移除nginx开机启动
update-rc.d -f nginx remove
#开启开机启动
sysv-rc-conf nginx on
#nginx操作命令
service nginx -s reload | stop | restart | start
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值