Advertisement
Sydcul

Untitled

Apr 21st, 2013
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.59 KB | None | 0 0
  1. Run PHP Applications under CGI with Apache on Debian 5 (Lenny)
  2. Published: Friday, December 18th, 2009 by Sam Kleinman
  3. In most cases, we recommend using mod_php module to run PHP scripts with the Apache HTTP server. This embeds a PHP interpreter in the Web Server process and makes running PHP applications easy. The embedded interpreter approach, however, is not without challenges; when the PHP interpreter is embedded in the web server process, PHP scripts are executed by and with the permissions of the web server's user. In smaller deployments, this is perfectly acceptable, but in larger deployments and operations it can create security risks. While Apache's itk message passing module (mpm) makes it possible to run Apache processes under user processes in a per-virtual host setup, this is incompatible with the embedded interpreter. The itk module is compatible with PHP running as a CGI process.
  4.  
  5. Additionally, in our experience mod_php is incompatible with the "mod_rails" or Phusion Passenger method of running Ruby On Rails. In these cases, if you want to run PHP and Rails applications within a single instance of Apache, you must run PHP scripts as CGI processes, using the method outlined below.
  6.  
  7. Before beginning this guide we assume that you've completed the getting started guide. If you are new to Linux server administration, we recommend considering the beginner's guide, and the article concerning systems administration basics. If you're interested in learning more about the Apache HTTP server, we encourage you to consider our extensive documentation on Apache configuration.
  8.  
  9. Contents
  10. Set the Hostname
  11. Installing Apache and PHP
  12. Configure Apache for PHP CGI
  13. Enabling the "itk" Message Passing Module
  14. More Information
  15. Set the Hostname
  16. Before you begin installing and configuring the components described in this guide, please make sure you've followed our instructions for setting your hostname. Issue the following commands to make sure it is set properly:
  17.  
  18. hostname
  19. hostname -f
  20. The first command should show your short hostname, and the second should show your fully qualified domain name (FQDN).
  21.  
  22. Installing Apache and PHP
  23. Make sure your package repositories and installed programs are up to date by issuing the following commands:
  24.  
  25. apt-get update
  26. apt-get upgrade --show-upgraded
  27. If you have not already installed the Apache HTTP server, issue the following command to install the packages for Apache:
  28.  
  29. apt-get install apache2
  30. You can now configure virtual hosting in accordance with the needs of your server. To install the PHP CGI binaries, issue the following command:
  31.  
  32. apt-get install php5-cgi
  33. When this process completes, we can configure Apache to hand PHP scripts to the CGI process for rendering these scripts.
  34.  
  35. Configure Apache for PHP CGI
  36. In order to set up Apache to use PHP-CGI on Debian systems, you must enable the mod_actions module. Issue the following command:
  37.  
  38. a2enmod actions
  39. The required directives can be set anywhere in Apache's configuration tree. We recommend creating the php-cgi.conf file in Apache's conf.d/ directory and setting these variables there. For Debian systems this directory is located at /etc/apache2/conf.d/. You may also choose to place these settings in your /etc/apache2/httpd.conf file. Regardless of their location, the relevant settings are:
  40.  
  41. File excerpt:Apache Configuration Block
  42.  
  43. ScriptAlias /local-bin /usr/bin
  44. AddHandler application/x-httpd-php5 php
  45. Action application/x-httpd-php5 /local-bin/php-cgi
  46. In this example, the path to the php-cgi binary is /usr/bin/php-cgi. All files with the php extension will be handed to the PHP CGI binary.
  47.  
  48. You may also choose to put these configuration directives within a virtual hosting block. If you do not have mod_php enabled or installed, you can use this to selectively enable PHP for certain virtual hosts. Furthermore, if your deployment requires multiple versions of PHP, you can specify virtual host specific handlers by specifying paths to various versions of php-cgi.
  49.  
  50. The configuration file for the CGI executable of PHP is located at /etc/php5/cgi/php.ini. You can modify this file to suit the needs of your deployment.
  51.  
  52. File excerpt:/etc/php5/cgi/php.ini
  53.  
  54. error_reporting = E_COMPILE_ERROR|E_RECOVERABLE_ERROR|E_ERROR|E_CORE_ERROR
  55. display_errors = Off
  56. log_errors = On
  57. error_log = /var/log/php.log
  58. max_execution_time = 30
  59. memory_limit = 64M
  60. register_globals = Off
  61. If you need support for MySQL in PHP, then you must install the php5-mysql package with the following command:
  62.  
  63. apt-get install php5-mysql
  64. When php-cgi is configured, you can now safely enable the itk message passing module for Apache. The installation process for itk will restart the Apache process. If you choose to use PHP CGI with the default or existing message passing module, then restart Apache by issuing the following command:
  65.  
  66. /etc/init.d/apache2 restart
  67. Enabling the "itk" Message Passing Module
  68. The default Apache configuration uses a message passing module called worker, which uses a threaded approach to efficiently handling HTTP requests. An alternative MPM is prefork which does not use threads and is compatible with non-tread-safe libraries. Both the worker and prefork modules, requires that all requests be handled by a process running under a user with particular permissions. On Debian systems, Apache processes run under the www-data user.
  69.  
  70. This may not be ideal if you have multiple users running publicly accessible scripts on your server. In some of these cases it is prudent to isolate virtual hosts under specific user accounts using an alternative message passing module, known as itk or mpm-itk. Functionally, mpm-itk is quite similar to prefork; however, itk can processes requests for each virtual host each site under a specified user account. This useful in situations where you're hosting a number of distinct sites that you need to isolate sites on the basis of user privileges.
  71.  
  72. Begin by installing the mpm-itk module:
  73.  
  74. apt-get install apache2-mpm-itk
  75. Now, in the <VirtualHost > entries for your sites (the site-specific files in /etc/apache2/sites-avalible/) add the following sub-block:
  76.  
  77. File excerpt:Apache Virtual Hosting Configuration Block
  78.  
  79. <IfModule mpm_itk_module>
  80. AssignUserId webeditor webgroup
  81. </IfModule>
  82. In this example, webeditor is the name of the user of the specific site in question, and webgroup is the name of the user group that "owns" the web server related files and processes for this host. Remember that you must create the user accounts and groups using the useradd command. Consider our documentation of user groups and permissions for more information about creating the necessary users and groups.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement