# Linux 环境下搭建 Web 服务器的完整指南
在 Linux 系统上搭建 Web 服务器是每个开发者和运维人员都应该掌握的基础技能。本文将详细介绍如何使用主流工具在 Linux 上部署功能完整的 Web 服务器环境。
## 环境准备与系统配置
### 系统要求检查
在开始之前,确保系统满足基本要求并完成基础配置:
```bash
# 检查系统版本
lsb_release -a
cat /etc/os-release
# 更新系统软件包
sudo apt update && sudo apt upgrade -y
# 安装基础工具
sudo apt install -y curl wget vim git build-essential
```
### 防火墙配置
正确配置防火墙确保服务器安全:
```bash
# 检查防火墙状态
sudo ufw status
# 启用防火墙
sudo ufw enable
# 开放必要端口
sudo ufw allow 22/tcp # SSH
sudo ufw allow 80/tcp # HTTP
sudo ufw allow 443/tcp # HTTPS
# 验证规则
sudo ufw status numbered
```
## Apache HTTP Server 安装与配置
### 安装 Apache2
Apache 是历史悠久且稳定的 Web 服务器选择:
```bash
# 安装 Apache2
sudo apt install -y apache2
# 启动并启用开机自启
sudo systemctl start apache2
sudo systemctl enable apache2
# 检查服务状态
sudo systemctl status apache2
<"nijq.mobi">
<"ufhc.mobi">
<"hlux.mobi">
```
### 基础配置调整
修改 Apache 默认配置以适应需求:
```bash
# 备份原始配置
sudo cp /etc/apache2/apache2.conf /etc/apache2/apache2.conf.backup
# 编辑主配置文件
sudo vim /etc/apache2/apache2.conf
# 在配置文件中调整以下参数:
# ServerName localhost
# KeepAlive On
# MaxKeepAliveRequests 100
# KeepAliveTimeout 5
```
创建自定义虚拟主机:
```bash
# 创建网站目录
sudo mkdir -p /var/www/example.com/html
sudo chown -R $USER:$USER /var/www/example.com/html
sudo chmod -R 755 /var/www/example.com
# 创建示例页面
sudo tee /var/www/example.com/html/index.html <<EOF
<!DOCTYPE html>
<html>
<head>
<title>Welcome to Example.com</title>
</head>
<body>
<h1>Success! Apache server is working.</h1>
<p>This is the default page for example.com</p>
</body>
<"lqiu.mobi">
<"ittv.mobi">
<"fj-gov.cn">
</html>
EOF
```
创建虚拟主机配置文件:
```bash
sudo tee /etc/apache2/sites-available/example.com.conf <<EOF
<VirtualHost *:80>
ServerAdmin webmaster@example.com
ServerName example.com
ServerAlias
DocumentRoot /var/www/example.com/html
ErrorLog \${APACHE_LOG_DIR}/error.log
CustomLog \${APACHE_LOG_DIR}/access.log combined
<Directory /var/www/example.com/html>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
<"yj-gov.cn">
<"kk-gov.cn">
<"kzb-gov.cn">
</VirtualHost>
EOF
```
启用站点并测试:
```bash
# 禁用默认站点
sudo a2dissite 000-default.conf
# 启用新站点
sudo a2ensite example.com.conf
# 测试配置
sudo apache2ctl configtest
# 重启 Apache
sudo systemctl restart apache2
```
## Nginx 服务器部署
### 安装 Nginx
Nginx 以高性能和低内存占用著称:
```bash
# 安装 Nginx
sudo apt install -y nginx
# 启动并启用服务
sudo systemctl start nginx
sudo systemctl enable nginx
# 检查状态
sudo systemctl status nginx
```
### Nginx 站点配置
创建 Nginx 服务器块配置:
```bash
# 创建网站目录
sudo mkdir -p /var/www/nginx-example.com/html
sudo chown -R $USER:$USER /var/www/nginx-example.com/html
sudo chmod -R 755 /var/www/nginx-example.com
# 创建示例页面
sudo tee /var/www/nginx-example.com/html/index.html <<EOF
<!DOCTYPE html>
<html>
<head>
<title>Nginx Server</title>
</head>
<body>
<h1>Nginx is running successfully!</h1>
<p>This server is powered by Nginx.</p>
</body>
<"zbw-gov.cn">
<"zqw-gov.cn">
<"tiyu-gov.cn">
</html>
EOF
```
创建 Nginx 服务器块配置:
```bash
sudo tee /etc/nginx/sites-available/nginx-example.com <<EOF
server {
listen 80;
listen [::]:80;
root /var/www/nginx-example.com/html;
index index.html index.htm index.nginx-debian.html;
server_name nginx-example.com
location / {
try_files \$uri \$uri/ =404;
}
location ~ /\.ht {
deny all;
}
}
EOF
```
启用配置并测试:
```bash
# 创建符号链接启用站点
sudo ln -s /etc/nginx/sites-available/nginx-example.com /etc/nginx/sites-enabled/
# 测试配置
sudo nginx -t
# 重启 Nginx
sudo systemctl restart nginx
```
## 数据库服务器安装
### MySQL 安装配置
安装并配置 MySQL 数据库:
```bash
# 安装 MySQL 服务器
sudo apt install -y mysql-server
# 启动服务
sudo systemctl start mysql
sudo systemctl enable mysql
# 运行安全脚本
sudo mysql_secure_installation
```
创建数据库和用户:
```bash
# 登录 MySQL
sudo mysql -u root -p
# 在 MySQL 提示符下执行:
CREATE DATABASE webapp;
CREATE USER 'webuser'@'localhost' IDENTIFIED BY 'secure_password';
GRANT ALL PRIVILEGES ON webapp.* TO 'webuser'@'localhost';
FLUSH PRIVILEGES;
<"zw-gov.cn">
<"cbadmk.com">
<"nbanbazb.com">
EXIT;
```
### PostgreSQL 安装配置
另一种流行的数据库选择:
```bash
# 安装 PostgreSQL
sudo apt install -y postgresql postgresql-contrib
# 启动服务
sudo systemctl start postgresql
sudo systemctl enable postgresql
```
配置 PostgreSQL:
```bash
# 切换到 postgres 用户
sudo -u postgres psql
# 在 PostgreSQL 提示符下:
CREATE DATABASE webapp;
CREATE USER webuser WITH PASSWORD 'secure_password';
GRANT ALL PRIVILEGES ON DATABASE webapp TO webuser;
\q
```
## PHP 环境配置
### 安装 PHP 及扩展
为动态网站添加 PHP 支持:
```bash
# 安装 PHP 和常用扩展
sudo apt install -y php libapache2-mod-php php-mysql \
php-cli php-curl php-gd php-json php-mbstring \
php-xml php-zip php-fpm
# 检查 PHP 版本
php --version
```
配置 PHP-FPM:
```bash
# 编辑 PHP 配置文件
sudo vim /etc/php/8.1/fpm/php.ini
# 调整重要参数:
# memory_limit = 256M
# upload_max_filesize = 64M
# post_max_size = 64M
# max_execution_time = 300
<"balivs.com">
<"sanzhibo.com">
<"kwzhibo.com">
# 重启 PHP-FPM
sudo systemctl restart php8.1-fpm
```
创建 PHP 测试页面:
```bash
sudo tee /var/www/example.com/html/phpinfo.php <<EOF
<?php
phpinfo();
?>
EOF
```
## SSL/TLS 证书配置
### 使用 Let's Encrypt 获取免费证书
安装 Certbot 并配置 HTTPS:
```bash
# 安装 Certbot
sudo apt install -y certbot python3-certbot-apache
# 获取 SSL 证书(Apache)
sudo certbot --apache -d example.com -d
# 获取 SSL 证书(Nginx)
sudo certbot --nginx -d nginx-example.com -d
# 测试自动续期
sudo certbot renew --dry-run
```
## 应用部署实战
### 部署 WordPress 网站
以 WordPress 为例展示完整应用部署:
```bash
# 进入网站目录
cd /var/www/example.com/html
# 下载 WordPress
wget https://wordpress.org/latest.tar.gz
tar -xzvf latest.tar.gz
mv wordpress/* .
<"stbooking.cn">
<"niyqmot.cn">
<"bkapint.cn">
rm -rf wordpress latest.tar.gz
# 设置权限
sudo chown -R www-data:www-data /var/www/example.com/html
sudo find /var/www/example.com/html -type d -exec chmod 755 {} \;
sudo find /var/www/example.com/html -type f -exec chmod 644 {} \;
```
创建 WordPress 配置文件:
```bash
cp wp-config-sample.php wp-config.php
# 编辑配置文件,添加数据库信息
sudo vim wp-config.php
```
配置 Apache 支持 WordPress:
```bash
sudo tee /etc/apache2/sites-available/wordpress.conf <<EOF
<VirtualHost *:80>
ServerName wordpress.example.com
DocumentRoot /var/www/example.com/html
<Directory /var/www/example.com/html>
Options FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog \${APACHE_LOG_DIR}/wordpress_error.log
CustomLog \${APACHE_LOG_DIR}/wordpress_access.log combined
</VirtualHost>
EOF
```
## 性能优化与监控
### Web 服务器优化
调整配置提升服务器性能:
```bash
# Apache 优化配置
sudo vim /etc/apache2/mods-available/mpm_prefork.conf
# 调整参数示例:
# StartServers 5
# MinSpareServers 5
# MaxSpareServers 10
# MaxRequestWorkers 150
# MaxConnectionsPerChild 0
```
Nginx 性能优化:
```bash
sudo vim /etc/nginx/nginx.conf
# 在 http 块中添加:
# gzip on;
# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
# client_max_body_size 64M;
```
### 服务器监控
设置基础监控确保服务稳定:
```bash
# 安装监控工具
sudo apt install -y htop iotop nethogs
# 创建服务状态检查脚本
sudo tee /usr/local/bin/check-services.sh <<'EOF'
#!/bin/bash
echo "=== 服务状态检查 ==="
systemctl is-active apache2 && echo "Apache: 运行中" || echo "Apache: 停止"
systemctl is-active nginx && echo "Nginx: 运行中" || echo "Nginx: 停止"
systemctl is-active mysql && echo "MySQL: 运行中" || echo "MySQL: 停止"
echo -e "\n=== 系统资源 ==="
free -h
echo -e "\n=== 磁盘使用 ==="
df -h
EOF
sudo chmod +x /usr/local/bin/check-services.sh
```
设置定时任务监控:
```bash
# 添加定时检查
(crontab -l 2>/dev/null; echo "*/5 * * * * /usr/local/bin/check-services.sh >> /var/log/service-monitor.log") | crontab -
```
## 安全加固措施
### 基础安全配置
实施基本安全最佳实践:
```bash
# 禁用不必要的服务
sudo systemctl disable bluetooth
sudo systemctl stop bluetooth
# 配置 SSH 安全
sudo vim /etc/ssh/sshd_config
# 建议修改:
# Port 2222 # 修改默认端口
# PermitRootLogin no
<"nanyangdq.cn">
<"anbdq09.cn">
<"vmcnors.cn">
# PasswordAuthentication no # 使用密钥认证
# 重启 SSH 服务
sudo systemctl restart sshd
```
### 文件权限安全
设置安全的文件权限:
```bash
# Web 目录权限设置
sudo find /var/www -type f -exec chmod 644 {} \;
sudo find /var/www -type d -exec chmod 755 {} \;
sudo chown -R www-data:www-data /var/www
# 保护配置文件
sudo chmod 600 /etc/mysql/my.cnf
sudo chmod 600 /etc/apache2/apache2.conf
```
## 故障排除指南
### 常见问题解决
日志检查和分析:
```bash
# 查看 Apache 错误日志
sudo tail -f /var/log/apache2/error.log
# 查看 Nginx 错误日志
sudo tail -f /var/log/nginx/error.log
# 检查系统日志
sudo journalctl -u apache2 -f
sudo journalctl -u nginx -f
```
端口和服务状态检查:
```bash
# 检查端口监听状态
sudo netstat -tulpn | grep :80
sudo netstat -tulpn | grep :443
# 检查防火墙规则
sudo ufw status verbose
# 测试网站响应
curl -I http://localhost
```
通过以上完整的步骤,您已经成功在 Linux 系统上搭建了功能完善的 Web 服务器环境。这个环境包含了 Web 服务器、数据库、PHP 支持和安全配置,为部署各种 Web 应用奠定了坚实基础。记得定期更新系统和维护服务器,确保服务的安全性和稳定性。
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/19545/viewspace-3101891/ ,如需转载,请注明出处,否则将追究法律责任。