Re: Apache shows PHP code instead of executing it

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



> When I invoke it from the browser Apache shows PHP code instead of
> executing it. Please let me know if you need any additional information.
> Thanks in Advance


This is because you did not tell apache to forward incoming HTTP requests to the PHP Engine.
You can integrate Apache httpd with PHP in different ways, generally they are PHP-FPM (through fastcgi), mod_php (apache SAPI module) and CGI (discouraged because is very slow and does not scale well).

Since you've installed the package php73-fpm-httpd-7.3.25-1.el7.ius.noarch I think you would like to use PHP-FPM.
Now, there are also different ways to integrate Apache httpd with PHP-FPM. Personally I like to use mod_proxy_fcgi.so or mod_fastcgi.so.

BTW, I'll try to point you to the right direction with mod_proxy_fcgi:

1: you need to configure PHP-FPM to listen to a local unix socket (eg: /run/php/php7.3-fpm.sock), also configure the other PHP-FPM and php.ini parameters and permissions and start your PHP-FPM process

2: tell Apache httpd to send right incoming requests (those ending with .php for example) to PHP-FPM. One way is something like this:


# ...
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so

Listen *:80
<VirtualHost *:80>
ServerName yourservername
ErrorLog logs/your-error_log
CustomLog logs/your-access_log common
LogLevel warn

# Use local php-fpm process
<IfModule proxy_fcgi_module>
# Enable http authorization headers
<IfModule setenvif_module>
SetEnvIfNoCase ^Authorization$ "(.+)" HTTP_AUTHORIZATION=$1
</IfModule>

<FilesMatch ".+\.ph(p[3457]?|t|tml)$">
SetHandler "proxy:unix:/run/php/php7.3-fpm.sock|fcgi://localhost"
</FilesMatch>
<FilesMatch ".+\.phps$">
# Deny access to raw php sources by default
# To re-enable it's recommended to enable access to the files
# only in specific virtual host or directory
Require all denied
</FilesMatch>
# Deny access to files without filename (e.g. '.php')
<FilesMatch "^\.ph(p[3457]?|t|tml|ps)$">
Require all denied
</FilesMatch>
</IfModule>

DocumentRoot /your/docroot
<Directory /your/docroot>
Options +FollowSymlinks -Includes
DirectoryIndex index.php
AllowOverride All
Require all granted
</Directory>

</VirtualHost>
# ...


3: check all your paths and permissions

4: if you would like to send any incoming request to PHP-FPM (for ex. in case your application make use of a controller), you would add a .htaccess file that contains something like this into your application docroot (and enable mod_rewrite in httpd.conf). This way, any incoming request that is not a valid resolvable file or directory will be forwarded to index.php (that resolve to PHP-FPM due to the config you set at item 2):

#-------------------------------------------------------------------------------------------------
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]
# Let PHP having access to Authorization: Bearer header
RewriteRule .* - [env=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
#-------------------------------------------------------------------------------------------------


HTH.
Ciao, Dino.

[Index of Archives]     [Open SSH Users]     [Linux ACPI]     [Linux Kernel]     [Linux Laptop]     [Kernel Newbies]     [Security]     [Netfilter]     [Bugtraq]     [Squid]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Samba]     [Video 4 Linux]     [Device Mapper]

  Powered by Linux