- How to configure Apache to run PHP as FastCGI on Ubuntu 12.04 via terminal?
- 4 Answers 4
- Installing and Configuring FastCGI and PHP-FPM on Ubuntu 18.04
- Before You Begin
- Install mod_fcgid and PHP-FPM
- Install and configure Apache and php with mod fastcgi on Ubuntu/Debian
- Install the packages
- Configure fastcgi
- Wrapper script
- Resource
- 4 Comments
How to configure Apache to run PHP as FastCGI on Ubuntu 12.04 via terminal?
My question is simply, how do you change this via the command line? I know there are many configuration files for apache.
The closest thing I have found is this question, however the directory structure does not seem to match for my OS (Ubuntu 12.04).
I’m quite bewildered how there does not seem to be a clear guide that I can find that details this process for something that seems to be so common. Forgive me if this exists. if so, please point me in the right direction.
@ta.speot.is forgivaness, pleaassuh.. Thanks Matt, of course I looked at fastcgi.com, but I found those instructions a little difficult to follow.
4 Answers 4
Thanks for previous answers they got me most of the way, but to get things working I had to combine instructions from a few places, so thought I would write up a complete set of commands.
FYI I’m running Ubuntu 14.04, Apache 2.4, and also had modphp running by default, previous instructions also left out the need to disable modphp.
Just run the following commands in a terminal one after the other.
First install the necessary packages (I leave out php5 as this assumes it’s already installed, add it back in for a first time install). Also note from Apache 2.4 up you can use the event-mpm instead of worker see http://www.vps.net/blog/2013/04/08/apache-mpms-prefork-worker-and-event/. My example shows worker, but just replace the word worker with event if you’d rather use that.
sudo apt-get install apache2-mpm-worker sudo apt-get install libapache2-mod-fastcgi php5-fpm
Now enable mods you need, and disable those you don’t.
sudo a2dismod php5 mpm_prefork sudo a2enmod actions fastcgi alias mpm_worker
Create the php5.fcgi file and give the webserver permission to use it.
sudo touch /usr/lib/cgi-bin/php5.fcgi sudo chown -R www-data:www-data /usr/lib/cgi-bin
Create a global config for php5-fpm
sudo nano /etc/apache2/conf-available/php5-fpm.conf
paste in the following (we’ll use a socket instead of IP address)
AddHandler php5.fcgi .php Action php5.fcgi /php5.fcgi Alias /php5.fcgi /usr/lib/cgi-bin/php5.fcgi FastCgiExternalServer /usr/lib/cgi-bin/php5.fcgi -socket /var/run/php5-fpm.sock -pass-header Authorization -idle-timeout 3600 Require all granted
sudo service apache2 restart && sudo service php5-fpm restart
As per other instructions paste the following into a new browseable php file on your webserver.
Open the file you just edited in a web browser, If you see «FPM/FastCGI» next to Server API, you are now serving PHP with FastCGI!
Installing and Configuring FastCGI and PHP-FPM on Ubuntu 18.04
Estamos traduciendo nuestros guías y tutoriales al Español. Es posible que usted esté viendo una traducción generada automáticamente. Estamos trabajando con traductores profesionales para verificar las traducciones de nuestro sitio web. Este proyecto es un trabajo en curso.
mod_fcgid is an Apache module that uses the FastCGI protocol to provide an interface between Apache and Common Gateway Interface (CGI) programs. CGI helps a web server handle dynamic content generation and processing for scripting languages like PHP. This dynamic functionality is commonly used when running content management systems like WordPress on a LAMP stack.
This guide will show you how to install mod_fcgid and PHP-FPM on Ubuntu 18.04. It will also provide a basic configuration that uses socket based connections, instead of TCP. These steps will enable you to run PHP through mod_fcgid . Running PHP through mod_fcgid helps to reduce the amount of system resources used by forcing the web server to act as a proxy and only pass files ending with the .php file extension to PHP-FPM. Additionally, using PHP-FPM allows each virtual host to be configured to run PHP code as individual users.
This guide assumes that you are familiar and comfortable with setting up a LAMP stack on Ubuntu 18.04. If you are new to Linux server administration, you may be interested in reading our Linux System Administration Basics guide.
Before You Begin
- Complete the steps in the How to Install a LAMP Stack on Ubuntu 18.04 guide. After completing the LAMP stack guide, you should have an Apache virtual hosts configuration for your own website. This guide will continue to refer to the site as example.com .
This guide’s examples will use PHP version 7.2. When running commands related to PHP, ensure you replace any version numbers with your own system’s PHP version.
Install mod_fcgid and PHP-FPM
In this section, you will install the mod_fcgid and PHP-FPM modules on your Ubuntu 18.04 Linode.
sudo apt-get update && sudo apt-get upgrade --show-upgraded
sudo apt-get install libapache2-mod-fcgid php-fpm htop
Install and configure Apache and php with mod fastcgi on Ubuntu/Debian
Mod_fastcgi is an apache module that enables apache to talk to fastcgi enabled applications. It can be used to run php code over fastcgi through the php-cgi binary which is fastcgi enabled.
Fastcgi has many improvements over the traditional cgi model of executing external programs inside a webserver. With fastcgi multiple processes are kept alive and each process is reused to serve multiple requests one after another. There are settings to control the maximum number of requests a process can serve, after which the process is terminated and a new one is started.
Install the packages
The first thing to do is install the necessary packages from synaptic. We need to install the apache server, mod_fastcgi, mpm worker and php along with the cgi binary. Note that if you already have php installed with mpm prefork and mod-php then it would be removed upon installing these packages
$ sudo apt-get install apache2 libapache2-mod-fastcgi apache2-mpm-worker php5 php5-cgi
On ubuntu the apache configuration file located at
/etc/apache2/sites-enabled/000-default
The php cgi binary is located at /usr/bin/php-cgi.
Configure fastcgi
After installing the packages, its time to configure apache to use mod_fastcgi to run php scripts. Like cgi, fastcgi will also run php processes using the php-cgi binary.
First enable the fastcgi module with a2enmod command
The a2enmod command copies the configuration file of the module from /etc/apache2/mods-available to the directory /etc/apache2/mods-enabled. The configuration file in this case is fastcgi.conf. It looks like this
AddHandler fastcgi-script .fcgi FastCgiIpcDir /var/lib/apache2/fastcgi
Mod_fastcgi registers a handler called fastcgi-script with apache. This can be used to specify which programs to execute through mod_fastcgi.
Ok, lets move on. Next thing is to configure the relevant vhost to run php using the fastcgi handler. Put the following configuration inside the desired vhost block in the apache configuration file.
FastCgiServer /usr/local/bin/php-fastcgi-wrapper -processes 10 -restart-delay 1 -init-start-delay 1 -pass-header HTTP_AUTHORIZATION Alias /binary /usr/local/binOptions ExecCGI SetHandler fastcgi-script AddHandler php-fastcgi .php Action php-fastcgi /binary/php-fastcgi-wrapper
The FastCgiServer registers /usr/local/bin/php-fastcgi-wrapper as the fastcgi application with various options.
Note that this is a shell script that will launch the php-cgi binary with various settings. Note that we asked the FastCgiServer to create and manage 10 processes.
The AddHandler line declares a new handler for «.php» files called php-fastcgi. This can named to anything you like.
The Action line tells apache to handle php-fastcgi file using the cgi program /cgi-bin/php-fastcgi-wrapper.
Now the location /cgi-bin/php-fastcgi-wrapper is not a real one. To make it point to the real location of wrapper script, the Alias directive is used. It points /cgi-bin to /usr/local/bin.
Wrapper script
Now comes the wrapper script that will be used by fastcgi to run php.
#!/bin/sh # Set desired PHP_FCGI_* environment variables. # Example: # PHP FastCGI processes exit after 500 requests by default. PHP_FCGI_MAX_REQUESTS=10000 export PHP_FCGI_MAX_REQUESTS PHP_FCGI_CHILDREN=5 export PHP_FCGI_CHILDREN # Replace with the path to your FastCGI-enabled PHP executable exec /usr/bin/php-cgi
Note the PHP_FCGI_CHILDREN setting. It specifies that each php process should further fork and manage 5 more child php processes. So earlier fastcgi was told to create 10 processes. The total is 10*5 = 50 processes. There are 2 levels of process management going on. Mod_fastcgi manages 10 php processes and each php process further manages 5 process each.
This file is stored at the following path
/usr/local/bin/php-fastcgi-wrapper
Make sure that the file is executable. Do a chmod on it
/usr/local/bin# chmod +x php-fastcgi-wrapper
The location of the file does not matter. The wrapper script is necessary so that various options like PHP_FCGI_MAX_REQUESTS can be passed on to the php process.
Now restart apache and test the setup by opening a php script in browser. View the contents of the $_SERVER variable in php. It should contain [FCGI_ROLE] => RESPONDER. This indicates that fastcgi is in action.
Also check the process table using htop or System Monitor. You should see 50 php-cgi processes running.
Resource
A Tech Enthusiast, Blogger, Linux Fan and a Software Developer. Writes about Computer hardware, Linux and Open Source software and coding in Python, Php and Javascript. He can be reached at [email protected] .
4 Comments
- tylercollier July 25, 2014 at 11:11 pm My previous comment about “client denied by server configuration: /usr/local/bin/php-fastcgi-wrapper” has not yet been approved, so I can’t see, edit, or reply to it. But I wanted to post my fix. From: http://stackoverflow.com/a/13923526/135101. Add [Directory /usr/local/bin]
Require all granted
[/Directory] to your vhost config. EDIT: I guess I can’t use markdown here and Disqus is munging my “html” of the directory element, so I replaced angle brackets with brackets.
- tylercollier July 25, 2014 at 11:11 pm I had to enable the multiverse source, but only from Ubuntu 14.04 (see instructions here: http://serversforhackers.com/articles/2014/05/05/apache-proxy-fcgi/. I just needed the section labeled Ubuntu 14.04). I didn’t on 12.04. I also had to run `sudo a2enmod actions`.