HoRain云--Linux搭建Apache服务器全攻略
2026/6/7 0:25:56 网站建设 项目流程

🎬 HoRain 云小助手:个人主页

⛺️生活的理想,就是为了理想的生活!


⛳️ 推荐

前些天发现了一个超棒的服务器购买网站,性价比超高,大内存超划算!忍不住分享一下给大家。点击跳转到网站。

目录

⛳️ 推荐

Linux上搭建Apache服务器:完整指南

一、环境准备

二、安装Apache(两种主流系统)

1. Ubuntu/Debian系统

2. CentOS/RHEL系统

三、验证安装

四、基本配置

1. 修改网站根目录(DocumentRoot)

2. 创建测试文件

3. 配置虚拟主机(多站点)

五、防火墙配置

Ubuntu/Debian(使用UFW)

CentOS/RHEL(使用firewalld)

六、常用命令总结

七、进阶配置

小贴士

常见问题


Linux上搭建Apache服务器:完整指南

你好!搭建Apache服务器是Web开发和运维的基础技能,今天我来给你一份清晰、实用的指南,让你轻松在Linux上部署Apache服务器。

一、环境准备

在开始前,请确认你的系统满足以下条件:

二、安装Apache(两种主流系统)

1. Ubuntu/Debian系统

# 更新软件包列表 sudo apt update # 安装Apache sudo apt install apache2 -y # 启动Apache服务 sudo systemctl start apache2 # 设置开机自启 sudo systemctl enable apache2

2. CentOS/RHEL系统

# 更新系统 sudo yum update # 安装Apache sudo yum install httpd -y # 启动Apache服务 sudo systemctl start httpd # 设置开机自启 sudo systemctl enable httpd

三、验证安装

# 检查服务状态 sudo systemctl status apache2 # Ubuntu/Debian sudo systemctl status httpd # CentOS/RHEL # 在浏览器中访问:http://localhost 或 http://服务器IP

如果看到Apache的默认欢迎页面,恭喜你,安装成功了!

四、基本配置

1. 修改网站根目录(DocumentRoot)

Apache的默认网站根目录是/var/www/html,如果你想更改:

# 创建新目录 sudo mkdir -p /var/www/mywebsite # 修改配置文件 sudo nano /etc/apache2/apache2.conf # Ubuntu/Debian # 或 sudo nano /etc/httpd/conf/httpd.conf # CentOS/RHEL # 找到DocumentRoot行,修改为: DocumentRoot "/var/www/mywebsite" # 保存并退出,然后重启Apache sudo systemctl restart apache2 # Ubuntu/Debian sudo systemctl restart httpd # CentOS/RHEL

2. 创建测试文件

# 创建index.html文件 echo "<h1>我的Apache服务器已成功运行!</h1>" | sudo tee /var/www/mywebsite/index.html

3. 配置虚拟主机(多站点)

# 创建配置文件(Ubuntu/Debian) sudo nano /etc/apache2/sites-available/mydomain.conf # 添加以下内容: <VirtualHost *:80> ServerAdmin webmaster@mydomain.com ServerName mydomain.com ServerAlias www.mydomain.com DocumentRoot /var/www/mydomain ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost> # 启用虚拟主机 sudo a2ensite mydomain.conf # 重启Apache sudo systemctl restart apache2

五、防火墙配置

Ubuntu/Debian(使用UFW)

sudo ufw allow 'Apache Full' sudo ufw reload

CentOS/RHEL(使用firewalld)

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

六、常用命令总结

操作Ubuntu/DebianCentOS/RHEL
启动Apachesudo systemctl start apache2sudo systemctl start httpd
停止Apachesudo systemctl stop apache2sudo systemctl stop httpd
重启Apachesudo systemctl restart apache2sudo systemctl restart httpd
查看状态sudo systemctl status apache2sudo systemctl status httpd
开机自启sudo systemctl enable apache2sudo systemctl enable httpd

七、进阶配置

  1. SSL配置(使用Let's Encrypt):

    # Ubuntu/Debian sudo apt install certbot python3-certbot-apache sudo certbot --apache -d yourdomain.com # CentOS/RHEL sudo yum install certbot python3-certbot-apache sudo certbot --apache -d yourdomain.com
  2. URL重写(.htaccess): 在网站根目录创建.htaccess文件,添加重写规则。

小贴士

常见问题

Q:为什么访问不了Apache页面?A:检查防火墙设置,确认80端口已开放;检查服务是否启动。

Q:如何修改Apache的默认端口?A:编辑配置文件,找到Listen 80,修改为其他端口,如Listen 8080,然后重启Apache。

Q:如何查看Apache版本?A:apache2 -v(Ubuntu)或httpd -v(CentOS)


现在,你已经掌握了在Linux上搭建Apache服务器的基本技能!如果你是刚开始接触Web服务器,不妨尝试部署一个简单的静态网站,然后慢慢学习更高级的配置。

需要我详细解释某个配置步骤,或者有其他关于Apache的问题吗?😊

❤️❤️❤️本人水平有限,如有纰漏,欢迎各位大佬评论批评指正!😄😄😄

💘💘💘如果觉得这篇文对你有帮助的话,也请给个点赞、收藏下吧,非常感谢!👍 👍 👍

🔥🔥🔥Stay Hungry Stay Foolish 道阻且长,行则将至,让我们一起加油吧!🌙🌙🌙

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询