温馨提示×

如何在CentOS上部署Web服务器

小樊
40
2025-03-01 13:42:19
栏目: 云计算

在CentOS上部署Web服务器的步骤如下:

  1. 更新系统
sudo yum update -y
  1. 安装Apache
sudo yum install httpd -y
  1. 启动并启用Apache服务
sudo systemctl start httpd
sudo systemctl enable httpd
  1. 配置防火墙
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
  1. 安装PHP和MySQL(可选,如果需要运行动态网页):
sudo yum install php php-mysql -y
sudo yum install mariadb-server mariadb -y
  1. 启动并启用MySQL服务
sudo systemctl start mysqld
sudo systemctl enable mysqld
  1. 配置虚拟主机(可选,如果需要托管多个网站):
  • 创建虚拟主机配置文件,例如 /etc/httpd/conf.d/example.com.conf
sudo vi /etc/httpd/conf.d/example.com.conf
  • 添加以下内容:
VirtualHost *:80
ServerAdmin webmaster@example.com
DocumentRoot "/var/www/html/example.com"
ServerName example.com
ServerAlias www.example.com
ErrorLog "/var/log/httpd/example.com-error.log"
CustomLog "/var/log/httpd/example.com-access.log" combined
  • 创建网站目录并设置权限:
sudo mkdir -p /var/www/html/example.com
sudo chown -R apache:apache /var/www/html/example.com
sudo chmod -R 755 /var/www/html/example.com
  • 重启Apache服务以应用新的配置:
sudo systemctl restart httpd
  1. 测试Web服务器
  • 在浏览器中输入服务器的IP地址或域名,如果看到Apache的默认页面或您配置的虚拟主机页面,说明服务器已经成功搭建。

0