{"id":1426,"date":"2014-09-22T14:13:28","date_gmt":"2014-09-22T18:13:28","guid":{"rendered":"http:\/\/mossiso.com\/?p=1426"},"modified":"2014-09-22T14:13:28","modified_gmt":"2014-09-22T18:13:28","slug":"setting-up-a-hosting-environment-part-5-apache-and-php","status":"publish","type":"post","link":"https:\/\/mossiso.com\/2014\/09\/22\/setting-up-a-hosting-environment-part-5-apache-and-php\/","title":{"rendered":"Setting up a Hosting Environment, Part 5: Apache and PHP"},"content":{"rendered":"
Figuring out the possibilities for Apache and PHP reminds me of a Dr. Seuss book, “Fox in Sox”. It’s a favorite of mine. I love reading it to the kids. In it, Mr. Fox tries to get Mr. Knox to say all kinds of ridiculous (in meaning and hard to say) tongue twisters. At one point Mr. Knox exclaims:
\n“I can’t blab such blibber blubber!
\nMy tongue isn’t make of rubber.”<\/p>\n
That’s what my brain felt like after trying to figure all of the options for Apache and PHP. To combat my rubber brain, I created this flow-chart to help me keep track of the options, the pros and cons for each, and the path I finally chose.<\/p>\n
First off, a list of requirements and goals:<\/p>\n
Here’s what I eventually figured out about Apache and PHP:<\/p>\n These sites were helpful for the initial set up of PHP as CGI with mod_fcgi and Apache in chroot (mod_fcgi sends one request to each PHP process regardless if PHP children are available to handle more, and no sharing of APC opcode cache across PHP processes):<\/p>\n This site was helpful for setting up PHP as CGI with mod_fastcgi and Apache in chroot (mod_fastcgi sends multiple requests to a PHP process, so the process can send them to children processes, and having one PHP process for each site allows for APC opcode cache to be usable.)<\/p>\n These sites helped me learn about php-fpm and how it is not quite ready for what I have in mind:<\/p>\n I ended up going with Apache’s mod_fastcgi for using PHP as a CGI, and NOT using PHP-FPM, while running Apache in threaded mode with apache.worker enabled.<\/p>\n To get this set up is pretty easy. I already had Apache and PHP installed and running (with PHP as CGI using mod_fcgi), so here are the steps I used to convert it to run mod_fastcgi and apache.worker. I’m running CentOS 6.3.<\/p>\n <\/p>\n Each virtual host needs to have an entry similar to this in the httpd.conf file, or I like to create a separate virtual_host.conf and include that in the main httpd.conf.<\/p>\n Things to note:<\/p>\n Add a Things to note:<\/p>\n Comment out everything in the Edit the Check the Apache configuration files to see if there are any errors.<\/p>\n If all good, restart Apache<\/p>\n Set up log rotation for Apache<\/p>\n Figuring out the possibilities for Apache and PHP reminds me of a Dr. Seuss book, “Fox in Sox”. It’s a favorite of mine. I love reading it to the kids. In it, Mr. Fox tries to get Mr. Knox to say all kinds of ridiculous (in meaning and hard to say) tongue twisters. At one … Continue reading Setting up a Hosting Environment, Part 5: Apache and PHP<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":1428,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[249,167,9,170],"tags":[20,251,254,28,252,253,69,250],"class_list":["post-1426","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-setting-up-a-hosting-environment","category-technical","category-technology","category-websites","tag-apache","tag-apache-worker","tag-apc","tag-centos","tag-mod_fastcti","tag-mod_fcgi","tag-php","tag-php-fpm"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/mossiso.com\/wp-content\/uploads\/2013\/02\/200px-FoxInSocksBookCover.jpg","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9wosP-n0","_links":{"self":[{"href":"https:\/\/mossiso.com\/wp-json\/wp\/v2\/posts\/1426"}],"collection":[{"href":"https:\/\/mossiso.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mossiso.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mossiso.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mossiso.com\/wp-json\/wp\/v2\/comments?post=1426"}],"version-history":[{"count":7,"href":"https:\/\/mossiso.com\/wp-json\/wp\/v2\/posts\/1426\/revisions"}],"predecessor-version":[{"id":1647,"href":"https:\/\/mossiso.com\/wp-json\/wp\/v2\/posts\/1426\/revisions\/1647"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/mossiso.com\/wp-json\/wp\/v2\/media\/1428"}],"wp:attachment":[{"href":"https:\/\/mossiso.com\/wp-json\/wp\/v2\/media?parent=1426"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mossiso.com\/wp-json\/wp\/v2\/categories?post=1426"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mossiso.com\/wp-json\/wp\/v2\/tags?post=1426"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}<\/a>
\n
\n
\n
Install the RPMForge repo for installing mod_fastcgi.<\/h4>\n
\n
rpm -Uvh http:\/\/pkgs.repoforge.org\/rpmforge-release\/rpmforge-release-0.5.2-2.el6.rf.x86_64.rpm<\/code><\/li>\n
yum --enablerepo=rpmforge install mod_fastcgi<\/code><\/li>\n<\/ul>\n
Edit the
\/etc\/httpd\/conf\/httpd.conf<\/code> file<\/h4>\n
\n
ServerTokens Prod<\/code><\/li>\n
KeepAlive On<\/code><\/li>\n
<IfModule worker.c>\r\n StartServers 8\r\n MaxClients 300\r\n MinSpareThreads 25\r\n MaxSpareThreads 75\r\n ThreadsPerChild 25\r\n MaxRequestsPerChild 0\r\n<\/IfModule><\/pre>\n<\/li>\n
LoadModule php5_module modules\/libphp5.so<\/code><\/li>\n
AddType application\/x-httpd-php .php<\/code><\/li>\n
Include conf\/virtual_hosts.conf<\/code><\/li>\n<\/ul>\n
Create a
\/etc\/httpd\/conf\/virtual_hosts.conf<\/code> file<\/h4>\n
# Name-based virtual hosts\r\n#\r\n\r\n# Default\r\nNameVirtualHost *:80\r\n\r\n# Begin domain-name.com section\r\n<VirtualHost *:80>\r\n DocumentRoot \/var\/domain-name\/home\/html\/\r\n ServerName domain-name.com\r\n ServerAlias www.domain-name.com\r\n\r\n # Rewrite domain name to not use the 'www'\r\n RewriteEngine On\r\n RewriteCond %{HTTP_HOST} !^domain-name\\.com$ [NC]\r\n RewriteRule ^\/(.*) http:\/\/domain-name.com\/$1 [R=301]\r\n\r\n # Specify where the error logs go for each domain\r\n ErrorLog \/var\/logs\/httpd\/current\/domain-name.com-error_log\r\n CustomLog \/var\/logs\/httpd\/current\/domain-name.com-access_log combined\r\n\r\n <IfModule mod_fastcgi.c>\r\n SuexecUserGroup domain-name domain-name\r\n ScriptAlias \/cgi-bin\/ \"\/var\/www\/cgi-bin\/domain-name\/\"\r\n <Directory \"\/var\/domain-name\/home\/html\">\r\n Options -Indexes FollowSymLinks +ExecCGI\r\n AddHandler php5-fastcgi .php\r\n Action php5-fastcgi \/cgi-bin\/php-fastcgi\r\n Order allow,deny\r\n Allow from all\r\n <\/Directory>\r\n <\/IfModule>\r\n<\/VirtualHost>\r\n# End domain-name.com section<\/pre>\n
\n
SuexecUserGroup<\/code> should have the user\/group for the project.<\/li>\n<\/ul>\n
Create the php-fastcgi file<\/h4>\n
\/var\/www\/cgi-bin\/projectname\/php-fastcgi<\/code> file for each project. This allows php to run as FastCGI, and use suEXEC. The php-fastcgi file needs to be under suexec\u2019s default directory path
\/var\/www\/cgi-bin\/<\/code>.<\/p>\n
\n
#!\/bin\/bash\r\n#\u00a0 Set PHPRC to the path for the php.ini file. Change this to\r\n#\u00a0 \/var\/projectname\/home\/ to let projects have their own php.ini file\r\nPHPRC=\/var\/domain-name\/home\/\r\nexport PHPRC\r\nexport PHP_FCGI_MAX_REQUESTS=5000\r\nexport PHP_FCGI_CHILDREN=5\r\nexec \/usr\/bin\/php-cgi<\/pre>\n<\/div>\n<\/li>\n<\/ul>\n
\n
\/var\/log\/httpd\/suexec.log<\/code><\/li>\n
\/var\/www\/cgi-bin\/projectname\/php-fastcgi<\/code> file:\n
\n
exec \/usr\/bin\/php-cgi -d apc.shm_size=128M<\/code><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n
Change php.conf<\/h4>\n
\/etc\/httpd\/conf.d\/php.conf<\/code> file so php is not loaded as a module when Apache starts.<\/p>\n
Apache multi-threaded<\/h4>\n
\/etc\/sysconfig\/httpd<\/code> file to allow Apache to use multi-threaded mode (httpd.worker) which handles basic HTML files much nicer (less RAM). Uncomment the line with
HTTPD=\/usr\/sbin\/httpd.worker<\/code><\/p>\n
Config Check<\/h4>\n
\n
service httpd configtest<\/code><\/li>\n<\/ul>\n
\n
service httpd restart<\/code> This will stop the running httpd service, and then start it again. Use this command after installing or removing a dynamically loaded module such as PHP. OR<\/li>\n
service httpd reload<\/code> This will cause the running httpd service to reload the configuration file. Note that any requests being currently processed will be interrupted, which may cause a client browser to display an error message or render a partial page. OR<\/li>\n
service httpd graceful<\/code> This will cause the running httpd service to reload the configuration file. Note that any requests being currently processed will use the old configuration.<\/li>\n<\/ul>\n
Install APC<\/h3>\n
\n
pecl install apc<\/code><\/li>\n<\/ul>\n
\n
\/etc\/logrotate.d\/httpd.monti<\/code><\/li>\n
\/var\/logs\/httpd\/*log {\r\n daily\r\n rotate 365\r\n compress\r\n missingok\r\n notifempty\r\n copytruncate\r\n olddir \/var\/logs\/httpd\/archives\/\r\n sharedscripts\r\n postrotate\r\n \/bin\/kill -HUP `cat \/var\/run\/httpd\/httpd.pid 2>\/dev\/null` 2> \/dev\/null || true\r\n endscript\r\n}<\/pre>\n<\/div>\n<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"