在Apache2中配置静态和动态内容主要涉及设置虚拟主机、配置文件类型处理以及调整相关参数。以下是详细的步骤:
创建或编辑虚拟主机配置文件
/etc/apache2/sites-available/目录下创建一个新的配置文件,例如example.com.conf。sudo nano /etc/apache2/sites-available/example.com.conf
配置虚拟主机
ServerName和ServerAlias。<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/html
<Directory /var/www/html>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/example.com_error.log
CustomLog ${APACHE_LOG_DIR}/example.com_access.log combined
</VirtualHost>
启用虚拟主机
a2ensite命令启用配置文件。sudo a2ensite example.com.conf
重启Apache2服务
sudo systemctl restart apache2
mod_php、mod_cgi、mod_fastcgi等。sudo a2enmod php7.4 # 根据你的PHP版本调整
sudo systemctl restart apache2
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/html
<Directory /var/www/html>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
# CGI配置
ScriptAlias /cgi-bin/ /var/www/html/cgi-bin/
<Directory "/var/www/html/cgi-bin">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/example.com_error.log
CustomLog ${APACHE_LOG_DIR}/example.com_access.log combined
</VirtualHost>
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/html
<Directory /var/www/html>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
# PHP配置
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
ErrorLog ${APACHE_LOG_DIR}/example.com_error.log
CustomLog ${APACHE_LOG_DIR}/example.com_access.log combined
</VirtualHost>
sudo apt-get install php-fpm
编辑PHP-FPM配置文件(通常在/etc/php/7.4/fpm/pool.d/www.conf),设置监听地址。
listen = /run/php/php7.4-fpm.sock
在Apache配置文件中添加FastCGI处理规则。
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/html
<Directory /var/www/html>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
# FastCGI配置
<FilesMatch \.php$>
SetHandler "proxy:fcgi://unix:/run/php/php7.4-fpm.sock|fcgi://localhost"
</FilesMatch>
ErrorLog ${APACHE_LOG_DIR}/example.com_error.log
CustomLog ${APACHE_LOG_DIR}/example.com_access.log combined
</VirtualHost>
通过以上步骤,你可以成功配置Apache2来处理静态和动态内容。确保根据你的实际需求调整配置文件中的路径和参数。