On my local dev maschine (Ubuntu), I have apache2 and different php version to develop different projects, managed over Vhosts.
Is there a way to configure each vhost to use a specific (already installed) PHP version? So for example:
example1.local should use PHP 7.4
example2.local should use PHP 8.0
example3.local should use PHP 8.0
example4.local should use PHP 5.6
Currently, I am always running for example sudo a2enmod php{X} & sudo a2dismod php{Y} & sudo service apache2 restart to switch versions, which is really anoying.
I see you are using Apache. So you can use a .htaccess file and add there
AddHandler application/x-httpd-php74 .php
In this way your vhost uses php version 7.4
or with normal apache configuration
<VirtualHost *:80>
ServerAdmin webmaster#example.com
ServerName example.com
ServerAlias www.example.com
<IfModule mod_fastcgi.c>
AddHandler php74-fcgi .php
</IfModule>
</VirtualHost>
Related
I have phpmyadmin installed on my ubuntu server, that runs several versions of PHP depending on the website used.
The default version is 7.4, but phpmyadmin use 7.2 for unknown reason.
Simple question : where do I change that ?
Cannot find it in any forum or documentation...
I tried to put in phpmyadmin/apache.conf what I use in my virtual hosts sites :
<FilesMatch \.php$>
SetHandler "proxy:unix:/var/run/php/php7.4-fpm.sock|fcgi://localhost/"
</FilesMatch>
but it doesn't work and I still have 7.2
Thanks a lot !
Solution found !
As my Phpmyadmin installation doesn't use the virtual host file system, the file to modify is /etc/phpmyadmin/apache.conf
I put a code, but outside the Directory tags, and you have to put it inside to make it work :
<Directory /usr/share/phpmyadmin>
<FilesMatch \.php$>
SetHandler "proxy:unix:/var/run/php/php7.4-fpm.sock|fcgi://localhost/"
</FilesMatch>
</Directory>
And then, restart Apache change the PHP version of phpmyadmin page.
Thanks !
Add the below code inside /etc/phpmyadmin/apache.conf
<Directory /usr/share/phpmyadmin>
<FilesMatch \.php$>
SetHandler "proxy:unix:/var/run/php/php8.1-fpm.sock|fcgi://localhost/"
</FilesMatch>
</Directory>
After that restart Apache
sudo systemctl restart apache2
Here's how I do it:
Go to phpMyAdmin config
cd /etc/apache2/sites-available
Open config file
sudo nano phpmyadmin.conf
Put the following line inside the tags to serve 7.4:
include /etc/apache2/conf-available/php7.4-fpm.conf
Restart apache:
sudo systemctl restart apache2
Note that my setup is mostly from this tutorial.
I installed php 8.1 and some of my sites are not working. I select version php 7.4
use the command update-alternatives --config php
and apache2 shows that the version is selected. But the Apache server itself runs php 8.1 this proves the php.ini file and not working sites. How do I back to php 7.4?
I've also tried making changes like:
AddHandler application/x-httpd-php73 .php
in a .htaccess
and
<VirtualHost *:80>
ServerAdmin webmaster#example.com
ServerName example.com
ServerAlias www.example.com
<IfModule mod_fastcgi.c>
AddHandler php74-fcgi .php
</IfModule>
So doesn't work at all, I no have idea the next steps...
enter image description here
turned out to be easier than it was, after disabling the additional config, you can freely switch between versions php
a2disconf php8.1-fpm.conf
right now I'm installing or new apache2 webserver with PHP-FPM, because the old one is running with mod_php.
I found different Tutorials at the internet, unlikely most of them 1-2 years old. Most of them use:
libapache2-mod-fastcgi in combination with Apache and PHP-FPM.
At the Ubuntu 18.04 Repository this package is not available, just the package:
libapache2-mod-fcgid
Which of them can I use now ? Or what is the difference between both of them ? Unfortunately I cant really find a good explanation at the internet.
Furthermore I often read about
mod_proxy_fcgi
does that mean I dont need the libaapche2-mod-f... packages anymore ? ?
Right now I installed everything like this and it works, but I'm not sure If this is the right way:
a2enmod actions fastcgi alias proxy_fcgi
apt install php-7.2 php7.2-fpm php7.2-gd php7.2-mysql php7.2-curl php7.2-xml php7.2-zip php7.2-intl php7.2-mbstring php7.2-bz2 php7.2-json php7.2-apcu php7.2-imagick
a2enmod actions fastcgi alias proxy_fcgi
vHost:
<VirtualHost *:80>
ServerAdmin webmaster#localhost
<FilesMatch \.php$>
SetHandler "proxy:unix:/var/run/php/php7.2-fpm.sock|fcgi://localhost/"
</FilesMatch>
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
PHP-FPM is running (see picture of info.php):
PHP-FPM Working
And what is the difference between:
SetHandler and FastCgiExternalServer and ProxyPassMatch ^/(..php(/.)?)$ fcgi://127.0.0.1:9000/path/to/your/documentroot/$1
?
I've got the feeling, that every tutorial is telling me something different and I cant really figure out what the best practice is in 2018 with Ubuntu2018.
I know this is an old question but I wanted to give an updated response.
As of the release of php5.3.3 (in 2010) a lot has changed.
Some great info can be found on the Apache HTTP Server Wiki
The short answer (Note: replace php7.2 with the version you have installed) as to how to install only PHP-FPM on an Ubuntu apache2 server is:
# Install php-fpm:
apt install php-fpm
# Disable mod_php (Apache Handler API):
a2dismod php*
# Enable Apache Modules/Configs required by fpm:
a2enmod proxy_fcgi setenvif
a2enconf php7.2-fpm.conf
# Restart the services:
systemctl restart php7.2-fpm.service systemctl restart apache2.service
You are also going to need to change from using Pre-fork as your Multi-Processing Module (MPM) if you are going to run PHP-FPM. Here are some instructions.
Detailed Explanation:
There are basically 3 different Server API's that can be installed with PHP: Apache Handler, FPM, or CGI.
Looking at the different config files can help to understand what you may have installed on your system.
Currently on Ubuntu 18.x with php7.x the following php.ini files get created depending on what you have installed:
/etc/php/7.2/cli/php.ini
This is the PHP-CLI program for running php on the command line.
This is included whenever you install FPM, CGI, or the Apache Handler.
You could install it directly with:
apt install php-cli
To find all the config files being used for PHP-CLI you can run:
php --ini
/etc/php/7.2/apache2/php.ini
This is the PHP plugin used by Apache. It will be found in /etc/apache2/mods-available/php7.2
If you have not installed PHP-FPM or PHP-CGI then this is the file that contains your webserver settings.
To find all the config files you need to create a phpinfo() file in the website root directory.
To install you must also enable mod_php from within Apache.
apt install libapache2-mod-php
a2enmod php7.2
/etc/php/7.2/fpm/php.ini
This is the FastCGI Process Manager. It is a wrapper for PHP processing and runs as a standalone process on the system (unlike the Apache PHP plugin).
You will only have this directory if you have installed PHP-FPM.
In this case it will be the place to make config changes for your webserver and takes the place of the apache2/php.ini file.
To find all the config files you need to create a phpinfo() file in the website root directory.
Running PHP as a fastCGI process server with PHP-FPM requires using the apache module mods-enabled/mod_proxy_fcgi it is enabled along with php-fpm.
Installing php-fpm will also configure apache with with conf-enabled/php7.2-fpm.conf that sets up FPM to run as a unix domain socket.
apt install php-fpm
a2enmod mod_proxy_fcgi
/etc/php/7.2/cgi/php.ini
This is a third way PHP could be installed. It is the legacy way of running PHP based applications as opposed to the newer PHP-FPM.
mod_fcgid is a high performance alternative to mod_cgi or mod_cgid
It would also be taking the place of the php.ini in either the Apache Plugin or PHP-FPM.
To find all the config files you need to create a phpinfo() file in the website root directory.
Again, it comes with it's own apache module and configuration: mods-enabled/fcgid.conf mods-enabled/fcgid
apt install libapache2-mod-fcgid
a2enmod fcgid
Here's my vhost for Apache connecting to FPM using mod_proxy_fcgi (apparently the recommended setup, although don't ask me for specifics!):
<VirtualHost *:80>
ServerName awesome.scot
ServerAlias localhost
DocumentRoot /var/www/html/public
<Directory "/var/www/html">
DirectoryIndex index.php
FallbackResource /index.php
Options -Indexes +FollowSymLinks
AllowOverride FileInfo All
Require all granted
</Directory>
ProxyPassMatch ^/(.*\.php)$ fcgi://php:9000/var/www/html/public/$1
</VirtualHost>
in the conf, I also have these on:
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so
If you use XDebug, you'll need to change it's port to 9001 since 9000 is now taken.
If you need to see more config, check out my Docker LAMP stack config here https://github.com/delboy1978uk/lamp
I'm using Apache 2.4 on Ubuntu 16.04, it shows the content of the index.php in browser.
here's the code of my site's conf file:
<VirtualHost *:80>
DocumentRoot /var/www/laravel/public
ServerName laravel.dev
<Directory /var/www/laravel/public>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
Why the home page renders index.php as text file?
Note: I have set 777 permissions to laravel folder. PHP is also installed.
I got the solution over here..
Actually, I had to install libapache2-mod-php and activate it i.e. a2enmod php7.0
add below command :
sudo apt-get install libapache2-mod-php7.2
(this will pull the default PHP version)
Reload Apache configuration changes by restarting service using below command
sudo service apache2 restart
I ran into same issue and was looking for answers. In my case I had multiple versions of PHP installed with all necessary libraries, including the one mentioned in answers above.
The cause of issue turned out to be not properly switching between PHP, which I do time to time while switching projects. Running again a2dismod php5.6 and then a2enmod php7.3 (switch from php5.6 to php7.3) and restarting apache did the job for me.
Hope this helps someone with similar issue like mine.
I set up a virtual host that uses mod_suexec to run PHP scripts as a different user than www-data.
<VirtualHost *:80>
ServerName my.server.com
DocumentRoot /srv/my-site
SuexecUserGroup webconfig webconfig
</VirtualHost>
However, when I create a directory or a file (mkdir, file_put_contents), those files/dirs have www-data as an owner. I also can not read files that are only readable by webconfig.
I also noticed this strange behaviour: This php file:
echo get_current_user()."\n";
echo `whoami`."\n";
echo exec('whoami')."\n";
produces the following output:
webconfig
www-data
www-data
mod_php5 is actually incompatible with mod_suexec. As described in this article, you have to use mod_suphp and php-cgi instead. The downside is that this decreases performance.
A simple solution that works for Ubuntu is
apt-get install -y suphp-common, libapache2-mod-suphp
a2dismod php5
a2enmod suphp
The linked article describes how to use mod_php5 for some vhosts and mod_suphp for others.