.htaccess not working in amazon ec2 ubuntu instance - php

I have a server from amazon's ec2 service running on Linux Ubuntu (
Ubuntu Server 13.04 64 bit) and I have installed apache, php, and mysql. I have added a .htaccess file in my document root (i.e /var/www/).
Here is the code in .htaccess file as follows:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
If I remove .php from url like "index1" instead of "index1.php", it returns 404 browser error. It works properly in my previous server.
I have .htaccess enabled in server. I did it using command "sudo vim /etc/apache2/sites-available/default" and changed "AllowOverride None" to "AllowOverride All".
I have also checked .htaccess working by passing invalid value in htaccess file and it returns "Internal server error - 500" in browser.
Here is the link of my server information : http://54.200.58.45/mytest.php
Any help in this regard will be highly appreciated.

This is wat worked for me on a fresh EC2 instance with Ubuntu 13.10:
a2enmod rewrite
vim /etc/apache2/sites-enabled/000-default.conf
add the following to the VirtualHost
<Directory "/var/www">
AllowOverride All
</Directory>
service apache2 restart

This happens because the rewrite module doesn’t come enabled by default for security reasons.
Create a new file called rewrite.conf in /etc/apache2/mods-enabled
in the file put this line LoadModule rewrite_module /usr/lib/apache2/modules/mod_rewrite.so
Now reload server sudo service apache2 restart
This worked for me and hopefully for you, but I don’t advice this for production servers. This is information for regular Ubuntu users not for a live server.

The problem is by default in apache installed on Ubuntu doesn't have .htaccess enabled so first you have to manually enable it in a config file and restart apache.
Here are the steps.
1) Login to your EC2 instance with SSH.
ssh -i "Yourkey.pem" ubuntu#yourpublicip
2) Enable Mode Rewrite, this is just incase if it's not enabled.
sudo a2enmod rewrite
3) Go to the following directory.
cd /etc/apache2/sites-available
If you use LS command here you will see the following file.
000-default.conf
That's the file with default apache configuration which is applied to your sites in /var/www/html folder.
4) Open this file for editing.
sudo nano 000-default.conf
5) Add following lines after DocumentRoot /var/www/html line.
Options FollowSymLinks
AllowOverride All
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
6) Save the file and restart the apache.
sudo service apache2 restart
that's it and now your .htaccess file will work

You need to check whether the mod rewrite module is enabled or not first.

Tried everything and nothing worked...
2 hours later.. Finally fixed it.
So first the things to rule out.
Make sure that you have made the changes in /etc/apache2/apache2.conf and in /etc/apache2/sites-enabled/000-default.conf that everyone is talking about... (AllowOverride All and AccessFileName .htaccess must not be commented out)
Make sure that the Directory /var/www/html is pointing to the right folder. Some times it has /html and some times it's just /var/www (however it must not end in / i think, but that might not matter.)
Finally, and this is what worked for me. Restart the whole server, not just Apache. I figured out that this was my problem, after i tried stopping apache2 (service apache2 stop) and I was still able to access my website. So I did a full reboot and then all the changes I have been making for the last 2 hours, finally kicked in.
Success :)

Related

Problem encountered running PHP on localhost [duplicate]

I have recently been trying to install PHP and Apache on my computer. After many hours, they're installed. I have modified the httpd.conf and php.ini files like everyone says. I then created a simple PHP script:
<?php phpinfo(); ?>
But when I try to run it with http://127.0.0.1/phpinfo.php it just shows the source code instead of executing it. I am using Apache 2, PHP 5 and Windows Vista.
EDITED LINES:
PHP.INI:
short_open_tag = On
HTTPD.CONF
LoadModule php5_module "C:/php/php5apache2_2.dll"
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
PHPIniDir "C:/php"
You must enable php! Check the folder mods-enabled in the Apache directory (default: /etc/apache2/) to see if you find a file named php. I don't remember the extension but I think it's .so.
Also check in /var/log/apache2/error.log to see if you have any other errors.
Wow, lots of solutions here! Here's what I did on Ubuntu 16.04:
sudo apt-get install php libapache2-mod-php
sudo a2enmod mpm_prefork && sudo a2enmod php7.0
sudo service apache2 restart
For PHP 7 (May apply to previous versions as well), but I had to do this:
Add this to the bottom of /etc/apache2/apache2.conf
<FilesMatch \.php$>
SetHandler application/x-httpd-php
</FilesMatch>
Run this from the terminal:
sudo a2dismod mpm_event && sudo a2enmod mpm_prefork && sudo a2enmod php7
Then don't forget to restart Apache so it knows you changed stuff:
sudo service apache2 restart
This is a summary from: https://www.atlantic.net/community/howto/try-php7-lamp-ubuntu-14-04/
open the file
/etc/apache2/httpd.conf
and change
#LoadModule php5_module libexec/apache2/libphp5.so
into
LoadModule php5_module libexec/apache2/libphp5.so
So just uncoment the PHP module load in httpd.conf
I found this to solve my related problem. I added it to the relevant <Directory> section:
<IfModule mod_php5.c>
php_admin_flag engine on
</IfModule>
A different answer that worked for me. Is that, to install [sudo] apt-get install libapache2-mod-php5.X
Apache shows php code instead of executing Issue fixed
1. Opened php5.6 conf or php7.x conf
# following command:
$ sudo vi /etc/apache2/mods-enabled/php5.6.conf
2. Commented following lines
3. Restarted the server
$ sudo service apache2 restart
4 Enjoy :)
Thanks to others on this thread for their suggestions. Following the steps mentioned I found that the apache server was not able to start reporting a syntax error in a load file in /etc/apache2/mods-enabled. Turns out that both php7.0 and php7.1 were enabled.
a2dismod php7.0
systemctl restart apache2
and php is rendered correctly again.
Add following configuration to /etc/apache2/apache2.conf
<FilesMatch "\.php$">
SetHandler application/x-httpd-php
</FilesMatch>
And restart the apache via sudo service apache2 restart
Alright if you've tried what you've been told above or earlier(which are possible reasons) and it still displays the code instead of executing it then there is one thing which you are doing wrong that hasn't been addressed.
The url you used to access your php code; some people try to execute their php code by just dragging the .php file into the web browser. this is wrong practice and could lead to this kind of problem.
if you have saved a file as "test.php" in the C://wamp/www folder then you must access this file this way:
localhost://test.php.
this kind of mistake will arise when you access it this way: localhost://wamp/www/test.php
Hope I helped someone out there. o/
~Daniel
If you are a ubuntu user, after installing apache must run the following command in fresh installation
sudo apt-get install php5 libapache2-mod-php5 php5-mcrypt
In my case works fine.
Posting what worked for me in case in helps someone down the road, though it is an unusual case.
I had set a handler to force my web host to use a higher version of php than their default. There's was 5.1, but I wanted 5.6 so I had this:
<FilesMatch \.php$>
SetHandler php56-cgi
</FilesMatch>
in my .htaccess file.
When trying to run my site locally, having that in there caused php code to be output to the browser. Removing it solved the problem.
For PHP7 and Apache2.4, this is all you need to do:
sudo vim /etc/httpd/conf/httpd.conf
Go to the very bottom and insert the following (all by itself):
<FilesMatch "\.*">
SetHandler application/x-httpd-php
</FilesMatch>
Then, restart Apache to load the new configuration:
sudo service httpd restart
Apache will now execute all HTTP/S requests with PHP. If all you have are html, css, and php files, then this will work fine for you. If you have other languages running, then you'll need to tailor the file matching to your situation. I've tested this with phpMyAdmin and it works fine.
None of that other stuff all the people are talking about here was necessary for me. When I put the "AddType Application....." thing in the correct spot, Apache just told me that that module was already loaded and skipped it.
More information can be found here: https://httpd.apache.org/docs/2.4/mod/core.html#filesmatch
My install was done as follows:
sudo yum install -y httpd24 php70 mysql56-server php70-mysqlnd
You should also read this, if you haven't: https://httpd.apache.org/docs/current/howto/htaccess.html#when
You could delete your .htaceess file and let the system creates a new (blank or basic) one. It may be that some lines you added are incorrect statements, or something else inside is causing the problem. You can then add line by line from your old .htaccess file to see what caused the problem.
That worked for me. The lines telling machine not to display php extensions were problem in my case (RewriteCond and RewriteRule). My old .htaccess file worked just fine on a shared server, but this issue appeared when I switched to VPS.
This was happening to me also when running $_POST in MAMP. All of my .ini and httpd files were all set up correctly. If you are doing form handling and you have an html document and posting the info to a php formhandler running $POST, make sure that you are running the html file from localhost via your server, and not just locally.
This was a shortcut I did to run html documents, by just clicking the html file in my directory and launching in my web browser, when in reality to check if php is being processed in your form, you must run your html through your servers. A very simple protocol that I overlooked.
Example:
Wrong: file:///Applications/MAMP/htdocs/form/form.html
Right: http://localhost:your port number/form/form.html
Now the php should be processed once you click your submit button
What worked for me:
In active httpd.conf, find
<IfModule mime_module>
...
</IfModule>
It was missing the following
AddType application/x-httpd-php .php
AddHandler application/x-httpd-php .php
After restarting apache, .php files are correctly parsed.
if the module userdir is enabled and your site is in a userdir (~/public_html) you must check /etc/apache2/mods-enabled/php5.conf.
The following part makes it work (on Ubuntu 14.10 utopic):
# 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>
none of the above applied or worked for me... PHP7, Apache Httpd 2.2 on CentOS 6
fact is, I installed (all with yum) php BEFORE Apache... you must do the reverse: always install Apache first, then PHP, and then it works...
Indeed, you are likely missing libphp7.so and libphp7-zts.so in /etc/httpd/modules/
I was able to recover anyhow without UNinstalling PHP. I did add the magic line in /etc/httpd/conf/httpd.conf: AddType application/x-httpd-php .php
And then run: yum install php php-mysql
I tried a number of the solutions above however the fix in our scenario was to install the fpm-module.
We had installed httpd before php which may have had something to do with the issue, but to resolve we installed the following:
yum module install php:7.2
This installed the php-fpm-7.2.11-4.module+el8.1.0+5443+bc1aeb77.x86_64.rpm module which we then enabled by:
systemctl enable --now php-fpm
From that point we left the /etc/httpd/conf.d/php.conf as default and restarted httpd
service httpd restart
Then everything worked.
Hope this helps, took way longer than it should have to figure out.
I had the same problem, caused by the Debian/ Ubuntu default configuration of module suphp. It contained suPHP_Engine off for whole /usr/share, which resulted in the php sources being shown in the browser. Deactivating with a2dismod suphp was the interim solution.
I had the same problem. When I run a php file, the web browser showed me the php code instead of execute it. I had tried many times: uninstall/reinstall the wampserver64, working around the PHP/Apache settings/modules, etc. After 2 days: I realised that when I tried to run the php file within the notepad++ by pressing the default combination "ctrl + alt + shift + R" for chrome. It was trying to execute my php file like: "file///C:/wamp64/www/bla/bla.." in my chrome's address bar. That was my problem. I made the changes according to page Configuring Notepad++ to run php on localhost?. My problem was solved. But after 2 days..
Run Xampp (apache) as administrator. In google chrome type:
localhost/<insert folder name here>/<insert file name>
i.e. if folder you created is "LearnPhp", file is "chapter1.php" then type
localhost/LearnPhp/chapter1.php
I created this folder in the xampp folder in the htdocs folder which gets created when you download xampp.
Debian 9 solution:
touch /etc/apache2/conf-available/php.conf
Add to file next lines:
<IfModule mod_php5.c>
<IfModule mod_mime.c>
AddType application/x-httpd-php .php
</IfModule>
<FilesMatch ".+\.php$">
SetHandler application/x-httpd-php
</FilesMatch>
</IfModule>
<IfModule mod_php.c>
<IfModule mod_mime.c>
AddType application/x-httpd-php .php
</IfModule>
<FilesMatch ".+\.php$">
SetHandler application/x-httpd-php
</FilesMatch>
</IfModule>
Then run:
a2enconf php && service apache2 restart
Some times if you have php version conflicts it happens
To overcome that please follow the below steps
Step 1 : Yum list installed | grep 'php'
if you have multiple versions of php like php 5.6 and php 7.0 this confilict will happens
Step 2 : yum remove **your php version **
Step 3 : Then restart the apapche /etc/init.d/httpd restart or service apache2 restart
This solution worked for me. I purged apache2 and reinstall. It happened after purge and install. If it is the first install, you would not face this problem.
In my case with PHP7.3 Apache2.4 Ubuntu 18.04 I had to execute:
$ a2enmod actions fastcgi alias proxy_fcgi
Do your other sites/subdirectories work? Is it just a single dir that shows this behavior?
Then check for syntax error in your index.php. If you miss the very first character in a PHP-file: the "<" in "<?php", then the browser shows the rest of the file as it would be a text-file.
Example:
?php
phpinfo();
?>
Then the browser shows just "?php phpinfo(); ?>" instead of the output of phpinfo().
Centos 7, PHP 8, Apache 2
I've recently upgraded a centos server to use php 8.
After the upgrade php had stopped working and apache started serving me the php code as text.
Having spent a good while looking for which config setting had not updated I tracked it down to this:
in /etc/httpd/conf.d
<IfModule mod_php7.c>
needed changing to:
<IfModule mod_php.c>
There are other places that you may see the mod_php7 check that could probably be updated to the mod_php variant but this one definitely fixed it for me.
Don't forget to restart the server afterwards.

How to host .php files on apache2 - ubuntu?

I am hosting my web application for my maturita exam on google cloud. So I created VM (running on ubuntu) and installed apache2 on it. Then I installed PHP successfully. The problem is, it is hosting only .html files even though I installed PHP properly. When i try to host .php file an HTTP ERROR 500 will ocur.
The phpinfo.php is the only file thats working.
<?php phpinfo(); ?>
My question is. How do I change configuration of apache on Google clouds ubuntu?
PHP is the component of your setup that will process code to display dynamic content. It can run scripts, connect to your MySQL databases to get information, and hand the processed content over to your web server to display.
Once again, leverage the apt system to install PHP. In addition, include some helper packages this time so that PHP code can run under the Apache server and talk to your MySQL database:
sudo apt install php libapache2-mod-php php-mysql
This should install PHP without any problems. We’ll test this in a moment.
In most cases, you will want to modify the way that Apache serves files when a directory is requested. Currently, if a user requests a directory from the server, Apache will first look for a file called index.html. We want to tell the web server to prefer PHP files over others, so make Apache look for an index.php file first.
To do this, type this command to open the dir.conf file in a text editor with root privileges:
sudo nano /etc/apache2/mods-enabled/dir.conf
It will look like this:
/etc/apache2/mods-enabled/dir.conf
<IfModule mod_dir.c>
DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm
</IfModule>
Move the PHP index file (highlighted above) to the first position after the DirectoryIndex specification, like this:
/etc/apache2/mods-enabled/dir.conf
<IfModule mod_dir.c>
DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm
</IfModule>
When you are finished, save and close the file by pressing CTRL+X. Confirm the save by typing Y and then hit ENTER to verify the file save location.
After this, restart the Apache webserver in order for your changes to be recognized. Do this by typing this:
sudo systemctl restart apache2
You can also check on the status of the apache2 service using systemctl:
sudo systemctl status apache2
An HTTP 500 error can be caused by a problem with your Apache configurations. If your code generate a 500 error, check to make sure they have permission to run in the directory where they are located. Apache will not allow programs to execute by default. So try to give permission to your virtual host directory with CHMOD.
And also for more details about the error you should check Apache error log like:
tail -f /var/log/apache2/error.log

API written in PHP on apache web server return the php code itself [duplicate]

I have recently been trying to install PHP and Apache on my computer. After many hours, they're installed. I have modified the httpd.conf and php.ini files like everyone says. I then created a simple PHP script:
<?php phpinfo(); ?>
But when I try to run it with http://127.0.0.1/phpinfo.php it just shows the source code instead of executing it. I am using Apache 2, PHP 5 and Windows Vista.
EDITED LINES:
PHP.INI:
short_open_tag = On
HTTPD.CONF
LoadModule php5_module "C:/php/php5apache2_2.dll"
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
PHPIniDir "C:/php"
You must enable php! Check the folder mods-enabled in the Apache directory (default: /etc/apache2/) to see if you find a file named php. I don't remember the extension but I think it's .so.
Also check in /var/log/apache2/error.log to see if you have any other errors.
Wow, lots of solutions here! Here's what I did on Ubuntu 16.04:
sudo apt-get install php libapache2-mod-php
sudo a2enmod mpm_prefork && sudo a2enmod php7.0
sudo service apache2 restart
For PHP 7 (May apply to previous versions as well), but I had to do this:
Add this to the bottom of /etc/apache2/apache2.conf
<FilesMatch \.php$>
SetHandler application/x-httpd-php
</FilesMatch>
Run this from the terminal:
sudo a2dismod mpm_event && sudo a2enmod mpm_prefork && sudo a2enmod php7
Then don't forget to restart Apache so it knows you changed stuff:
sudo service apache2 restart
This is a summary from: https://www.atlantic.net/community/howto/try-php7-lamp-ubuntu-14-04/
open the file
/etc/apache2/httpd.conf
and change
#LoadModule php5_module libexec/apache2/libphp5.so
into
LoadModule php5_module libexec/apache2/libphp5.so
So just uncoment the PHP module load in httpd.conf
I found this to solve my related problem. I added it to the relevant <Directory> section:
<IfModule mod_php5.c>
php_admin_flag engine on
</IfModule>
A different answer that worked for me. Is that, to install [sudo] apt-get install libapache2-mod-php5.X
Apache shows php code instead of executing Issue fixed
1. Opened php5.6 conf or php7.x conf
# following command:
$ sudo vi /etc/apache2/mods-enabled/php5.6.conf
2. Commented following lines
3. Restarted the server
$ sudo service apache2 restart
4 Enjoy :)
Thanks to others on this thread for their suggestions. Following the steps mentioned I found that the apache server was not able to start reporting a syntax error in a load file in /etc/apache2/mods-enabled. Turns out that both php7.0 and php7.1 were enabled.
a2dismod php7.0
systemctl restart apache2
and php is rendered correctly again.
Add following configuration to /etc/apache2/apache2.conf
<FilesMatch "\.php$">
SetHandler application/x-httpd-php
</FilesMatch>
And restart the apache via sudo service apache2 restart
Alright if you've tried what you've been told above or earlier(which are possible reasons) and it still displays the code instead of executing it then there is one thing which you are doing wrong that hasn't been addressed.
The url you used to access your php code; some people try to execute their php code by just dragging the .php file into the web browser. this is wrong practice and could lead to this kind of problem.
if you have saved a file as "test.php" in the C://wamp/www folder then you must access this file this way:
localhost://test.php.
this kind of mistake will arise when you access it this way: localhost://wamp/www/test.php
Hope I helped someone out there. o/
~Daniel
If you are a ubuntu user, after installing apache must run the following command in fresh installation
sudo apt-get install php5 libapache2-mod-php5 php5-mcrypt
In my case works fine.
Posting what worked for me in case in helps someone down the road, though it is an unusual case.
I had set a handler to force my web host to use a higher version of php than their default. There's was 5.1, but I wanted 5.6 so I had this:
<FilesMatch \.php$>
SetHandler php56-cgi
</FilesMatch>
in my .htaccess file.
When trying to run my site locally, having that in there caused php code to be output to the browser. Removing it solved the problem.
For PHP7 and Apache2.4, this is all you need to do:
sudo vim /etc/httpd/conf/httpd.conf
Go to the very bottom and insert the following (all by itself):
<FilesMatch "\.*">
SetHandler application/x-httpd-php
</FilesMatch>
Then, restart Apache to load the new configuration:
sudo service httpd restart
Apache will now execute all HTTP/S requests with PHP. If all you have are html, css, and php files, then this will work fine for you. If you have other languages running, then you'll need to tailor the file matching to your situation. I've tested this with phpMyAdmin and it works fine.
None of that other stuff all the people are talking about here was necessary for me. When I put the "AddType Application....." thing in the correct spot, Apache just told me that that module was already loaded and skipped it.
More information can be found here: https://httpd.apache.org/docs/2.4/mod/core.html#filesmatch
My install was done as follows:
sudo yum install -y httpd24 php70 mysql56-server php70-mysqlnd
You should also read this, if you haven't: https://httpd.apache.org/docs/current/howto/htaccess.html#when
You could delete your .htaceess file and let the system creates a new (blank or basic) one. It may be that some lines you added are incorrect statements, or something else inside is causing the problem. You can then add line by line from your old .htaccess file to see what caused the problem.
That worked for me. The lines telling machine not to display php extensions were problem in my case (RewriteCond and RewriteRule). My old .htaccess file worked just fine on a shared server, but this issue appeared when I switched to VPS.
This was happening to me also when running $_POST in MAMP. All of my .ini and httpd files were all set up correctly. If you are doing form handling and you have an html document and posting the info to a php formhandler running $POST, make sure that you are running the html file from localhost via your server, and not just locally.
This was a shortcut I did to run html documents, by just clicking the html file in my directory and launching in my web browser, when in reality to check if php is being processed in your form, you must run your html through your servers. A very simple protocol that I overlooked.
Example:
Wrong: file:///Applications/MAMP/htdocs/form/form.html
Right: http://localhost:your port number/form/form.html
Now the php should be processed once you click your submit button
What worked for me:
In active httpd.conf, find
<IfModule mime_module>
...
</IfModule>
It was missing the following
AddType application/x-httpd-php .php
AddHandler application/x-httpd-php .php
After restarting apache, .php files are correctly parsed.
if the module userdir is enabled and your site is in a userdir (~/public_html) you must check /etc/apache2/mods-enabled/php5.conf.
The following part makes it work (on Ubuntu 14.10 utopic):
# 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>
none of the above applied or worked for me... PHP7, Apache Httpd 2.2 on CentOS 6
fact is, I installed (all with yum) php BEFORE Apache... you must do the reverse: always install Apache first, then PHP, and then it works...
Indeed, you are likely missing libphp7.so and libphp7-zts.so in /etc/httpd/modules/
I was able to recover anyhow without UNinstalling PHP. I did add the magic line in /etc/httpd/conf/httpd.conf: AddType application/x-httpd-php .php
And then run: yum install php php-mysql
I tried a number of the solutions above however the fix in our scenario was to install the fpm-module.
We had installed httpd before php which may have had something to do with the issue, but to resolve we installed the following:
yum module install php:7.2
This installed the php-fpm-7.2.11-4.module+el8.1.0+5443+bc1aeb77.x86_64.rpm module which we then enabled by:
systemctl enable --now php-fpm
From that point we left the /etc/httpd/conf.d/php.conf as default and restarted httpd
service httpd restart
Then everything worked.
Hope this helps, took way longer than it should have to figure out.
I had the same problem, caused by the Debian/ Ubuntu default configuration of module suphp. It contained suPHP_Engine off for whole /usr/share, which resulted in the php sources being shown in the browser. Deactivating with a2dismod suphp was the interim solution.
I had the same problem. When I run a php file, the web browser showed me the php code instead of execute it. I had tried many times: uninstall/reinstall the wampserver64, working around the PHP/Apache settings/modules, etc. After 2 days: I realised that when I tried to run the php file within the notepad++ by pressing the default combination "ctrl + alt + shift + R" for chrome. It was trying to execute my php file like: "file///C:/wamp64/www/bla/bla.." in my chrome's address bar. That was my problem. I made the changes according to page Configuring Notepad++ to run php on localhost?. My problem was solved. But after 2 days..
Run Xampp (apache) as administrator. In google chrome type:
localhost/<insert folder name here>/<insert file name>
i.e. if folder you created is "LearnPhp", file is "chapter1.php" then type
localhost/LearnPhp/chapter1.php
I created this folder in the xampp folder in the htdocs folder which gets created when you download xampp.
Debian 9 solution:
touch /etc/apache2/conf-available/php.conf
Add to file next lines:
<IfModule mod_php5.c>
<IfModule mod_mime.c>
AddType application/x-httpd-php .php
</IfModule>
<FilesMatch ".+\.php$">
SetHandler application/x-httpd-php
</FilesMatch>
</IfModule>
<IfModule mod_php.c>
<IfModule mod_mime.c>
AddType application/x-httpd-php .php
</IfModule>
<FilesMatch ".+\.php$">
SetHandler application/x-httpd-php
</FilesMatch>
</IfModule>
Then run:
a2enconf php && service apache2 restart
Some times if you have php version conflicts it happens
To overcome that please follow the below steps
Step 1 : Yum list installed | grep 'php'
if you have multiple versions of php like php 5.6 and php 7.0 this confilict will happens
Step 2 : yum remove **your php version **
Step 3 : Then restart the apapche /etc/init.d/httpd restart or service apache2 restart
This solution worked for me. I purged apache2 and reinstall. It happened after purge and install. If it is the first install, you would not face this problem.
In my case with PHP7.3 Apache2.4 Ubuntu 18.04 I had to execute:
$ a2enmod actions fastcgi alias proxy_fcgi
Do your other sites/subdirectories work? Is it just a single dir that shows this behavior?
Then check for syntax error in your index.php. If you miss the very first character in a PHP-file: the "<" in "<?php", then the browser shows the rest of the file as it would be a text-file.
Example:
?php
phpinfo();
?>
Then the browser shows just "?php phpinfo(); ?>" instead of the output of phpinfo().
Centos 7, PHP 8, Apache 2
I've recently upgraded a centos server to use php 8.
After the upgrade php had stopped working and apache started serving me the php code as text.
Having spent a good while looking for which config setting had not updated I tracked it down to this:
in /etc/httpd/conf.d
<IfModule mod_php7.c>
needed changing to:
<IfModule mod_php.c>
There are other places that you may see the mod_php7 check that could probably be updated to the mod_php variant but this one definitely fixed it for me.
Don't forget to restart the server afterwards.

OctoberCms Backend not working in subfolder Apache

I am install a new project of the OctoberCMS in my VPS in a subfolder, example, /var/www/myCMS/ (here this my installation of the OctoberCMS).
When I access my site by domain example: www.mydomain.com/myCMS/ I having a image of the welcome demo, picture bottom:
But which accessing the Backend (www.mydomain.com/myCMS/backend I have this error:
Any solution?
My system is:
- Apache2
- Ubuntu 16.04
sometime default apache installation is not come with mode rewrite enabled( as you described its your vpn)
so you need to enabled rewrite module manually
use this 2 commands to enable rewrite module and restart apache
a2enmod rewrite
service apache2 restart
then just check that your directory have .htaccess in order to redirect all request to the index.php
in your apache server you need this things
<Directory /var/www/html/example.com/public_html>
Options Indexes FollowSymLinks
# this is needed to work .htaccess
AllowOverride All
Require all granted
</Directory>

Apache shows PHP code instead of executing it

I have recently been trying to install PHP and Apache on my computer. After many hours, they're installed. I have modified the httpd.conf and php.ini files like everyone says. I then created a simple PHP script:
<?php phpinfo(); ?>
But when I try to run it with http://127.0.0.1/phpinfo.php it just shows the source code instead of executing it. I am using Apache 2, PHP 5 and Windows Vista.
EDITED LINES:
PHP.INI:
short_open_tag = On
HTTPD.CONF
LoadModule php5_module "C:/php/php5apache2_2.dll"
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
PHPIniDir "C:/php"
You must enable php! Check the folder mods-enabled in the Apache directory (default: /etc/apache2/) to see if you find a file named php. I don't remember the extension but I think it's .so.
Also check in /var/log/apache2/error.log to see if you have any other errors.
Wow, lots of solutions here! Here's what I did on Ubuntu 16.04:
sudo apt-get install php libapache2-mod-php
sudo a2enmod mpm_prefork && sudo a2enmod php7.0
sudo service apache2 restart
For PHP 7 (May apply to previous versions as well), but I had to do this:
Add this to the bottom of /etc/apache2/apache2.conf
<FilesMatch \.php$>
SetHandler application/x-httpd-php
</FilesMatch>
Run this from the terminal:
sudo a2dismod mpm_event && sudo a2enmod mpm_prefork && sudo a2enmod php7
Then don't forget to restart Apache so it knows you changed stuff:
sudo service apache2 restart
This is a summary from: https://www.atlantic.net/community/howto/try-php7-lamp-ubuntu-14-04/
open the file
/etc/apache2/httpd.conf
and change
#LoadModule php5_module libexec/apache2/libphp5.so
into
LoadModule php5_module libexec/apache2/libphp5.so
So just uncoment the PHP module load in httpd.conf
I found this to solve my related problem. I added it to the relevant <Directory> section:
<IfModule mod_php5.c>
php_admin_flag engine on
</IfModule>
A different answer that worked for me. Is that, to install [sudo] apt-get install libapache2-mod-php5.X
Apache shows php code instead of executing Issue fixed
1. Opened php5.6 conf or php7.x conf
# following command:
$ sudo vi /etc/apache2/mods-enabled/php5.6.conf
2. Commented following lines
3. Restarted the server
$ sudo service apache2 restart
4 Enjoy :)
Thanks to others on this thread for their suggestions. Following the steps mentioned I found that the apache server was not able to start reporting a syntax error in a load file in /etc/apache2/mods-enabled. Turns out that both php7.0 and php7.1 were enabled.
a2dismod php7.0
systemctl restart apache2
and php is rendered correctly again.
Add following configuration to /etc/apache2/apache2.conf
<FilesMatch "\.php$">
SetHandler application/x-httpd-php
</FilesMatch>
And restart the apache via sudo service apache2 restart
Alright if you've tried what you've been told above or earlier(which are possible reasons) and it still displays the code instead of executing it then there is one thing which you are doing wrong that hasn't been addressed.
The url you used to access your php code; some people try to execute their php code by just dragging the .php file into the web browser. this is wrong practice and could lead to this kind of problem.
if you have saved a file as "test.php" in the C://wamp/www folder then you must access this file this way:
localhost://test.php.
this kind of mistake will arise when you access it this way: localhost://wamp/www/test.php
Hope I helped someone out there. o/
~Daniel
If you are a ubuntu user, after installing apache must run the following command in fresh installation
sudo apt-get install php5 libapache2-mod-php5 php5-mcrypt
In my case works fine.
Posting what worked for me in case in helps someone down the road, though it is an unusual case.
I had set a handler to force my web host to use a higher version of php than their default. There's was 5.1, but I wanted 5.6 so I had this:
<FilesMatch \.php$>
SetHandler php56-cgi
</FilesMatch>
in my .htaccess file.
When trying to run my site locally, having that in there caused php code to be output to the browser. Removing it solved the problem.
For PHP7 and Apache2.4, this is all you need to do:
sudo vim /etc/httpd/conf/httpd.conf
Go to the very bottom and insert the following (all by itself):
<FilesMatch "\.*">
SetHandler application/x-httpd-php
</FilesMatch>
Then, restart Apache to load the new configuration:
sudo service httpd restart
Apache will now execute all HTTP/S requests with PHP. If all you have are html, css, and php files, then this will work fine for you. If you have other languages running, then you'll need to tailor the file matching to your situation. I've tested this with phpMyAdmin and it works fine.
None of that other stuff all the people are talking about here was necessary for me. When I put the "AddType Application....." thing in the correct spot, Apache just told me that that module was already loaded and skipped it.
More information can be found here: https://httpd.apache.org/docs/2.4/mod/core.html#filesmatch
My install was done as follows:
sudo yum install -y httpd24 php70 mysql56-server php70-mysqlnd
You should also read this, if you haven't: https://httpd.apache.org/docs/current/howto/htaccess.html#when
You could delete your .htaceess file and let the system creates a new (blank or basic) one. It may be that some lines you added are incorrect statements, or something else inside is causing the problem. You can then add line by line from your old .htaccess file to see what caused the problem.
That worked for me. The lines telling machine not to display php extensions were problem in my case (RewriteCond and RewriteRule). My old .htaccess file worked just fine on a shared server, but this issue appeared when I switched to VPS.
This was happening to me also when running $_POST in MAMP. All of my .ini and httpd files were all set up correctly. If you are doing form handling and you have an html document and posting the info to a php formhandler running $POST, make sure that you are running the html file from localhost via your server, and not just locally.
This was a shortcut I did to run html documents, by just clicking the html file in my directory and launching in my web browser, when in reality to check if php is being processed in your form, you must run your html through your servers. A very simple protocol that I overlooked.
Example:
Wrong: file:///Applications/MAMP/htdocs/form/form.html
Right: http://localhost:your port number/form/form.html
Now the php should be processed once you click your submit button
What worked for me:
In active httpd.conf, find
<IfModule mime_module>
...
</IfModule>
It was missing the following
AddType application/x-httpd-php .php
AddHandler application/x-httpd-php .php
After restarting apache, .php files are correctly parsed.
if the module userdir is enabled and your site is in a userdir (~/public_html) you must check /etc/apache2/mods-enabled/php5.conf.
The following part makes it work (on Ubuntu 14.10 utopic):
# 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>
none of the above applied or worked for me... PHP7, Apache Httpd 2.2 on CentOS 6
fact is, I installed (all with yum) php BEFORE Apache... you must do the reverse: always install Apache first, then PHP, and then it works...
Indeed, you are likely missing libphp7.so and libphp7-zts.so in /etc/httpd/modules/
I was able to recover anyhow without UNinstalling PHP. I did add the magic line in /etc/httpd/conf/httpd.conf: AddType application/x-httpd-php .php
And then run: yum install php php-mysql
I tried a number of the solutions above however the fix in our scenario was to install the fpm-module.
We had installed httpd before php which may have had something to do with the issue, but to resolve we installed the following:
yum module install php:7.2
This installed the php-fpm-7.2.11-4.module+el8.1.0+5443+bc1aeb77.x86_64.rpm module which we then enabled by:
systemctl enable --now php-fpm
From that point we left the /etc/httpd/conf.d/php.conf as default and restarted httpd
service httpd restart
Then everything worked.
Hope this helps, took way longer than it should have to figure out.
I had the same problem, caused by the Debian/ Ubuntu default configuration of module suphp. It contained suPHP_Engine off for whole /usr/share, which resulted in the php sources being shown in the browser. Deactivating with a2dismod suphp was the interim solution.
I had the same problem. When I run a php file, the web browser showed me the php code instead of execute it. I had tried many times: uninstall/reinstall the wampserver64, working around the PHP/Apache settings/modules, etc. After 2 days: I realised that when I tried to run the php file within the notepad++ by pressing the default combination "ctrl + alt + shift + R" for chrome. It was trying to execute my php file like: "file///C:/wamp64/www/bla/bla.." in my chrome's address bar. That was my problem. I made the changes according to page Configuring Notepad++ to run php on localhost?. My problem was solved. But after 2 days..
Run Xampp (apache) as administrator. In google chrome type:
localhost/<insert folder name here>/<insert file name>
i.e. if folder you created is "LearnPhp", file is "chapter1.php" then type
localhost/LearnPhp/chapter1.php
I created this folder in the xampp folder in the htdocs folder which gets created when you download xampp.
Debian 9 solution:
touch /etc/apache2/conf-available/php.conf
Add to file next lines:
<IfModule mod_php5.c>
<IfModule mod_mime.c>
AddType application/x-httpd-php .php
</IfModule>
<FilesMatch ".+\.php$">
SetHandler application/x-httpd-php
</FilesMatch>
</IfModule>
<IfModule mod_php.c>
<IfModule mod_mime.c>
AddType application/x-httpd-php .php
</IfModule>
<FilesMatch ".+\.php$">
SetHandler application/x-httpd-php
</FilesMatch>
</IfModule>
Then run:
a2enconf php && service apache2 restart
Some times if you have php version conflicts it happens
To overcome that please follow the below steps
Step 1 : Yum list installed | grep 'php'
if you have multiple versions of php like php 5.6 and php 7.0 this confilict will happens
Step 2 : yum remove **your php version **
Step 3 : Then restart the apapche /etc/init.d/httpd restart or service apache2 restart
This solution worked for me. I purged apache2 and reinstall. It happened after purge and install. If it is the first install, you would not face this problem.
In my case with PHP7.3 Apache2.4 Ubuntu 18.04 I had to execute:
$ a2enmod actions fastcgi alias proxy_fcgi
Do your other sites/subdirectories work? Is it just a single dir that shows this behavior?
Then check for syntax error in your index.php. If you miss the very first character in a PHP-file: the "<" in "<?php", then the browser shows the rest of the file as it would be a text-file.
Example:
?php
phpinfo();
?>
Then the browser shows just "?php phpinfo(); ?>" instead of the output of phpinfo().
Centos 7, PHP 8, Apache 2
I've recently upgraded a centos server to use php 8.
After the upgrade php had stopped working and apache started serving me the php code as text.
Having spent a good while looking for which config setting had not updated I tracked it down to this:
in /etc/httpd/conf.d
<IfModule mod_php7.c>
needed changing to:
<IfModule mod_php.c>
There are other places that you may see the mod_php7 check that could probably be updated to the mod_php variant but this one definitely fixed it for me.
Don't forget to restart the server afterwards.

Categories