How install and configure manually php and apache? - php

Installed PHP like this:
wget http://in1.php.net/distributions/php-5.3.28.tar.bz2
tar -xvf php-5.3.28.tar.bz2
cd php-5.3.28
./configure
make
make install
Installed Apache2 like this:
sudo apt-get install apache2
So, how do I now link Apache to PHP?
PS - I know I can install PHP as a module which will be 100x easier, but I want to know how to link exactly these two in this way.
Thanks.

Edit your httpd.conf to load the PHP module. The path on the right hand side of the LoadModule statement must point to the path of the PHP module on your system. The make install from above may have already added this for you, but be sure to check.
LoadModule php5_module modules/libphp5.so
Tell Apache to parse certain extensions as PHP. For example, let's have Apache parse .php files as PHP. Instead of only using the Apache AddType directive, we want to avoid potentially dangerous uploads and created files such as exploit.php.jpg from being executed as PHP. Using this example, you could have any extension(s) parse as PHP by simply adding them. We'll add .php to demonstrate.
<FilesMatch \.php$>
SetHandler application/x-httpd-php
</FilesMatch>
Or, if we wanted to allow .php, .php2, .php3, .php4, .php5, .php6, and .phtml files to be executed as PHP, but nothing else, we'd use this:
<FilesMatch "\.ph(p[2-6]?|tml)$">
SetHandler application/x-httpd-php
</FilesMatch>
And to allow .phps files to be handled by the php source filter, and displayed as syntax-highlighted source code, use this:
<FilesMatch "\.phps$">
SetHandler application/x-httpd-php-source
</FilesMatch>
And restart Apache
service httpd restart
You can read how do that in the php documentation
http://php.net/manual/en/install.unix.apache2.php
http://php.net/manual/en/install.unix.apache2.php#92797

Related

Cant find AddHandler of PHP inside Apache configuration files

I have a fresh install of CentOS (latest 8 and Stream 9) and I did this do install PHP and Apache:
dnf install httpd php
Everything works fine and PHP files are being interpreted correctly.
I realized there is a file at /etc/httpd/conf.modules.d/20-php.conf that loads PHP inside Apache using a line like:
LoadModule php_module modules/libphp.so
Great! But I cant find anywhere else a code like AddHandler xxxx php. On all the previous times I installed PHP+Apache there always would be a line to load php using AddHandler.
I am asking this because I want to allow PHP to be interepreted in my entire server HOWEVER I dont want PHP to be interpreted if the user access any directory with name dont_interpret_php. In this case, if there is a PHP file inside this directory, it should be server by Apache as is, without interpreting its content.
I found some ways on Google to do that but all of them require modifying the line AddHandler but I cant find it anywhere (not in httpd.conf, not in virtualhosts file... no where!).
Thank you so much!
Here's a fragment from one of my configurations (Apache/2.4.25):
<FilesMatch ".+\.ph(ar|p|tml)$">
SetHandler application/x-httpd-php
</FilesMatch>
Try grep -R php in your /etc/APACHE_CONFIG_DIR

ubuntu 20.04 apache2 - php files downloading

php files do not load in the browser, but are downloaded. I have read through many solutions to this problem on both serverfault.com and stackoverflow. (Many of the posts are several years old with older versions of Apache and php, and some of the config files and their locations have changed.) I've found a common set of solutions to the problem, but none of them have worked for me. The following links contain examples of the suggestions I have tried that didn't solve my problem.
apache2 on ubuntu - php files downloading
Apache shows PHP code instead of executing it
https://serverfault.com/questions/25227/why-is-php-script-downloaded-instead-of-executed
https://serverfault.com/questions/286882/apache-is-not-interpreting-php-files
I installed apache2. If I go to "localhost" in my browser, it serves up the "Apache2 Ubuntu Default Page". I installed php. "php7.4.conf" and "php7.4.load" appears in both /etc/apache2/mods-available and /etc/apache2/mods-enabled. I verify that php is enabled with sudo a2enmod php7.4, which gives
Considering conflict mpm_event for mpm_prefork:
Considering conflict mpm_worker for mpm_prefork:
Module mpm_prefork already enabled
Considering conflict php5 for php7.4:
Module php7.4 already enabled
Based off multiple replies in different questions, I have ended up with the following in my /etc/apache2/apache2.conf file (note this is not the entire file):
Include /etc/phpmyadmin/apache.conf
AddType application/x-httpd-php .php
# Use for PHP 7.x:
LoadModule php7_module modules/libphp7.4.so
AddHandler php7-script php
AddType application/x-httpd-php-source .phps
AddHandler application/x-httpd-php .phps
AddHandler application/x-httpd-php .php
AddType application/x-httpd-php .php
# Add index.php to your DirectoryIndex line:
DirectoryIndex index.html index.php
Notes: I have verified that the apache2.conf file is being executed, by adding a bad line to the file and attempting to restart apache, which resulted in an error. libphp7.4.so is located in /usr/lib/apache2/modules.
And my /etc/apache2/mods-available/php7.4.conf and /etc/apache2/mods-enabled/php7.4.conf files look like this:
<FilesMatch ".+\.ph(ar|p|tml)$">
SetHandler application/x-httpd-php
</FilesMatch>
<FilesMatch ".+\.phps$">
SetHandler application/x-httpd-php-source
# # 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(ar|p|ps|tml)$">
# Require all denied
</FilesMatch>
# Running PHP scripts in user directories is disabled by default
#
# To re-enable PHP in user directories comment the following lines
# (from <IfModule ...> to </IfModule>.) Do NOT set it to On as it
# prevents .htaccess files from disabling it.
# <IfModule mod_userdir.c>
# <Directory /home/*/public_html>
# php_admin_flag engine Off
# </Directory>
# </IfModule>
After every change I have made, I have restarted apache with sudo service apache2 restart. I do a test and still php files are downloaded. I'm using a simple php file called verify.php, which contains the following:
<?php
phpinfo();
?>
I've never worked with these things before so I could be missing something obvious? For some additional context, I'm working on a school project where I plan to use a php page to query a database and generate a table with the results.
In my case of similar hardship:
I do not need those statements in apache2.conf:List item
I have similar content in my /etc/apache2/mods-available/php7.4.conf and /etc/apache2/mods-enabled/php7.4.conf
I ran:
sudo a2dismod mpm_event && sudo a2enmod mpm_prefork && sudo a2enmod php7.4
However what got me to work is a misspelled 'php' as 'phd' (in SetHandler application/x-httpd-php of etc/apache2.conf):
<FilesMatch \.php$>
SetHandler application/x-httpd-php
</FilesMatch>
I think that is the only needed command to call PHP as handler.
p.s. I have more than one instance of messed up config files.. i haven't found a good control template for all those linux configuration process. (this time the install was started by somebody, i took over the second part)
(This section contains notes and hints specific to Apache 2.x installs of PHP on Unix systems.
https://www.php.net/manual/en/install.unix.apache2.php)

I need libphp7.so module to configure apache on Centos

I follow the tutorial of this link to use php7 or phpng on my Centos 6.5 with apache.
I can execute php scripts in the console but I would like to be able to run php scripts using the Apache Server.
I need some help because I can't find the libphp7.so module. I don't know if I have to build it or what.
I believe you need to add --with-apxs2 to your configure script. According to the link you provided I do not see that in the configure flags. --with-apxs2 will "Build shared Apache 2.0 Handler module". You may also need to make sure in your apache configuration you have:
<FilesMatch \.php$>
SetHandler application/x-httpd-php
</FilesMatch>
The first time I built php7 it just displayed the source rather than execute it, but adding that caused it to execute the code.
If you want to try it out with Docker I just created a Docker image for PHP7 at https://registry.hub.docker.com/u/silintl/php7/ You can also just view the Dockerfile which includes all the commands used to install and configure it.
In my config file /etc/httpd/conf.modules.d/15-php.conf which is loaded by the parent config file /etc/httpd/conf/httpd.conf I found the following default configuration:
<IfModule !mod_php5.c>
<IfModule prefork.c>
LoadModule php7_module modules/libphp7.so
</IfModule>
</IfModule>
<IfModule !mod_php5.c>
<IfModule !prefork.c>
LoadModule php7_module modules/libphp7-zts.so
</IfModule>
</IfModule>
Using information provided by #JanePage and #PhilipShipley, i change it to this:
LoadModule php7_module modules/libphp7.so
<FilesMatch \.php$>
SetHandler application/x-httpd-php
</FilesMatch>
and Apache started working fine.
Thanks Phillip who gaves some clues for this issue, but in my case I've solved my problem this way :
1 - Copy the Library php7.so that you have into apache module directory with this command :
sudo cp /etc/httpd/modules/libphp7.so /opt/bitnami/apache2/modules
2 - Add in your Apache config file following code :
LoadModule php7_module ./modules/libphp7.so
SetHandler application/x-httpd-php
And PHP worked fine after that !

Apache, PHP, Centos 6

When I navigate to my website (e.g. www.mydomain.com), the browser opens a pop-up window to download a file.
In my httpdocs folder i have:
- index.html
- index.php ()
- I do NOT have .htaccess file
Checking in the browser:
- www.mydomain.com/index.html -> it works fine
- www.mydomain.com/index.php -> it works WRONG (it downloads the index.php file)
- www.mydomain.com -> it works WRONG (it download "descarga" file with index.php contents).
PHP is installed, and when I run the php -v command, it says that I am using PHP 5.3
I have other domains on the same server working fine with php files, so AddHandler or LoadModule should not be the problem as far as PHP is a constant for all my domains.
This is my php.conf and looks like everythin is right:
# PHP is an HTML-embedded scripting language which attempts to make it
# easy for developers to write dynamically generated webpages.
#
<IfModule prefork.c>
LoadModule php5_module modules/libphp5.so
</IfModule>
<IfModule worker.c>
LoadModule php5_module modules/libphp5-zts.so
</IfModule>
#
# Cause the PHP interpreter to handle files with a .php extension.
#
AddHandler php5-script .php
AddType text/html .php
#
# Add index.php to the list of files that will be served as directory
# indexes.
#
DirectoryIndex index.php
What should I try to navigate to my domain successfully?
Make sure you have the PHP module loaded. Somewhere in your config you should see something like:
LoadModule php5_module modules/libphp5.so
Then, make you you add a handler to handle files with the php extension:
AddHandler application/x-httpd-php .php .php5 .phtml
AddHandler application/x-httpd-php-source .phps
I would recommend you take advantage of yum as it can help you get things setup quickly, and allows for easy ways to update your server down the road per the yum update command. The following are some commands that may be of use to you:
yum update
yum install php*
yum install httpd*
yum update
chkconfig httpd on
/etc/init.d/httpd start
service httpd restart
I solved the problem.
There was just one domain asking me to download a file, rest of domains were working fine.
I could not find what was the problem so I went to my Plesk Panel, I deleted that domain, and I created the same domain again. After that it was working fine.
My server is a Linux CentOS 6, and I use Parallel Plesk Panel 11.
Thanks to everybody that was trying to help me on this issue.
J. Pablo.

Why are my PHP files showing as plain text? [duplicate]

This question already has answers here:
PHP code is not being executed, but the code shows in the browser source code
(35 answers)
Closed 7 years ago.
I've been writing PHP applications using PHP for a while in WAMP. Now I'm installing PHP and Apache HTTP Server separately on my work PC. I've installed PHP 5, and the latest Apache. I go to localhost and see it works!
Now I add a file called test.php which displays:
<?php
phpinfo();
?>
But in the browser it just displays plain text. Is there somewhere I have explicitly tell it to use PHP 5?
You should install the PHP 5 library for Apache.
For Debian and Ubuntu:
apt-get install libapache2-mod-php5
And restart the Apache:
service apache2 restart
You'll need to add this to your server configuration:
AddType application/x-httpd-php .php
That is assuming you have installed PHP properly, which may not be the case since it doesn't work where it normally would immediately after installing.
It is entirely possible that you'll also have to add the php .so/.dll file to your Apache configuration using a LoadModule directive (usually in httpd.conf).
Yet another reason (not for this case, but maybe it'll save some nerves for someone) is that in PHP 5.5 short open tags <? phpinfo(); ?> are disabled by default.
So the PHP interpreter would process code within short tags as plain text. In previous versions PHP this feature was enable by default. So the new behaviour can be a little bit mysterious.
You need to configure Apache (the webserver) to process PHP scripts as PHP. Check Apache's configuration. You need to load the module (the path may differ on your system):
LoadModule php5_module "c:/php/php5apache.dll"
And you also need to tell Apache what to process with PHP:
AddType application/x-httpd-php .php
See the documentation for more details.
You might also, like me, have installed php-cgi prior to installing Apache and when doing so it doesn't set up Apache properly to run PHP, removing PHP entirely and reinstalling seemed to fix my problem.
You will need to add handlers in Apache to handle php code.
Edit by command sudo vi /etc/httpd/conf/httpd.conf
Add these two handlers
AddType application/x-httpd-php .php
AddType application/x-httpd-php .php3
at position specified below
<IfModule mime_module>
AddType application/x-compress .Z
AddType application/x-gzip .gz .tgz
--Add Here--
</IfModule>
for more details on AddType handlers
http://httpd.apache.org/docs/2.2/mod/mod_mime.html
Are you using the userdir mod?
In that case the thing is that PHP5 seems to be disabling running scripts from that location by default and you have to comment out the following lines:
<IfModule mod_userdir.c>
<Directory /home/*/public_html>
php_admin_flag engine Off
</Directory>
</IfModule>
in /etc/apache2/mods-enabled/php5.conf (on a ubuntu system)

Categories