CentOS7配置网站环境
系统环境准备
执行系统更新确保所有软件包为最新版本:
yum update -y
安装常用工具包:
yum install -y wget curl vim
Web服务器安装
Apache安装方案
yum install -y httpd
systemctl start httpd
systemctl enable httpd
Nginx安装方案
yum install -y epel-release
yum install -y nginx
systemctl start nginx
systemctl enable nginx
数据库服务部署
yum install -y mariadb-server mariadb
systemctl start mariadb
systemctl enable mariadb
mysql_secure_installation
PHP环境配置
yum install -y php php-mysql php-fpm
systemctl start php-fpm
systemctl enable php-fpm
安装常用扩展:
yum install -y php-gd php-json php-mbstring php-xml
防火墙配置
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
firewall-cmd --reload
虚拟主机配置
Apache示例配置
<VirtualHost *:80>
DocumentRoot /var/www/html
ServerName example.com
ErrorLog /var/log/httpd/example-error.log
CustomLog /var/log/httpd/example-access.log combined
</VirtualHost>
Nginx示例配置
server {
listen 80;
server_name example.com;
root /usr/share/nginx/html;
index index.php index.html;
location ~ .php$ {
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
}
}
环境验证测试
创建PHP测试文件:
echo "<?php phpinfo(); ?>" > /var/www/html/info.php
通过浏览器访问 http://服务器IP/info.php 验证PHP环境
安全加固建议
- 禁用不必要的PHP函数
- 配置数据库远程访问限制
- 定期更新系统安全补丁
- 启用SSL证书加密传输