好的,我来给你一个完整的Ubuntu下nginx安装和配置教程,使用守护进程方式启动,运行用户为www-data。
完整的 Ubuntu Nginx 安装配置教程
第一步:更新系统并安装nginx
bash
# 1. 更新软件包列表
sudo apt update
# 2. 安装nginx
sudo apt install nginx -y
# 3. 验证安装
nginx -v
第二步:创建www-data用户(通常已存在)
bash
# 检查www-data用户是否存在
id www-data
# 如果不存在,创建www-data用户和组
sudo groupadd -r www-data 2>/dev/null || true
sudo useradd -r -g www-data -s /sbin/nologin -d /var/www -c "Web Server User" www-data 2>/dev/null || true
# 验证用户
id www-data
第三步:配置nginx以www-data用户运行
bash
# 备份原配置文件
sudo cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.backup
# 编辑nginx主配置文件
sudo vim /etc/nginx/nginx.conf
修改 nginx.conf 配置文件:
nginx
# 运行用户设置为 www-data
user www-data;
# worker进程数(通常设置为CPU核心数)
worker_processes auto;
# 错误日志
error_log /var/log/nginx/error.log warn;
# PID文件
pid /run/nginx.pid;
# 以守护进程方式运行
daemon on;
events {
# 单个worker进程的最大并发连接数
worker_connections 1024;
# 使用epoll事件模型
use epoll;
}
http {
# 基本设置
include /etc/nginx/mime.types;
default_type application/octet-stream;
# 日志格式
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
# 访问日志
access_log /var/log/nginx/access.log main;
# 性能优化
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# Gzip压缩
gzip on;
gzip_vary on;
gzip_min_length 1024;
gzip_types text/plain text/css text/xml text/javascript
application/x-javascript application/xml+rss
application/json application/javascript;
# 包含其他配置文件
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
第四步:配置默认站点
bash
# 编辑默认站点配置
sudo vim /etc/nginx/sites-available/default
默认站点配置:
nginx
server {
listen 80 default_server;
listen [::]:80 default_server;
# 网站根目录
root /var/www/html;
index index.html index.htm index.nginx-debian.html;
# 服务器名称
server_name _;
# 访问日志和错误日志
access_log /var/log/nginx/default_access.log;
error_log /var/log/nginx/default_error.log;
# 主要location配置
location / {
try_files $uri $uri/ =404;
}
# 拒绝访问隐藏文件
location ~ /\. {
deny all;
access_log off;
log_not_found off;
}
# PHP支持(如果需要)
# location ~ \.php$ {
# include snippets/fastcgi-php.conf;
# fastcgi_pass unix:/run/php/php8.1-fpm.sock;
# }
}
第五步:设置目录权限
bash
# 创建网站根目录(如果不存在)
sudo mkdir -p /var/www/html
# 设置目录所有者为www-data
sudo chown -R www-data:www-data /var/www/html
# 设置合适的权限
sudo chmod -R 755 /var/www/html
# 设置日志目录权限
sudo chown -R www-data:adm /var/log/nginx
sudo chmod -R 750 /var/log/nginx
# 创建一个测试页面
sudo tee /var/www/html/index.html > /dev/null <<'EOF'
<!DOCTYPE html>
<html>
<head>
<title>Welcome to Nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to Nginx!</h1>
<p>Nginx is running as www-data user.</p>
<p>Server Time: <?php echo date('Y-m-d H:i:s'); ?></p>
</body>
</html>
EOF
# 设置文件所有者
sudo chown www-data:www-data /var/www/html/index.html
第六步:配置systemd服务(守护进程)
Ubuntu安装nginx后会自动创建systemd服务文件,但我们可以查看和优化它。
bash
# 查看现有的服务文件
sudo systemctl cat nginx
# 如果需要自定义,可以创建override文件
sudo systemctl edit nginx
创建或优化 systemd 服务文件(通常在 /lib/systemd/system/nginx.service):
bash
# 查看服务文件位置
systemctl show -p FragmentPath nginx
# 如果需要完全自定义,编辑服务文件
sudo vim /lib/systemd/system/nginx.service
完整的 nginx.service 文件:
ini
[Unit]
Description=A high performance web server and a reverse proxy server
Documentation=man:nginx(8)
After=network.target nss-lookup.target
[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t -q -g 'daemon on; master_process on;'
ExecStart=/usr/sbin/nginx -g 'daemon on; master_process on;'
ExecReload=/usr/sbin/nginx -g 'daemon on; master_process on;' -s reload
ExecStop=-/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid
TimeoutStopSec=5
KillMode=mixed
# 进程管理
Restart=on-failure
RestartSec=10s
# 安全设置
NoNewPrivileges=true
PrivateTmp=true
# 用户设置(进程以root启动,但worker以www-data运行)
# User和Group在nginx.conf中配置
[Install]
WantedBy=multi-user.target
第七步:测试和启动nginx
bash
# 1. 测试配置文件语法
sudo nginx -t
# 2. 如果修改了systemd服务文件,重新加载
sudo systemctl daemon-reload
# 3. 启动nginx服务
sudo systemctl start nginx
# 4. 设置开机自启动
sudo systemctl enable nginx
# 5. 查看服务状态
sudo systemctl status nginx
# 6. 验证nginx进程运行用户
ps aux | grep nginx
# 应该看到类似输出:
# root 1234 0.0 0.1 nginx: master process
# www-data 1235 0.0 0.2 nginx: worker process
# www-data 1236 0.0 0.2 nginx: worker process
第八步:配置防火墙(如果启用了UFW)
bash
# 查看UFW状态
sudo ufw status
# 允许HTTP流量
sudo ufw allow 'Nginx HTTP'
# 或者允许HTTPS
sudo ufw allow 'Nginx HTTPS'
# 或者允许两者
sudo ufw allow 'Nginx Full'
# 查看UFW应用列表
sudo ufw app list
# 重新加载防火墙
sudo ufw reload
第九步:测试nginx是否正常工作
bash
# 1. 使用curl测试本地访问
curl http://localhost
# 2. 查看nginx监听的端口
sudo ss -tlnp | grep nginx
# 3. 查看nginx进程
ps aux | grep nginx
# 4. 查看nginx日志
sudo tail -f /var/log/nginx/access.log
sudo tail -f /var/log/nginx/error.log
# 5. 测试配置重载(不中断服务)
sudo systemctl reload nginx
第十步:常用管理命令
bash
# 启动nginx
sudo systemctl start nginx
# 停止nginx
sudo systemctl stop nginx
# 重启nginx
sudo systemctl restart nginx
# 重新加载配置(不中断服务,推荐)
sudo systemctl reload nginx
# 查看状态
sudo systemctl status nginx
# 查看nginx版本和编译参数
nginx -V
# 测试配置文件
sudo nginx -t
# 查看实时日志
sudo journalctl -u nginx -f
# 查看最近的日志
sudo journalctl -u nginx -n 50 --no-pager
# 查看nginx打开的文件
sudo lsof -c nginx
# 查看nginx监听的端口
sudo netstat -tlnp | grep nginx
# 或
sudo ss -tlnp | grep nginx
完整的一键安装脚本
将以下内容保存为 install_nginx.sh:
bash
#!/bin/bash
# Ubuntu Nginx 安装配置脚本
# 守护进程方式运行,用户为www-data
set -e
echo "=== Ubuntu Nginx 安装和配置 ==="
echo ""
# 检查是否为root用户
if [[ $EUID -ne 0 ]]; then
echo "错误:此脚本需要root权限运行"
echo "请使用: sudo bash $0"
exit 1
fi
# 1. 更新系统
echo "[1/9] 更新系统软件包..."
apt update
# 2. 安装nginx
echo "[2/9] 安装nginx..."
apt install nginx -y
# 3. 检查www-data用户
echo "[3/9] 检查www-data用户..."
id www-data &>/dev/null || {
groupadd -r www-data
useradd -r -g www-data -s /sbin/nologin -d /var/www -c "Web Server User" www-data
}
# 4. 备份原配置
echo "[4/9] 备份原配置文件..."
cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.backup.$(date +%Y%m%d%H%M%S)
# 5. 配置nginx.conf
echo "[5/9] 配置nginx主配置文件..."
cat > /etc/nginx/nginx.conf <<'EOF'
user www-data;
worker_processes auto;
pid /run/nginx.pid;
error_log /var/log/nginx/error.log warn;
daemon on;
events {
worker_connections 1024;
use epoll;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
gzip on;
gzip_vary on;
gzip_min_length 1024;
gzip_types text/plain text/css text/xml text/javascript
application/x-javascript application/xml+rss
application/json application/javascript;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
EOF
# 6. 设置目录权限
echo "[6/9] 设置目录权限..."
mkdir -p /var/www/html
chown -R www-data:www-data /var/www/html
chmod -R 755 /var/www/html
chown -R www-data:adm /var/log/nginx
chmod -R 750 /var/log/nginx
# 7. 创建测试页面
echo "[7/9] 创建测试页面..."
cat > /var/www/html/index.html <<'EOF'
<!DOCTYPE html>
<html>
<head>
<title>Welcome to Nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
background: #f0f0f0;
padding: 50px;
}
.container {
background: white;
padding: 30px;
border-radius: 10px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}
h1 { color: #009639; }
.info { color: #666; margin-top: 20px; }
</style>
</head>
<body>
<div class="container">
<h1>✅ Nginx 安装成功!</h1>
<p>Nginx 已成功安装并以守护进程方式运行。</p>
<div class="info">
<p><strong>运行用户:</strong>www-data</p>
<p><strong>配置文件:</strong>/etc/nginx/nginx.conf</p>
<p><strong>网站目录:</strong>/var/www/html</p>
</div>
</div>
</body>
</html>
EOF
chown www-data:www-data /var/www/html/index.html
# 8. 测试配置并启动
echo "[8/9] 测试配置文件..."
nginx -t
echo "[8/9] 重启nginx服务..."
systemctl daemon-reload
systemctl restart nginx
systemctl enable nginx
# 9. 验证安装
echo "[9/9] 验证安装..."
sleep 2
echo ""
echo "=== 安装完成 ==="
echo ""
echo "✅ Nginx版本: $(nginx -v 2>&1)"
echo "✅ 服务状态: $(systemctl is-active nginx)"
echo "✅ 开机自启: $(systemctl is-enabled nginx)"
echo ""
echo "运行进程:"
ps aux | grep nginx | grep -v grep
echo ""
echo "监听端口:"
ss -tlnp | grep nginx
echo ""
echo "访问测试:"
echo " 本地访问: curl http://localhost"
echo " 浏览器访问: http://$(hostname -I | awk '{print $1}')"
echo ""
echo "常用命令:"
echo " 启动: sudo systemctl start nginx"
echo " 停止: sudo systemctl stop nginx"
echo " 重启: sudo systemctl restart nginx"
echo " 重载: sudo systemctl reload nginx"
echo " 状态: sudo systemctl status nginx"
echo " 日志: sudo journalctl -u nginx -f"
echo ""
使用一键脚本安装
bash
# 1. 保存脚本
vim install_nginx.sh
# 2. 添加执行权限
chmod +x install_nginx.sh
# 3. 运行脚本
sudo ./install_nginx.sh
安装后验证
bash
# 完整验证脚本
echo "=== Nginx 安装验证 ==="
# 1. 检查nginx版本
echo "1. Nginx版本:"
nginx -v
# 2. 检查服务状态
echo -e "\n2. 服务状态:"
systemctl status nginx --no-pager -l
# 3. 检查进程用户
echo -e "\n3. 进程用户:"
ps aux | grep nginx | grep -v grep
# 4. 检查监听端口
echo -e "\n4. 监听端口:"
ss -tlnp | grep nginx
# 5. 测试HTTP访问
echo -e "\n5. HTTP访问测试:"
curl -I http://localhost
# 6. 检查配置文件
echo -e "\n6. 配置文件测试:"
nginx -t
# 7. 查看日志
echo -e "\n7. 最近日志:"
tail -n 5 /var/log/nginx/access.log
tail -n 5 /var/log/nginx/error.log
总结
现在你的Ubuntu系统上已经成功安装了nginx,并且:
✅ 以守护进程(daemon)方式运行
✅ worker进程以www-data用户运行
✅ 通过systemd管理,开机自启
✅ 配置了合理的权限和日志
✅ 可以使用标准的systemctl命令管理