Loading... <div class="tip inlineBlock warning simple"> 🤖 本博客内容使用 GPT-4 技术进行润色 </div> 当今互联网时代,构建个人网站或搭建自己的服务器已经成为越来越多人的需求和追求。作为一款迷你型的单板计算机,树莓派(Raspberry Pi)可以作为个人服务器的理想选择,而 Apache2 则是一种常用的 Web 服务器软件,广泛应用于搭建和托管网站。本文将带您一步步了解如何在树莓派上安装并设置 Apache2,为您提供一个简单而强大的个人网站搭建方案。无论您是初学者还是有一定经验的开发者,相信本文都能帮助您轻松入门,并在树莓派上搭建起自己的 Web 服务器。 ## 1、安装 Apache2 首先,我们需要安装 Apache2,您可以通过以下命令在树莓派上进行安装: ```bash sudo apt-get install apache2 -y ``` 默认的网页路径在 `/var/www/html/index.html`,您可以编辑 `index.html` 内容来实现个人网页的定制。然后,通过访问树莓派的 IP 地址,您就可以在浏览器中看到相应的界面了。 ## 2、安装 PHP 接下来,我们还可以安装 PHP,以进一步扩展树莓派的功能。您可以使用以下命令安装 PHP 及其相关模块: ```bash sudo apt-get install php libapache2-mod-php -y ``` 安装完成后,可以进入目录 `/var/www/html` 中,删除其中的 `index.html`,然后新建一个 `index.php` 的文件,随便写一些内容,例如:`<?php echo "hello world";?>` 或者 `<?php phpinfo();?>`。最后,访问树莓派的 IP 地址就可以在浏览器中看到相应的界面了。 如果您在安装程序过程中需要使用特定的库,请按照以下步骤进行安装: <div class="panel panel-default collapse-panel box-shadow-wrap-lg"><div class="panel-heading panel-collapse" data-toggle="collapse" data-target="#collapse-50f9c916d75023bd11d97090b978871090" aria-expanded="true"><div class="accordion-toggle"><span style="">安装 CURL 库</span> <i class="pull-right fontello icon-fw fontello-angle-right"></i> </div> </div> <div class="panel-body collapse-panel-body"> <div id="collapse-50f9c916d75023bd11d97090b978871090" class="collapse collapse-content"><p></p> ```bash # 查询合适的库和版本 sudo apt-cache search curl | grep php # 注意安装对应PHP版本的库 sudo apt-get install php7.3-curl ``` <p></p></div></div></div> </br> <div class="panel panel-default collapse-panel box-shadow-wrap-lg"><div class="panel-heading panel-collapse" data-toggle="collapse" data-target="#collapse-cb8f09f1e650f178a82d23b0bd7e7d5d92" aria-expanded="true"><div class="accordion-toggle"><span style="">安装 MB_STRING 库</span> <i class="pull-right fontello icon-fw fontello-angle-right"></i> </div> </div> <div class="panel-body collapse-panel-body"> <div id="collapse-cb8f09f1e650f178a82d23b0bd7e7d5d92" class="collapse collapse-content"><p></p> ```bash # 查询合适的库和版本 sudo apt-cache search php-mbstring | grep php # 注意安装对应PHP版本的库 sudo apt-get install php7.3-mbstring ``` <p></p></div></div></div> </br> <div class="panel panel-default collapse-panel box-shadow-wrap-lg"><div class="panel-heading panel-collapse" data-toggle="collapse" data-target="#collapse-2f249b6c49aedf9ea9350ce95021e34c47" aria-expanded="true"><div class="accordion-toggle"><span style="">安装 GD 库</span> <i class="pull-right fontello icon-fw fontello-angle-right"></i> </div> </div> <div class="panel-body collapse-panel-body"> <div id="collapse-2f249b6c49aedf9ea9350ce95021e34c47" class="collapse collapse-content"><p></p> ```bash # 查询合适的库和版本 sudo apt-cache search php-gd | grep php # 注意安装对应PHP版本的库 sudo apt-get install php7.3-gd ``` <p></p></div></div></div> 在安装完所需的库之后,为了确保这些更改能够生效,我们可能需要重启 Apache2 服务。下面是三种不同的方法来重启 Apache2 服务,您可以选择其中任意一种执行: <div class="panel panel-default collapse-panel box-shadow-wrap-lg"><div class="panel-heading panel-collapse" data-toggle="collapse" data-target="#collapse-61154a06df079ce8d732c27defd95e5651" aria-expanded="true"><div class="accordion-toggle"><span style="">使用 `/etc/init.d` 脚本来重启服务</span> <i class="pull-right fontello icon-fw fontello-angle-right"></i> </div> </div> <div class="panel-body collapse-panel-body"> <div id="collapse-61154a06df079ce8d732c27defd95e5651" class="collapse collapse-content"><p></p> ```bash sudo /etc/init.d/apache2 restart ``` <p></p></div></div></div> </br> <div class="panel panel-default collapse-panel box-shadow-wrap-lg"><div class="panel-heading panel-collapse" data-toggle="collapse" data-target="#collapse-8e3c4feddfdc0bf92003e4d2d9864c2295" aria-expanded="true"><div class="accordion-toggle"><span style="">使用 `service` 命令来重启服务</span> <i class="pull-right fontello icon-fw fontello-angle-right"></i> </div> </div> <div class="panel-body collapse-panel-body"> <div id="collapse-8e3c4feddfdc0bf92003e4d2d9864c2295" class="collapse collapse-content"><p></p> ```bash sudo service apache2 restart ``` <p></p></div></div></div> </br> <div class="panel panel-default collapse-panel box-shadow-wrap-lg"><div class="panel-heading panel-collapse" data-toggle="collapse" data-target="#collapse-f5ea20b43f6201121b8958a4795b021876" aria-expanded="true"><div class="accordion-toggle"><span style="">使用 `systemctl` 命令来重启服务</span> <i class="pull-right fontello icon-fw fontello-angle-right"></i> </div> </div> <div class="panel-body collapse-panel-body"> <div id="collapse-f5ea20b43f6201121b8958a4795b021876" class="collapse collapse-content"><p></p> ```bash sudo systemctl restart apache2.service ``` <p></p></div></div></div> 请注意,您可能需要根据您的操作系统和 Apache2 的安装方式选择适当的方法来重启服务。在某些系统上,`systemctl` 方法可能是首选,因为它提供了更多的功能和灵活性。无论您选择哪种方法,确保以管理员权限(例如使用 `sudo`)执行这些命令,以便具有足够的权限来重启 Apache2 服务。 ## 3、端口映射到对应目录 在 Apache 安装完成后,默认情况下,它会将 `/var/www/html/` 路径下的 `index.php` 或 `index.html` 文件(若不存在则显示目录列表)映射到`80`端口。但如果您希望将其他路径下的项目映射到不同的端口,比如将 `/srv/www/blog` 映射到 `8180` 端口,则需要进行以下步骤。 ### ① 设置目录访问 打开 Apache 的主配置文件: ```bash sudo vim /etc/apache2/apache2.conf ``` 在适当的位置添加以下内容,以允许对 `/srv/www/` 目录的访问: ``` <Directory /srv/www/> Options Indexes FollowSymLinks AllowOverride None Require all granted </Directory> ``` #### ② 添加监听端口 编辑 Apache 的端口配置文件: ```bash sudo vim /etc/apache2/ports.conf ``` 此处原来只有 `80` 端口,我们需要在文件中添加希望监听的端口,开放 `8180` 端口可以按照下面方式添加: ``` Listen 80 Listen 8180 ``` #### ③ 配置虚拟主机 编辑虚拟主机配置文件: ``` sudo vim /etc/apache2/sites-available/000-default.conf ``` 在配置文件中,我们就会发现 `80` 监听端口对应着目录 `/var/www/html`,我们可以修改该设置或者在此文件中添加一个新的虚拟主机配置,用于映射 `8180` 端口到 `/srv/www/blog` 目录。例如: ``` <VirtualHost *:8180> ServerAdmin webmaster@localhost DocumentRoot /srv/www/blog ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost> ``` <div class="tip inlineBlock warning simple"> **完成上述步骤后,请确保重启 Apache 服务,以使更改生效。** </div> ## Apache禁止访问目录或文件 Apache服务器默认配置可能会暴露整个文件目录结构,允许用户查看敏感的配置信息或文件内容,这对于部署开源项目是尤其危险的。为了确保服务器安全,我们可以通过命令 `sudo vim /etc/apache2/apache2.conf` 编辑 Apache 的主配置文件,并参考以下配置示例来禁止对特定目录或文件的访问。 <div class="panel panel-default collapse-panel box-shadow-wrap-lg"><div class="panel-heading panel-collapse" data-toggle="collapse" data-target="#collapse-d0c00b7ab2bb63820859573bb569682795" aria-expanded="true"><div class="accordion-toggle"><span style="">禁止访问目录</span> <i class="pull-right fontello icon-fw fontello-angle-right"></i> </div> </div> <div class="panel-body collapse-panel-body"> <div id="collapse-d0c00b7ab2bb63820859573bb569682795" class="collapse collapse-content"><p></p> ``` <Directory /var/www/html/> Options -Indexes +FollowSymLinks AllowOverride None Order allow,deny allow from all </Directory> ``` <p></p></div></div></div> </br> <div class="panel panel-default collapse-panel box-shadow-wrap-lg"><div class="panel-heading panel-collapse" data-toggle="collapse" data-target="#collapse-a4283b81bd0adaafa530b8cfd27ea0c317" aria-expanded="true"><div class="accordion-toggle"><span style="">通过正则禁止访问某些目录</span> <i class="pull-right fontello icon-fw fontello-angle-right"></i> </div> </div> <div class="panel-body collapse-panel-body"> <div id="collapse-a4283b81bd0adaafa530b8cfd27ea0c317" class="collapse collapse-content"><p></p> ``` <Directory ~ "^/var/www/(.+/)*[0-9]{3}"> Order allow,deny Deny from all </Directory> ``` <p></p></div></div></div> </br> <div class="panel panel-default collapse-panel box-shadow-wrap-lg"><div class="panel-heading panel-collapse" data-toggle="collapse" data-target="#collapse-7a600501e476fa4596c34e354c5af1e681" aria-expanded="true"><div class="accordion-toggle"><span style="">禁止访问某类文件</span> <i class="pull-right fontello icon-fw fontello-angle-right"></i> </div> </div> <div class="panel-body collapse-panel-body"> <div id="collapse-7a600501e476fa4596c34e354c5af1e681" class="collapse collapse-content"><p></p> ``` <Files ~ "\.txt$"> Order allow,deny Deny from all </Files> ``` <p></p></div></div></div> </br> <div class="panel panel-default collapse-panel box-shadow-wrap-lg"><div class="panel-heading panel-collapse" data-toggle="collapse" data-target="#collapse-4cfa414cff0015f51e5a52d098f0f1d877" aria-expanded="true"><div class="accordion-toggle"><span style="">通过正则禁止访问某些文件</span> <i class="pull-right fontello icon-fw fontello-angle-right"></i> </div> </div> <div class="panel-body collapse-panel-body"> <div id="collapse-4cfa414cff0015f51e5a52d098f0f1d877" class="collapse collapse-content"><p></p> ``` <FilesMatch \.(?i:gif|jpe?g|png)$> Order allow,deny Deny from all </FilesMatch> ``` <p></p></div></div></div> </br> <div class="panel panel-default collapse-panel box-shadow-wrap-lg"><div class="panel-heading panel-collapse" data-toggle="collapse" data-target="#collapse-5572ade366ba867c3c66f24f46a0799f82" aria-expanded="true"><div class="accordion-toggle"><span style="">禁止通过代理访问</span> <i class="pull-right fontello icon-fw fontello-angle-right"></i> </div> </div> <div class="panel-body collapse-panel-body"> <div id="collapse-5572ade366ba867c3c66f24f46a0799f82" class="collapse collapse-content"><p></p> ``` <Proxy https://xxxxx.com/*> Order allow,deny Deny from all </Proxy> ``` <p></p></div></div></div> </br> <div class="panel panel-default collapse-panel box-shadow-wrap-lg"><div class="panel-heading panel-collapse" data-toggle="collapse" data-target="#collapse-f4ce532572e3cb31ec8112225b2c460b85" aria-expanded="true"><div class="accordion-toggle"><span style="">禁止某些IP访问</span> <i class="pull-right fontello icon-fw fontello-angle-right"></i> </div> </div> <div class="panel-body collapse-panel-body"> <div id="collapse-f4ce532572e3cb31ec8112225b2c460b85" class="collapse collapse-content"><p></p> ``` <Directory "/var/www/web/"> Order allow,deny Allow from all Deny from 10.0.0.1 Deny from 192.168.0.0/24 </Directory> ``` <p></p></div></div></div> </br> <div class="panel panel-default collapse-panel box-shadow-wrap-lg"><div class="panel-heading panel-collapse" data-toggle="collapse" data-target="#collapse-7bad76cdb07f44041dc157e3e451cdbf67" aria-expanded="true"><div class="accordion-toggle"><span style="">只允许某些IP访问</span> <i class="pull-right fontello icon-fw fontello-angle-right"></i> </div> </div> <div class="panel-body collapse-panel-body"> <div id="collapse-7bad76cdb07f44041dc157e3e451cdbf67" class="collapse collapse-content"><p></p> ``` <Directory "/var/www/web/"> Order deny,allow Deny from all All from xxxxx.com All from 10.0.0.1 All from 192.168.0.0/24 All from 10.0.0.1 10.0.0.2 </Directory> ``` <p></p></div></div></div> Last modification:February 29, 2024 © Allow specification reprint Support Appreciate the author AliPayWeChat Like 如果觉得我的文章对你有用,请随意赞赏