ubnutu系统安装nginx并设置为系统服务

 首先安装依赖

apt-get install gcc
apt-get install libpcre3 libpcre3-dev
apt-get install zlib1g zlib1g-dev
sudo apt-get install openssl 
sudo apt-get install libssl-dev

然后下载解压nginx

cd /usr/local
mkdir ./nginx
cd ./nginx
wget http://nginx.org/download/nginx-1.16.0.tar.gz
tar -xvf ./nginx-1.16.0.tar.gz

编译安装nginx

cd ./nginx-1.16.0
./configure
make
make install

 创建nginx.service文件并赋予权限

cd /lib/systemd/system
touch ./nginx.service
chmod 777 nginx.service

编辑文件nginx.service

#  This file is part of systemd.
#  systemd is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or
#  (at your option) any later version.
# This unit gets pulled automatically into multi-user.target by
[Unit]
Description=/home/user/service/nginx.sh Compatibility
ConditionFileIsExecutable=/home/user/service/nginx.sh
After=network.target
[Service]
Type=forking
ExecStart=/home/user/service/nginx.sh
TimeoutSec=0
RemainAfterExit=yes
#### 添加 ####
[Install]
WantedBy=multi-user.target
Alias=nginx.service

创建nginx.sh文件

cd /home/user/service/
vi ./nginx.sh

内容:

#!/bin/sh -e
cd /usr/local/nginx/sbin
./nginx

赋予权限

chmod 677 ./nginx.sh

创建链接

sudo ln -s /lib/systemd/system/nginx.service /etc/systemd/system

设置开机自启

systemctl enable nginx.service

启动服务

systemctl start nginx.service

查看服务状态

systemctl status nginx.service

nginx已启动

 浏览器输入本地IP

完成

posted @ 2022-07-15 14:47  卷心菜的奇妙历险  阅读(306)  评论(0编辑  收藏  举报