apache2 keeps serving old versions of php files - php

as in the title, this problem still persist, i've configured a virtualHost in apache2(on Ubuntu 12.04 on OVH Dedicated server), i've put a simple index.php in the base directory of my VirtualHost that contains this simple code:
<?php
echo "Hello world! ";
?>
but when i update my file by adding or removing code, i chek it by accessing the url http://test.xxxxx.ma:8082, the update is not applied, i've cleared my browser cache, used a new browser, i've cheked if mod_cache is enabled but it's not and i'm sur i'm working on the right directory, the new version of the file index.php is only served after a restart of apache2, my VirtualHost configuration is as follow :
<VirtualHost *:8082>
DocumentRoot "/var/www/vhosts/xxxxx.ovh.net/test"
ServerName test.xxxxx.ma
<Directory "/var/www/vhosts/xxxxx.ovh.net/test">
<IfModule sapi_apache2.c>
php_admin_flag engine on
</IfModule>
<IfModule mod_php5.c>
php_admin_flag engine on
</IfModule>
AllowOverride All
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch +Indexes
allow from all
</Directory>
</VirtualHost>

try adding
# Disable Caching for Scripts and Other Dynamic Files
<FilesMatch "\.(pl|php|cgi|spl|scgi|fcgi)$">
Header unset Cache-Control
</FilesMatch>
in a .htaccess file

Related

Why Xampp server allow open not existing path

I have a problem with xampp etc. When I typing in URL address, for example, http://localhost/project/index.php/whatever.php server opened index.php with crashed style instead Not Found
The requested URL was not found on this server or something like that because /whatever.php does not exist in the project.
In console showed:
Resource interpreted as Stylesheet but transferred with MIME type text/html: "http://localhost/project/index.php/style/style.css".
How I can prevent the open not existing path from my project ?
All versions of Apache allow this, it's normal... To solve your issue, please, edit your httpd.conf and where you have your VirtualHost config add this line:
AcceptPathInfo Off
Like here:
<VirtualHost localhost:80>
ServerName localhost:80
ServerAlias localhost
ErrorLog "${SRVROOT}/logs/localhost-error.log"
TransferLog "${SRVROOT}/logs/localhost-access.log"
DocumentRoot "D:/Web/www"
<Directory "D:/Web/www">
Require all granted
Options Indexes FollowSymLinks Includes ExecCGI
AcceptPathInfo Off
AllowOverride All
<IfModule mod_deflate.c>
SetOutputFilter DEFLATE
</IfModule>
</Directory>
</VirtualHost>
If not enough add this line to your .htaccess
################################################################################
######################### Remove /index.php/ from URLs #########################
################################################################################
RedirectMatch 301 ^/index\.php(/.*) $1
Hope this helps.

Different PHP version for each folder on my server

I have little knowledge of how to configure Apache and the server. Currently, my server is running a version of PHP 5.X
I would like to upgrade to PHP 7 but there would be a lot of projects that would need to be upgraded.
So here's my question :
Can I specify a version of PHP for a specific folder on my server? For example, I have 20 projects in /var/www/html
Project 01/
Project 02/
...
Project 20/
All are working on PHP 5.
For my new project, could I force the directory "project 21" to use PHP7 (while the others still use PHP5)?
Thanks.
It's a bit old question, but here is how to do it, you need to use php-fpm version and fast CGI to do it.
You can do it for example with virtual host config. This example is fitting if you have a root and inside the root multiple websites (from a browser you reach it like this: www.domain.com/site1) this is from my dev environment, so the settings are not fully fit for production:
<VirtualHost *:8080>
#ServerName www.example.com
<Directory /home/my_user/environment>
Options Indexes FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
AllowOverride All
Require all granted
<FilesMatch ".+\.ph(ar|p|tml)$">
SetHandler "proxy:unix:/run/php/php7.3-fpm.sock|fcgi://localhost"
</FilesMatch>
</Directory>
<Directory /home/my_user/environment/site1>
<FilesMatch ".+\.ph(ar|p|tml)$">
SetHandler "proxy:unix:/run/php/php5.6-fpm.sock|fcgi://localhost"
</FilesMatch>
</Directory>
<Directory /home/my_user/environment/site2>
<FilesMatch ".+\.ph(ar|p|tml)$">
SetHandler "proxy:unix:/run/php/php7.2-fpm.sock|fcgi://localhost"
</FilesMatch>
</Directory>
ServerAdmin webmaster#localhost
DocumentRoot /home/my_user/environment
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
AccessFileName .htaccess
</VirtualHost>
Or if every site has its own domain you can use multiple vhosts:
<VirtualHost *:8080>
#ServerName www.example.com
<Directory /home/my_user/environment>
Options Indexes FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
AllowOverride All
Require all granted
<FilesMatch ".+\.ph(ar|p|tml)$">
SetHandler "proxy:unix:/run/php/php7.3-fpm.sock|fcgi://localhost"
</FilesMatch>
</Directory>
ServerAdmin webmaster#localhost
DocumentRoot /home/my_user/environment
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
AccessFileName .htaccess
</VirtualHost>
<VirtualHost *:8080>
ServerName site1.localhost
DocumentRoot "/home/my_user/environment/site1"
<FilesMatch ".+\.ph(ar|p|tml)$">
SetHandler "proxy:unix:/run/php/php5.6-fpm.sock|fcgi://localhost"
</FilesMatch>
</VirtualHost>
<VirtualHost *:8080>
ServerName site2.localhost
DocumentRoot "/home/my_user/environment/site2"
<FilesMatch ".+\.ph(ar|p|tml)$">
SetHandler "proxy:unix:/run/php/php7.2-fpm.sock|fcgi://localhost"
</FilesMatch>
</VirtualHost>
In both examples, the default PHP is 7.3, while site1 runs from 5.6 and site2 from 7.2.
The difference between the two is that you have a domain that only points to that website or it points to multiple and just the route determinate which website should be displayed.
Side note, if you have multiple virtual hosts files, apache will choose the most specific one, that's what we using out in the second example.
This config represents ubuntu/debian systems, i.e. centos the sockets placed in a different location.
All used PHP version config needs to be enabled via a2enconfig command before you can use it.*
EDIT: *Actually not. I have all disabled and just the vhost determinate which PHP needs to run on which directory/site. I had to do this because I have a simple script that changes the default PHP version.
But it also possible without a virtual host, in this case, you need something like this between the config files:
<Directory /home/my_user/environment>
Options Indexes FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
AllowOverride All
Require all granted
<FilesMatch ".+\.ph(ar|p|tml)$">
SetHandler "proxy:unix:/run/php/php7.3-fpm.sock|fcgi://localhost"
</FilesMatch>
</Directory>
<Directory /home/my_user/environment/site1>
<FilesMatch ".+\.ph(ar|p|tml)$">
SetHandler "proxy:unix:/run/php/php5.6-fpm.sock|fcgi://localhost"
</FilesMatch>
</Directory>
<Directory /home/my_user/environment/site2>
<FilesMatch ".+\.ph(ar|p|tml)$">
SetHandler "proxy:unix:/run/php/php7.2-fpm.sock|fcgi://localhost"
</FilesMatch>
</Directory>
Some other notes that my apache root is in my user's home directory/environment. So I can easily access my web server files. For this I running apache as my user and group to prevent file permission issues.
You may also need to change the port where the apache runs (in my example 8080).
I had a very similar question: "How do I test a newer version of PHP without disrupting my existing code running an older version of PHP?"
I tried the different solutions provided through some of the other links above, including trying to use the .htaccess file to try to force a specific PHP version for just one directory, but they didn't work. I'm using a shared hosting service, so I don't have the ability to reconfigure Apache or anything like that either.
Here is a solution that does work, although not necessarily exactly in the way you requested:
I used a subdomain. I created a subdomain called "test" (could have been called "Project20" as in the question above) which I could then access using test.mydomain.com. Using my shared hosting service (Lunarpages) I was able to set up a separate directory called "test" (could have been "Project20") and put my PHP 7 files in it for testing. Then I chose for only this subdomain to use PHP 7.2 instead of PHP 5.6. Voila!
Here how, step by step (this was done on a shared hosting service using the web-based cPanel for site administration and ftp to put the files on the server:
In cPanel choose "Subdomains" and create a new subdomain, such as "test", then set the document root to "/public_html/test".
Create the "/public_html/test" directory and fill it with your project test files.
In cPanel use MultiPHPManager to choose PHP 7.2 for the subdomain "test.mydomain.com" ONLY. (Note that this MultiPHPManager tool seems to be new, there may be other ways.)
Now accessing test.mydomain.com with your web browser runs any php file using the new PHP version, and the rest of your main site is unaffected.
Here is a good way to test whether this worked:
Create a file called phpinfo.php with the following code in it:
<? phpinfo(); ?>
Then before switching to the new version of PHP (before step 3 above) visit the web page "test.mydomain.com/phpinfo.php" and you should see your old PHP version displayed.
Then complete step 3 above and refresh the page and you should see the new PHP version displayed. Voila!
Sorry for not having a nice command-line version of these directions for you, but I don't have shell access to my hosting provider...

Symfony 3, domain name shows Directory Structure

I know this question is asked a lot but I read all the questions related to this but they didn't solve my problem.
I just deployed a Symfony 3 web app and I followed the Symfony server configuration documentation just to find out that the www.domain.com displays the directory structure!
Here's the configuration that I used of file /etc/apache2/sites-available/site.com.conf:
<VirtualHost *:80>
ServerName domain.tld
ServerAlias www.domain.tld
DocumentRoot /var/www/project/web
<Directory /var/www/project/web>
AllowOverride None
Order Allow,Deny
Allow from All
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ app.php [QSA,L]
</IfModule>
</Directory>
ErrorLog /var/log/apache2/project_error.log
CustomLog /var/log/apache2/project_access.log combined
I can see the app when I go to www.domain.com/web and www.domain.com/web/app.php
I want to be able to visit www.domain.com and see the app.
Change the line:
AllowOverride None
to next one:
AllowOverride All
Remove section <IfModule mod_rewrite.c> And use standard Symfony's .htaccess file.
Also you need to set up PHP as module for your apache. Be sure to have uncommented line in http.conf that loads PHP module:
LoadModule php7_module libexec/apache2/libphp7.so
You are missing your DirectoryIndex app.php or DirectoryIndex dev_app.php contained in your directory tag. This will cause apache to load app.php if a file has not been requested.
https://httpd.apache.org/docs/2.0/mod/mod_dir.html
<Directory /var/www/project/web>
DirectoryIndex app.php
...
</Directory>
Also make sure mod rewrite is enabled.
a2enmod rewrite
You may have to use sudo.
One final note:
Options -MultiViews should be contained inside the <Directory> for use all the time and not just when mod_rewrite is enabled.

PHP in userdir not working

My /etc/apache2/mods-enabled/userdir.conf file looks like:
<IfModule mod_userdir.c>
UserDir /var/zpanel/hostdata/*/public_html/
UserDir disabled root
<Directory /var/zpanel/hostdata/*/public_html/*>
AllowOverride All
Options MultiViews Indexes SymLinksIfOwnerMatch
<Limit GET POST OPTIONS>
# Apache <= 2.2:
Order allow,deny
Allow from all
# Apache >= 2.4:
# Require all granted
</Limit>
<LimitExcept GET POST OPTIONS>
# Apache <= 2.2:
Order deny,allow
Deny from all
# Apache >= 2.4:
#Require all denied
</LimitExcept>
</Directory>
</IfModule>
And file /etc/apache2/mods-enabled/php5.conf
<FilesMatch ".+\.ph(p[345]?|t|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(p[345]?|t|tml|ps)$">
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 /var/zpanel/hostdata/*/public_html/*>
# php_admin_value engine On
# </Directory>
#</IfModule>
But php not working when in accessing web site via userdir.
www.example.com/~admin
When i access via domain then it works. I have zPanel installed
Server version: Apache/2.4.18 (Ubuntu)
In my /etc/apache2/mods-available/php7.0.conf I have this section
# 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>
I think now you know what to do.
Try the following actions:
Make sure mod_userdir is enabled (a2enmod userdir).
The $HOME and public_html dirs have the right read permissions.
sudo -Hu SOMEUSER sh -c 'chmod 755 $HOME $HOME/public_html'
Make sure php_admin_value engine is On.
sudo vim /etc/apache2/mods-enabled/php*.conf
Then find the line containing php_admin_flag and change it to:
php_admin_flag engine On
This is due that PHP is disabled by default for user dirs for Apache 2.
In your particular case, you've to uncomment the following lines:
<IfModule mod_userdir.c>
<Directory /var/zpanel/hostdata/*/public_html/*>
php_admin_value engine On
</Directory>
</IfModule>
As mention by #kenorb, this is because PHP is disabled php_admin_value engine Off by default in userdirs for Apache2.
But instead of changing the php_admin_value engine to On in /etc/apache2/mods-enabled/php*.conf, it is advised in the .conf file itself that the lines starting from <IfModule> to </IfModule> be commented out, so that .htaccess can handle the setting separately. This can be done as shown below:
#<IfModule mod_userdir.c>
# <Directory /home/*/public_html>
# php_admin_flag engine Off
# </Directory>
#</IfModule>

Apache uploads (gives for download) php instead of executing it

Im using Virtualmin on top of Webmin to configure my websites on a VPS.
Everything worked fine, but now I try to create a new domain, but when I put files in its public_html it gives the php-files to download instead of executing it. How to solve this
These is my apache config (can't find anything strange):
/etc/apache2/sites-available/testbyc.--.nu.conf
SuexecUserGroup "#1009" "#1011"
ServerName testbyc.--.nu
ServerAlias www.testbyc.--.nu
ServerAlias webmail.--.--.nu
ServerAlias admin.testbyc.--.nu
DocumentRoot /home/byc/domains/testbyc.--.nu/public_html
ErrorLog /var/log/virtualmin/testbyc.--.nu_error_log
CustomLog /var/log/virtualmin/testbyc.--.nu_access_log combined
ScriptAlias /cgi-bin/ /home/byc/domains/testbyc.--.nu/cgi-bin/
ScriptAlias /awstats/ /home/byc/domains/testbyc.--.nu/cgi-bin/
DirectoryIndex index.html index.htm index.php index.php4 index.php5
<Directory /home/byc/domains/testbyc.--.nu/public_html>
Options -Indexes +IncludesNOEXEC +SymLinksIfOwnerMatch +ExecCGI
allow from all
AllowOverride All Options=ExecCGI,Includes,IncludesNOEXEC,Indexes,MultiViews,SymLinksIfOwnerMatch
AddHandler fcgid-script .php
AddHandler fcgid-script .php5
FCGIWrapper /home/byc/domains/testbyc.--.nu/fcgi-bin/php5.fcgi .php
FCGIWrapper /home/byc/domains/testbyc.--.nu/fcgi-bin/php5.fcgi .php5
</Directory>
<Directory /home/byc/domains/testbyc.--.nu/cgi-bin>
allow from all
AllowOverride All Options=ExecCGI,Includes,IncludesNOEXEC,Indexes,MultiViews,SymLinksIfOwnerMatch
</Directory>
RewriteEngine on
RewriteCond %{HTTP_HOST} =webmail.testbyc.--.nu
RewriteRule ^(.*) https://testbyc.--.nu:20000/ [R]
RewriteCond %{HTTP_HOST} =admin.testbyc.--.nu
RewriteRule ^(.*) https://testbyc.--.nu:10000/ [R]
php_admin_value engine Off
IPCCommTimeout 31
FcgidMaxRequestLen 1073741824
<Files awstats.pl>
AuthName "testbyc.--.nu statistics"
AuthType Basic
AuthUserFile /home/byc/domains/testbyc.--.nu/.awstats-htpasswd
require valid-user
</Files>
RedirectMatch /cgi-bin/mailman/([^/\.]*)(.cgi)?(.*) https://testbyc.--.nu:10000/virtualmin-mailman/unauthenticated/$1.cgi$3
RedirectMatch /mailman/([^/\.]*)(.cgi)?(.*) https://testbyc.--.nu:10000/virtualmin-mailman/unauthenticated/$1.cgi$3
Alias /pipermail /var/lib/mailman/archives/public
Try commenting out the line
php_admin_value engine Off
It's related to a larger issue that I'm still researching, but a recent update to Virtualmin added that line to some FCGI-related config files, breaking some things for some users.
As the accepted answer has said comment out
php_admin_value engine Off
We had this issue moving a site from one Virtualmin Install with PHP 5.X to a new server with PHP 7.1.
What had happened is that the default configuration for PHP7.1 had the following
<IfModule mod_userdir.c>
<Directory /home/*/public_html>
php_admin_flag engine Off
</Directory>
</IfModule>
This stopped virtualmin from running PHP scripts in users directories by adding that flag to all the local .conf files.
The php7.conf file /etc/apache2/mods-available/php7.conf also had the following
SetHandler application/x-httpd-php
This was causing all child sites to run with the process www-data and not the virtualmin user. Causing write access errors. So comment that out also.

Categories