New Apache2 and Virtual Host install showing plain text for cgi directory

50 Views Asked by At

I have a new vps which has Ubuntu 22.04 installed and Apache/2.4.52 (Ubuntu). I have SSL installed on this machine, and created a Virtual Host which looks like this:

<VirtualHost *:443>
        ServerName dev.jts.co.uk

        ServerAdmin [email protected]
        DocumentRoot /var/www/

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

<Directory "/var/www">
    Options +ExecCGI +Indexes
    AllowOverride All
    Require all granted
</Directory>


<Directory "/var/www/pt/pt_v1">
    # Disable PHP 7.4 engine
    php_value engine off

    # Enable PHP 5.6 via CGI
    AddHandler application/x-httpd-php5 .php .htm .html .cgi
    Action application/x-httpd-php5 /cgi-bin/php-cgi5.6
    Options +ExecCGI
    AllowOverride None
</Directory>

ScriptAlias /cgi-bin/ /var/www/cgi-bin

SSLCertificateFile /etc/letsencrypt/live/dev.jts.co.uk/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/dev.jts.co.uk/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf

</VirtualHost>

This is one of many veriations of the .conf file, I have tried many more, including .htaccess directives, all have made the phpinfo.php show in plain text, or download.

The default php is php 7.4, which is used in the root folder, but the /pt/.. folders will need to be run via cgi. starting with php-cgi5.6

I am trying to run dev.jts.co.uk/jts/jts_v1/phpinfo.php which consists of this code:

<?php phpinfo();

But it only gives me this in plain text.

I can run it from the commandline using /var/www/cgi-bin$ ./php-cgi5.6 < /var/www/pt/pt_v1/phpinfo.php and this seems to working and display. So i am confident the php-cgi is ok.

The module is enabled sudo a2enmod cgi.

The issue seems to be how Apache is interpreting it, and not executing it. I have check permissions for folder, and files which are correct. I have exhausted all google search which focus on httpd.conf rather than the latest apache2 and Virtual Hosts. I have asked OpenAI ChatGPT to look at it, who has exhausted all ideas. None seem to work.

Due the nature of the issue it must be something simple i am missing. Can anyone help me please?

Thank you Lazy

1

There are 1 best solutions below

1
Алексей Никитченко On

Did you try

<Directory "/var/www/pt/pt_v1">
    # Disable PHP 7.4 engine
    php_value engine off

    # Enable PHP 5.6 via CGI
    AddType application/x-httpd-php5 .php .htm .html .cgi #replaced Addhandler with addtype
    Action application/x-httpd-php5 /cgi-bin/php-cgi5.6
    Options +ExecCGI
    AllowOverride None
</Directory>

actually, panels like virtualmin do it in this way. Hope /cgi-bin/php-cgi5.6 is the actual cgi wrapper.

EDIT: i make vps with ubuntu 20.04, apache2 and php5.6-cgi and 8.1-cgi installed. this is my virtual host. i used a2disconf php5.6-cgi and a2disconf php8.1-cgi, a2enmod cgid

and this is config on clen system:

<VirtualHost *:80>
    # 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 webmaster@localhost
    DocumentRoot /var/www/html

    # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
    # error, crit, alert, emerg.
    # It is also possible to configure the loglevel for particular
    # modules, e.g.
    #LogLevel info ssl:warn

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
ScriptAlias /cgi/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
    AllowOverride None
    Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
    Order allow,deny
    Allow from all
</Directory>
Action application/x-httpd-php5 /cgi-bin/php5.6
Action application/x-httpd-php8 /cgi-bin/php8.1
    <Directory "/var/www/html">
   
     Options +Indexes +FollowSymLinks +Includes 
                AllowOverride All
                Order allow,deny
                Allow from all
SetHandler application/x-httpd-php8 #HERE if i set ...-php5 i use 5.6, if ...-php8 8.1 is used.

    </Directory>
   
    # For most configuration files from conf-available/, which are
    # enabled or disabled at a global level, it is possible to
    # include a line for only one particular virtual host. For example the
    # following line enables the CGI configuration for this host only
    # after it has been globally disabled with "a2disconf".
    #Include conf-available/serve-cgi-bin.conf
</VirtualHost>

all works fine. i think you can add other php version as php-fpm and switch where you need.