(dv) 3.5:Run PHP as FastCGI and resolve Apache permission errors
- This page was last modified on May 10, 2012, at 10:38.
From (mt) Community Wiki
End of Life Warning
This service has a pending End of Life. Please move to a current service as soon as possible. For more information, please see: (dv) Dedicated-Virtual 4.0: Migration Information.
Contents |
Overview
This article will explain how to configure a domain on your (dv) Dedicated-Virtual Server to run PHP as FastCGI. This can improve the speed of your web pages. For more information about FastCGI, please see their website:
http://www.fastcgi.com/drupal/
Another reason to run PHP as FastCGI is to allow PHP to run as your domain/FTP user rather than as the apache user. Because of the ownership that Plesk assigns to your website files and directories, Apache does not have permission to write to your web directories by default. This can cause permission errors in some applications - for example, a PHP upload script.
Results
Your domain will run PHP as FastCGI, with the user and group that Plesk uses for your domain site files.
Please note that (mt) Media Temple does not support changing your default Apache configuration. This server modification is not guaranteed to work with other configurations you might have. Please consider making a backup copy of your (dv) Dedicated-Virtual Server before proceeding, as (mt) Media Temple does not support the procedures covered in this walkthrough.
Requirements
Before you start:
- Please replace example.com in the examples with your own domain, and domainuser with your own domain/FTP user that you set up in Plesk.
- Root access enabled. For more information, please see: How do I enable root access to my (dv)?
- Be able to connect to your (dv) via SSH. Details are here: Connecting via SSH to your server.
Instructions
Method 1: Using the Automated PHP as FastCGI Script
Please see this article for instructions: (dv):Using the Automated PHP as FastCGI Script
Method 2: Via SSH
- Log into Plesk and enable CGI/FastCGI support for your domain. Go to the Domains tab, click on your domain, then on Setup. Make sure PHP support, CGI support, and FastCGI support are all selected. Click OK to save your changes.
- Log into your server with a root or sudo user via SSH.
- Make a local copy of the PHP CGI binary program for your domain:
cp /usr/bin/php-cgi /var/www/vhosts/example.com/bin/ - Change the ownership of the bin directory and your new local copy of PHP:
chown -R domainuser:psacln /var/www/vhosts/example.com/bin/ - Modify or create your local Apache configuration file, vhost.conf:
vim /var/www/vhosts/example.com/conf/vhost.conf - Add the following lines to the file:
vhost.conf
AddHandler fcgid-script .php SuexecUserGroup domainuser psacln <Directory /var/www/vhosts/example.com/httpdocs> FCGIWrapper /var/www/vhosts/example.com/bin/php-cgi .php Options +ExecCGI +FollowSymLinks allow from all </Directory> - Reload your Apache configuration settings:
/usr/local/psa/admin/sbin/websrvmng -av
- Restart Apache:
/etc/init.d/httpd graceful
PHP will now be running as FastCGI as the same user and group that owns your website files.
-
Adjust your sessions directory:
Since PHP will now be running as the script's user rather than apache, the permissions for the session folder need adjusted to account for this.
chmod 777 /var/lib/php/session
-
Adjust permissions:
If you have already modified permissions, or your site has been running for a while on your (dv) Dedicated-Virtual Server, you may need to correct file ownership and permissions. You can use the following commands to reset permissions and ownership to a "standard" state. Please note that some specific exceptions may exist, and you may need to manually tweak permissions on a few folders or files. Most CMS software will warn you about security risks related to permissions in the administrative panel somewhere. You probably don't need to run these commands if you're configuring a new domain or have just installed your CMS.
cd /var/www/vhosts/example.com/httpdocs && chown -R domainuser:psacln * && find . -type f -exec chmod 644 {} \; && find . -type d -exec chmod 755 {} \; - You should consider adjusting the number of simultaneous FastCGI processes allowed for each domain and for the server overall, based on the number of domains that you have running FastCGI. The default configuration allows 64 total processes and 8 per domain. Edit your configuration file:
vim /etc/httpd/conf.d/fcgid.conf
Update the following variables, if desired:
fcgid.confMaxProcessCount 64 DefaultMaxClassProcessCount 8
You should set the DefaultMaxClassProcessCount to the number of processes you want a single domain to be able to run simultaneously. Multiply that number by the number of domains that are running FastCGI, and use that number for the MaxProcessCount. For example, if you have 4 domains using FastCGI, and you want them to run a maximum of 10 simultaneous processes each, you can set the following values:
fcgid.confMaxProcessCount 40 DefaultMaxClassProcessCount 10
Do not set these values arbitrarily high, as this may interfere with your server's memory usage. Alternately, you can pick a server maximum first for the MaxProcessCount, and then divide by the number of your domains to set the DefaultMaxClassProcessCount value.
Testing and troubleshooting
You should be able to see the Server API setting in your PHP Info page set to "CGI/FastCGI," if you have been successful. To make a PHP Info page:
<?php // Show all information, defaults to INFO_ALL phpinfo(); ?>
If you have a setting in your .htaccess file that overrides the handling for files with a .php extension, you will need to remove or comment out this line. This is likely to be the case if you previously migrated from the (gs) Grid-Service and were using PHP 5. The line to be removed will start with "AddHandler" and provide a setting for .php files.
Contributions
Thanks to member "Buddy Chris" on our forums for providing a very good forum walkthrough on this topic. If you have further questions about running PHP as FastCGI on your (dv) Dedicated-Virtual Server, you should ask the (mt) Media Temple community on our forums.
Join the (mt) Media Temple User Forums.