本教程适用于termux和linux发行版本,请根据代码注释选择合适的代码

了解 Apache

Apache,像羽毛一样的图标,全球服务器使用量第一的服务器软件,扩展高,安全高都是对Apache最好的标志。
在termux proot容器中,直接安装好编译的nginx是无法登陆的,所以apache便成了替代品。
apache的1.3版本早已占领上世纪90年代的服务器,所以现在apache便成了第一
2.x更是比1.3强劲更多
apache还是debian系发行版的最好搭配
好了不说了,直接开始安装好吧

开始安装

考虑到源码安装方式在termux、proot环境和旧服务器中编译缓慢,这里优先考虑deb安装

1
2
3
4
# 在termux上
pkg install apache2
# 在debian系发行版和termux proot上(proot仅debian系发行版)
apt install apache2

安装好上述功能后就要开始配置了

配置

在 termux 上

在termux上,你无需做任何配置。因为你只知道一个文件夹

1
/data/data/com.termux/files/usr/share/apache2/default-site/htdocs/

这个文件夹是网站文件的根目录,你只需要获得访问权限并使用cp命令将你在的文件复制到这里即可
如果你仍然要在手机存储的某个目录作为apache的文件夹的话,那么请等一等,因为明天是周一我要回学校,我得早睡,写完这个文章后便没时间更正了,不然明天在学校睡觉直接被真实。

在 debian 系发行版和termux proot上

apache 配置文件夹

使用

1
cd /etc/apache2

移步到该文件夹,这里便是apache的配置文件。

关于默认配置文件的描述

  1. /etc/apache2/apache2.conf
  • 主要配置文件,在该文件末尾include了其他的配置文件
  1. /etc/apache2/ports.conf
  • 与主配置一个文件夹,用于配置端口,默认都需要重新配置,termux proot下必须重新配置
  1. /etc/apache2/envvars
  • 定义apaxhe用户的配置文件,默认为www-data
  1. /etc/apache2/mods-enabled/dir.conf
  • 默认主页的配置文件
  1. /etc/apache2/site-enabled/000-default.conf
  • 默认的主机配置文件

主要配置

首先重新配置配置端口
Vim,启动

1
vim /etc/apache2/ports.conf 

先在ports.conf修改监听端口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# If you just change the port or add more ports here, you will likely also
# have to change the VirtualHost statement in
# /etc/apache2/sites-enabled/000-default.conf

Listen 80 # 在这里修改监听端口号,这里记住了后面要考

<IfModule ssl_module>
Listen 443
</IfModule>

<IfModule mod_gnutls.c>
Listen 443
</IfModule>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

然后修改主机端口

1
vim /etc/apache2/site-available/000-default.conf
1
2
3
4
5
6
7
8
9
10
11
<VirtualHost *:80> # 把这里面的80换成刚才在 ports.conf 里输入的端口号
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
#ServerName www.example.com

ServerAdmin xiaolan2332021@163.com

测试

termux

1
2
3
4
# 启动
apachectl
# 关闭
pkill -9 apache2

debian系发行版和proot

1
2
3
4
5
6
# 启动
/etc/init.d/apache2 start
# 查看状态
/etc/init.d/apache2 status
# 关闭
/etc/init.d/apache2 stop

查看状态时,如果提前运行了启动指令,则显示

1
* apache2 is running

查看运行结果

打开任意浏览器,然后输入 localhost: 加在ports.conf里输入的端口号 (localhost后面的冒号要带上),无任何问题时应显示:
运行结果

总结

nginx的替代品
如果deb包安装不行的话就去网上找源码安装罢
还要想要试试nginx的可以去看这篇文章
不想折腾就备个ksweb罢
到时候给大家整个php安装并在apache上运行