使用 Debian / Apache2 在本地查看 https 站点

使用 Debian / Apache2 在本地查看 https 站点

我正在做 Debian 测试。我在 localhost/~myhome/ 看到本地 http 站点... 为此,我对 /etc/apache2/mods-available/php8.2.conf 进行了经典更改

To re-enable php in user directories comment the following lines
...

但是我有一个 https 网站,其中我添加了 .htaccess

RewriteEngine on
RewriteCond %{HTTP_HOST} ^sito.net$ [OR]
RewriteCond %{HTTP_HOST} ^sito.it$ [OR]
RewriteCond %{HTTP_HOST} ^www.sito.it$
RewriteRule ^/?$ "http\:\/\/www\.site\.net" [R=301,L]`

并且从本地我仍然看不到它,接收:

Internal Server Error
The server encountered an internal error or misconfiguration
and was unable to complete your request.`
Please contact the server administrator at webmaster@localhost
to inform them of the time this error occurred, and the actions
you performed just before this error.
More information about this error may be available
in the server error log.
Apache/2.4.58 (Debian) Server at localhost Port 80`

我遵循了此线程中的一些(旧)说明:如何在本地主机上允许 Apache 使用 HTTPS?

我执行了以下步骤:

# openssl req -x509 -newkey rsa:2048 -keyout mykey.key -out mycert.pem -days 365 -nodes
#cp mycert.pem mykey.key /etc/ssl/private/
#a2enmod ssl
#a2ensite default-ssl.conf
#systemctl restart apache2

但收到:

root@debian:~# systemctl restart apache2
Job for apache2.service failed because the control process exited with error code.
See "systemctl status apache2.service" and "journalctl -xeu      apache2.service" for details.

之后:

root@debian:~# systemctl status apache2.service
× apache2.service - The Apache HTTP Server
Loaded: loaded (/lib/systemd/system/apache2.service; enabled; preset: enabled)
Active: failed (Result: exit-code) since Mon 2023-12-04 21:35:15 CET; 8s ago
Docs: https://httpd.apache.org/docs/2.4/
Process: 4216 ExecStart=/usr/sbin/apachectl start (code=exited, status=1/FAILURE)
    CPU: 93ms

dic 04 21:35:15 debian systemd[1]: Starting apache2.service - The Apache HTTP Server...
dic 04 21:35:15 debian apachectl[4218]: AH00526: Syntax error on line 227 of /etc/apache2/apache2.conf:
dic 04 21:35:15 debian apachectl[4218]: Cannot define multiple Listeners on the same IP:port
dic 04 21:35:15 debian systemd[1]: apache2.service: Control process exited, code=exited, status=1/FAILURE
dic 04 21:35:15 debian systemd[1]: apache2.service: Failed with result 'exit-code'.
dic 04 21:35:15 debian systemd[1]: Failed to start apache2.service - The Apache HTTP Server.

引用该错误的行:

# Include the virtual host configurations:
IncludeOptional sites-enabled/*.conf
Listen 80
NameVirtualHost *:80
<VirtualHost *:443>
DocumentRoot "/home/samiel/public_html"  #your wamp www root dir
ServerName debian
SSLEngine on
SSLCertificateFile "/etc/ssl/certs/mycert.pem"
SSLCertificateKeyFile "/etc/ssl/certs/mykey.key"

我无法再进行任何尝试...

答案1

首次安装 apt install libapache2-mod-php8.2

使用以下命令转储整个运行配置apachectl -S

当你想让应用程序由 apache 提供服务时,最佳做法是在 /etc/apache2/sites-available 中创建一个虚拟主机

虚拟主机的示例如下

    <VirtualHost *:80>
    ServerName yourdomain.com
    ServerAlias www.yourdomain.com

    DocumentRoot /var/www/html

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    RewriteEngine on
    RewriteCond %{SERVER_NAME} =yourdomain.com [OR]
    RewriteCond %{SERVER_NAME} =www.yourdomain.com
    RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

<VirtualHost *:443>
    ServerName yourdomain.com
    ServerAlias www.yourdomain.com

    DocumentRoot /var/www/html

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    SSLEngine on
    SSLCertificateFile /etc/ssl/certs/yourdomain.crt
    SSLCertificateKeyFile /etc/ssl/private/yourdomain.key
    SSLCertificateChainFile /etc/ssl/certs/yourdomain.ca-bundle

    <Directory /var/www/html>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>

    <FilesMatch \.php$>
        SetHandler "proxy:unix:/var/run/php/php8.2-fpm.sock|fcgi://localhost/"
    </FilesMatch>
</VirtualHost>

然后将其保存/etc/apache2/sites-available/为 yoursite.conf

  • 然后启用vhost sudo a2ensite yourdomain.conf
  • 测试配置 sudo apachectl configtest
  • 如果一切正常,重新启动 apache sudo systemctl restart apache2

相关内容