在 CentOS 上部署 Apache2 项目,可以按照以下步骤进行:
1. 安装 Apache2
首先,确保你的 CentOS 系统是最新的,并且已经安装了必要的依赖包。
sudo yum update -y
sudo yum install -y httpd
2. 启动 Apache2 服务
安装完成后,启动 Apache2 服务并设置开机自启动。
sudo systemctl start httpd
sudo systemctl enable httpd
3. 配置防火墙
确保防火墙允许 HTTP 和 HTTPS 流量。
sudo firewall-cmd --permanent --zone=public --add-service=http
sudo firewall-cmd --permanent --zone=public --add-service=https
sudo firewall-cmd --reload
4. 配置 Apache2 虚拟主机
创建一个新的虚拟主机配置文件,或者编辑默认的虚拟主机配置文件。
sudo vi /etc/httpd/conf/httpd.conf
在文件中添加以下内容:
:80 >
ServerAdmin webmaster@yourdomain.com
DocumentRoot /var/www/html/yourproject
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
将 /var/www/html/yourproject 替换为你的项目目录。
5. 创建项目目录
创建项目目录并设置适当的权限。
sudo mkdir -p /var/www/html/yourproject
sudo chown -R apache:apache /var/www/html/yourproject
6. 部署项目文件
将你的项目文件复制到 /var/www/html/yourproject 目录下。
sudo cp -r /path/to/your/project/* /var/www/html/yourproject/
7. 重启 Apache2 服务
为了使配置生效,重启 Apache2 服务。
sudo systemctl restart httpd
8. 验证部署
打开浏览器,访问 http://yourdomain.com,你应该能够看到你的项目。
9. 配置 SSL(可选)
如果你需要使用 HTTPS,可以配置 SSL 证书。
安装 Certbot
sudo yum install -y certbot python2-certbot-apache
获取并安装 SSL 证书
sudo certbot --apache -d yourdomain.com
按照提示完成证书的安装和配置。
10. 配置自动续期
Certbot 会自动设置一个 cron 任务来续期证书。
sudo systemctl status certbot.timer
确保定时任务正在运行。
通过以上步骤,你应该能够在 CentOS 上成功部署 Apache2 项目。如果有任何问题,请检查日志文件以获取更多信息。
以上就是关于“如何在 CentOS 上部署 Apache2 项目”的相关介绍,筋斗云是国内较早的云主机应用的服务商,拥有10余年行业经验,提供丰富的云服务器、租用服务器等相关产品服务。云服务器资源弹性伸缩,主机vCPU、内存性能强悍、超高I/O速度、故障秒级恢复;电子化备案,提交快速,专业团队7×24小时服务支持!
简单好用、高性价比云服务器租用链接:https://www.jindouyun.cn/product/cvm