Skip to content

19.2 服务器环境配置

服务器环境概述

服务器环境配置是网站上线的重要步骤,它决定了网站的运行性能和安全性。对于 PHP 网站,通常需要配置以下组件:

  • Web 服务器:如 Apache、Nginx
  • PHP:PHP 解释器
  • 数据库:如 MySQL、PostgreSQL
  • 其他工具:如 FTP 服务、防火墙等

操作系统选择

常见的服务器操作系统有:

  • Linux:如 Ubuntu、CentOS、Debian,适合大多数网站,稳定、安全、性能好
  • Windows Server:适合需要使用 Windows 特定功能的网站

服务器环境配置步骤

步骤 1:更新系统

首先,更新服务器系统,确保系统处于最新状态:

Ubuntu/Debian

bash
sudo apt update
sudo apt upgrade -y

CentOS/RHEL

bash
sudo yum update -y
sudo yum upgrade -y

步骤 2:安装 Web 服务器

安装 Apache

Ubuntu/Debian

bash
sudo apt install apache2 -y

CentOS/RHEL

bash
sudo yum install httpd -y
sudo systemctl start httpd
sudo systemctl enable httpd

安装 Nginx

Ubuntu/Debian

bash
sudo apt install nginx -y

CentOS/RHEL

bash
sudo yum install nginx -y
sudo systemctl start nginx
sudo systemctl enable nginx

步骤 3:安装 PHP

安装 PHP 7.4

Ubuntu/Debian

bash
sudo apt install php7.4 php7.4-mysql php7.4-curl php7.4-gd php7.4-mbstring php7.4-xml php7.4-zip -y

CentOS/RHEL

bash
sudo yum install epel-release -y
sudo yum install remi-release -y
sudo yum-config-manager --enable remi-php74
sudo yum install php php-mysql php-curl php-gd php-mbstring php-xml php-zip -y

验证 PHP 安装

bash
php -v

步骤 4:安装数据库

安装 MySQL

Ubuntu/Debian

bash
sudo apt install mysql-server -y
sudo mysql_secure_installation

CentOS/RHEL

bash
sudo yum install mysql-server -y
sudo systemctl start mysqld
sudo systemctl enable mysqld
sudo mysql_secure_installation

安装 PostgreSQL

Ubuntu/Debian

bash
sudo apt install postgresql postgresql-contrib -y

CentOS/RHEL

bash
sudo yum install postgresql postgresql-server -y
sudo postgresql-setup --initdb
sudo systemctl start postgresql
sudo systemctl enable postgresql

步骤 5:配置 Web 服务器

Apache 配置

创建虚拟主机配置

bash
sudo nano /etc/apache2/sites-available/yourdomain.conf

配置内容

apache
<VirtualHost *:80>
    ServerName www.yourdomain.com
    ServerAlias yourdomain.com
    DocumentRoot /var/www/yourdomain
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
    <Directory /var/www/yourdomain>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

启用虚拟主机

bash
sudo a2ensite yourdomain.conf
sudo a2enmod rewrite
sudo systemctl restart apache2

Nginx 配置

创建虚拟主机配置

bash
sudo nano /etc/nginx/sites-available/yourdomain.conf

配置内容

nginx
server {
    listen 80;
    server_name www.yourdomain.com yourdomain.com;
    root /var/www/yourdomain;
    index index.php index.html index.htm;
    location / {
        try_files $uri $uri/ =404;
    }
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.4-fpm.sock;
    }
    location ~ /\.ht {
        deny all;
    }
}

启用虚拟主机

bash
sudo ln -s /etc/nginx/sites-available/yourdomain.conf /etc/nginx/sites-enabled/
sudo systemctl restart nginx

步骤 6:创建网站目录

bash
sudo mkdir -p /var/www/yourdomain
sudo chown -R www-data:www-data /var/www/yourdomain
sudo chmod -R 755 /var/www/yourdomain

步骤 7:配置防火墙

Ubuntu/Debian

bash
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw enable

CentOS/RHEL

bash
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload

步骤 8:安装 FTP 服务

安装 vsftpd

bash
sudo apt install vsftpd -y  # Ubuntu/Debian
sudo yum install vsftpd -y  # CentOS/RHEL

配置 vsftpd

bash
sudo nano /etc/vsftpd.conf

配置内容

txt
anonymous_enable=NO
local_enable=YES
write_enable=YES
local_umask=022
dirmessage_enable=YES
xferlog_enable=YES
connect_from_port_20=YES
xferlog_std_format=YES
chroot_local_user=YES
allow_writeable_chroot=YES
listen=NO
listen_ipv6=YES
pasv_min_port=40000
pasv_max_port=50000

重启 vsftpd

bash
sudo systemctl restart vsftpd
sudo systemctl enable vsftpd

实战演练

场景:配置 Ubuntu 服务器环境

步骤 1:更新系统

bash
sudo apt update
sudo apt upgrade -y

步骤 2:安装 Apache、PHP 和 MySQL

bash
sudo apt install apache2 php7.4 php7.4-mysql php7.4-curl php7.4-gd php7.4-mbstring php7.4-xml php7.4-zip mysql-server -y

步骤 3:配置 MySQL

bash
sudo mysql_secure_installation

步骤 4:创建虚拟主机

bash
sudo nano /etc/apache2/sites-available/yourdomain.conf

配置内容

apache
<VirtualHost *:80>
    ServerName www.yourdomain.com
    ServerAlias yourdomain.com
    DocumentRoot /var/www/yourdomain
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
    <Directory /var/www/yourdomain>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

步骤 5:启用虚拟主机

bash
sudo a2ensite yourdomain.conf
sudo a2enmod rewrite
sudo systemctl restart apache2

步骤 6:创建网站目录

bash
sudo mkdir -p /var/www/yourdomain
sudo chown -R www-data:www-data /var/www/yourdomain
sudo chmod -R 755 /var/www/yourdomain

步骤 7:测试环境

创建 info.php 文件:

bash
sudo nano /var/www/yourdomain/info.php

内容

php
<?php
phpinfo();
?>

在浏览器中访问 http://www.yourdomain.com/info.php,如果能看到 PHP 信息页面,说明环境配置成功。

总结

服务器环境配置是网站上线的关键步骤,需要正确安装和配置 Web 服务器、PHP、数据库等组件。本文介绍了在 Ubuntu 和 CentOS 系统上配置服务器环境的详细步骤,包括更新系统、安装 Web 服务器、安装 PHP、安装数据库、配置 Web 服务器、创建网站目录、配置防火墙和安装 FTP 服务等。

通过本文的学习,你应该能够独立配置服务器环境,为网站上线做好准备。

© 2026 编程马·菜鸟教程 版权所有