下面以 CentOS + LAMP(Linux + Apache + MySQL/MariaDB + PHP) 为例,说明如何搭建一个网站站点(以 CentOS 7/8 常见操作为准)。
yum install -y httpd
systemctl start httpd
systemctl enable httpd
测试:浏览器访问 http://服务器IP
yum install -y mariadb-server
systemctl start mariadb
systemctl enable mariadb
初始化数据库:
mysql_secure_installation
yum install -y php php-mysql
systemctl restart httpd
测试 PHP:
echo "<?php phpinfo(); ?>" > /var/www/html/info.php
访问 http://IP/info.php
假设站点域名为 example.com
mkdir -p /var/www/example.com/public_html
chown -R apache:apache /var/www/example.com
chmod -R 755 /var/www/example.com
vi /etc/httpd/conf.d/example.com.conf
内容示例:
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example.com/public_html
<Directory /var/www/example.com/public_html>
AllowOverride All
Require all granted
</Directory>
ErrorLog /var/log/httpd/example.com-error.log
CustomLog /var/log/httpd/example.com-access.log combined
</VirtualHost>
systemctl restart httpd
将网站文件放到:
/var/www/example.com/public_html
例如:
firewall-cmd --permanent --add-service=http
firewall-cmd --reload
setsebool -P httpd_can_network_connect 1
chcon -R -t httpd_sys_content_t /var/www/example.com
在域名解析平台添加:
A记录 example.com → 服务器IP
直接放 index.html
确保 PHP 已安装
CREATE DATABASE example_db;
CREATE USER 'example_user'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON example_db.* TO 'example_user'@'localhost';
FLUSH PRIVILEGES;
如果你用的是 CentOS 8 / Stream / Rocky / Alma 或想用 Nginx + PHP,也可以告诉我,我可以给你对应方案。