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 @   卷心菜的奇妙历险  阅读(306)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 25岁的心里话
点击右上角即可分享
微信分享提示