1. 使用以下命令安装最新 Apache2服务器
sudo apt update && sudo apt install apache2
2. 修改配置文件
位置:/etc/apache2/apache2.conf
增加服务器目录
<Directory /新增目录位置>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
保存文件
3.修改端口文件
位置:/etc/apache2/ports.conf
# 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
Listen 自定义端口
<IfModule ssl_module>
Listen 443
</IfModule>
<IfModule mod_gnutls.c>
Listen 443
</IfModule>
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
保存文件
4.绑定文件夹位置对应端口
位置:/etc/apache2/sites-available/000-default.conf
<VirtualHost *:自定义端口>
ServerAdmin webmaster@localhost
DocumentRoot /新增目录位置 #新增目录位置
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
保存文件
5.由于Apache2使用www-data:www-data进行访问,因此为了正常访问要赋予读写权限
在终端输入
sudo chmod 775 -R /新增目录位置
或者
sudo chmod 755 -R /新增目录位置
修改完成
6.重启Apache2服务器
sudo systemctl restart apache2.service
7.检查服务器状态
systemctl status apache2.service
8.假如服务器文件夹新建在home目录或者其他根目录下
需要检查该文件夹上层根目录有没有足够权限,根目录权限不足依然会出现403错误
sudo chmod 775 -R /根目录位置
或者
sudo chmod 755 -R /根目录位置