Difference between revisions of "Running PHP Scripts on the CS Webservers"

 
(18 intermediate revisions by 3 users not shown)
Line 1: Line 1:
<!--  '''''(The following applies starting after the morning of Tuesday, April 23.)'''''  -->
+
The multi-user nature of the CS webservers require us to run PHP in a slightly uncommon way.  We run PHP as a CGI using Apache's [https://httpd.apache.org/mod_fcgid/ mod_fcgid] [https://en.wikipedia.org/wiki/FastCGI FastCGI] module.  That means that some common ways to use PHP need to be done slightly differently in our environment.  These differences are documented below.
The multi-user nature of the CS '''www.cs.jhu.edu''' website requires that we run PHP in a slightly uncommon way.  We run PHP as a CGI using Apache's [https://httpd.apache.org/mod_fcgid/ mod_fcgid] [https://en.wikipedia.org/wiki/FastCGI FastCGI] module.  That means that some common ways to use PHP need to be done slightly differently in our environment.  These differences are documented below.
 
  
 
== Files in Your Home Directory ==
 
== Files in Your Home Directory ==
  
There is a ''new'' <tt>'''php-cgi'''</tt> script in your <tt>~'''/public_html/cgi-bin</tt>''' directory.  This is the script that runs the ''PHP CGI'' interpreter for you.  Apache is told about this script by the <tt>'''FcgidWrapper'''</tt> directive in your <tt>'''~/public_html/.htaccess'''</tt> file.
+
There is a <tt>'''php-cgi'''</tt> script in your <tt>~/public_html/cgi-bin</tt> directory.  This is the script that runs the ''PHP CGI'' interpreter for you.  Apache is told about this script by the <tt>'''FcgidWrapper'''</tt> directive in your <tt>'''~/public_html/.htaccess'''</tt> file.
  
 
''Changing either of these files should be done with care, as doing so could prevent PHP from working for your webpages.''
 
''Changing either of these files should be done with care, as doing so could prevent PHP from working for your webpages.''
 +
 +
== The phpinfo() Function ==
 +
 +
The <code>phpinfo()</code> function is disabled on our webservers.  It discloses a significant amount of information about our servers that people on the public Internet don't need.  If you have a question about availability of particular PHP modules, please feel free to {{mailto|support|email us}} and ask.
  
 
== Apache AddType Directives ==
 
== Apache AddType Directives ==
  
The CS '''www.cs.jhu.edu''' webserver treats any file that has a <tt>'''.php'''</tt> extension as a PHP script.  In some cases, you might want files with other extensions to also be treated as PHP scripts.  
+
The CS webservers treat any file that has a <tt>'''.php'''</tt> extension as a PHP script.  In some cases, you might want files with other extensions to also be treated as PHP scripts.  
  
 
Most documentation on the web describes how to use the [https://httpd.apache.org/docs/2.2/mod/mod_mime.html#AddType AddType] Apache directive in your <tt>'''.htaccess'''</tt> file for this, and that directive usually looks like this:
 
Most documentation on the web describes how to use the [https://httpd.apache.org/docs/2.2/mod/mod_mime.html#AddType AddType] Apache directive in your <tt>'''.htaccess'''</tt> file for this, and that directive usually looks like this:
  
<pre style="background-color: #cb4b16;">AddType application/x-httpd-php .html</pre>
+
<pre class="bad">AddType application/x-httpd-php .html</pre>
  
''However,'' for our http://www.cs.jhu.edu website, ''instead of AddType,'' you will need to use the [https://httpd.apache.org/docs/2.2/mod/mod_mime.html#addhandler AddHandler] directive to process the file as a FastCGI script and give the ''full path'' to your <tt>'''php-cgi'''</tt> script to run it.
+
''However,'' for our websites, ''instead of AddType,'' you will need to use the [https://httpd.apache.org/docs/2.2/mod/mod_mime.html#addhandler AddHandler] directive to process the file as a FastCGI script and give the ''full path'' to your <tt>'''php-cgi'''</tt> script to run it. For example, a ''grad'' account might use the following to execute HTML files as PHP scripts:
  
<pre style="background-color: #859900;">AddHandler fcgid-script .html
+
<pre class="good">AddHandler fcgid-script .html
 
FcgidWrapper /users/YOUR-ACCOUNT-HERE/public_html/cgi-bin/php-cgi .html</pre>
 
FcgidWrapper /users/YOUR-ACCOUNT-HERE/public_html/cgi-bin/php-cgi .html</pre>
 +
 +
A ''ugrad'' account might use the following to execute files with a "<code>.p</code>" extension as PHP scripts:
 +
 +
<pre class="good">AddHandler fcgid-script .p
 +
FcgidWrapper /home/YOUR-ACCOUNT-HERE/public_html/cgi-bin/php-cgi .p</pre>
  
 
== Apache ForceType Directives ==
 
== Apache ForceType Directives ==
Line 25: Line 33:
 
Another way to force a particular file or files to be treated as PHP is to use a [https://httpd.apache.org/docs/2.2/mod/core.html#files <Files>] or [https://httpd.apache.org/docs/2.2/mod/core.html#directory <Directory>] section in your <tt>'''.htaccess'''</tt> file to target those files specifically.  The customary documentation on the web uses the [https://httpd.apache.org/docs/2.2/mod/core.html#ForceType ForceType] directive for this:
 
Another way to force a particular file or files to be treated as PHP is to use a [https://httpd.apache.org/docs/2.2/mod/core.html#files <Files>] or [https://httpd.apache.org/docs/2.2/mod/core.html#directory <Directory>] section in your <tt>'''.htaccess'''</tt> file to target those files specifically.  The customary documentation on the web uses the [https://httpd.apache.org/docs/2.2/mod/core.html#ForceType ForceType] directive for this:
  
<pre style="background-color: #cb4b16;"><Files quux.foo>
+
<pre class="bad"><Files quux.foo>
 
   ForceType application/x-httpd-php
 
   ForceType application/x-httpd-php
 
</Files></pre>
 
</Files></pre>
  
For our webserver, you have to use the [https://httpd.apache.org/docs/2.0/mod/core.html#sethandler SetHandler] directive along with the ''full path'' to your <tt>'''php-cgi'''</tt> script:
+
''However,'' for our websites, ''instead of ForceType,'' you have to use the [https://httpd.apache.org/docs/2.0/mod/core.html#sethandler SetHandler] directive along with the ''full path'' to your <tt>'''php-cgi'''</tt> script.  For a ''grad'' example:
  
<pre style="background-color: #859900;"><Files quux.foo>
+
<pre class="good"><Files quux.foo>
 
   SetHandler fcgid-script
 
   SetHandler fcgid-script
 
   FcgidWrapper /users/YOUR-ACCOUNT-HERE/public_html/cgi-bin/php-cgi
 
   FcgidWrapper /users/YOUR-ACCOUNT-HERE/public_html/cgi-bin/php-cgi
 
</Files></pre>
 
</Files></pre>
  
 +
''ugrad'' accounts need to use a slightly different path:
 +
 +
<pre class="good"><Files quux.foo>
 +
  SetHandler fcgid-script
 +
  FcgidWrapper /home/YOUR-ACCOUNT-HERE/public_html/cgi-bin/php-cgi
 +
</Files></pre>
 +
 +
== If PHP Doesn't Work ==
 +
 +
First, check to make sure your <tt>~/public_html/.htaccess</tt> and <tt>~/public_html/cgi-bin/php-cgi</tt> files exist and have the right contents and permissions.
 +
 +
For ''grad'' accounts, <tt>~/public_html/.htaccess</tt> should contain the following line:
 +
 +
FcgidWrapper /users/''your-account''/public_html/cgi-bin/php-cgi .php
 +
 +
For ''ugrad'' accounts, <tt>~/public_html/.htaccess</tt> should contain the following line:
 +
 +
FcgidWrapper /home/''your-account''/public_html/cgi-bin/php-cgi .php
 +
 +
'''Make sure you replace "''your-account''" with your actual account name.'''
 +
 +
<tt>~/public_html/cgi-bin/php-cgi</tt> should have the following contents:
 +
 +
#!/bin/sh
 +
exec /usr/bin/php-cgi
 +
 +
[[File Permissions for Webpages|All of the files need to have specific permissions.]]  Run the following commands from a terminal window to set their permissions appropriately:
 +
 +
chmod 0701 ~
 +
chmod 0701 ~/public_html
 +
chmod 0604 ~/public_html/.htaccess
 +
chmod 0701 ~/public_html/cgi-bin
 +
chmod 0700 ~/public_html/cgi-bin/php-cgi
 +
 +
If you are still having problems, please email [mailto:support@cs.jhu.edu support@cs.jhu.edu] for assistance.
  
 
[[Category:Webpages_and_Webservices]]
 
[[Category:Webpages_and_Webservices]]

Latest revision as of 14:05, 7 July 2021

The multi-user nature of the CS webservers require us to run PHP in a slightly uncommon way. We run PHP as a CGI using Apache's mod_fcgid FastCGI module. That means that some common ways to use PHP need to be done slightly differently in our environment. These differences are documented below.

Files in Your Home Directory

There is a php-cgi script in your ~/public_html/cgi-bin directory. This is the script that runs the PHP CGI interpreter for you. Apache is told about this script by the FcgidWrapper directive in your ~/public_html/.htaccess file.

Changing either of these files should be done with care, as doing so could prevent PHP from working for your webpages.

The phpinfo() Function

The phpinfo() function is disabled on our webservers. It discloses a significant amount of information about our servers that people on the public Internet don't need. If you have a question about availability of particular PHP modules, please feel free to email us and ask.

Apache AddType Directives

The CS webservers treat any file that has a .php extension as a PHP script. In some cases, you might want files with other extensions to also be treated as PHP scripts.

Most documentation on the web describes how to use the AddType Apache directive in your .htaccess file for this, and that directive usually looks like this:

AddType application/x-httpd-php .html

However, for our websites, instead of AddType, you will need to use the AddHandler directive to process the file as a FastCGI script and give the full path to your php-cgi script to run it. For example, a grad account might use the following to execute HTML files as PHP scripts:

AddHandler fcgid-script .html
FcgidWrapper /users/YOUR-ACCOUNT-HERE/public_html/cgi-bin/php-cgi .html

A ugrad account might use the following to execute files with a ".p" extension as PHP scripts:

AddHandler fcgid-script .p
FcgidWrapper /home/YOUR-ACCOUNT-HERE/public_html/cgi-bin/php-cgi .p

Apache ForceType Directives

Another way to force a particular file or files to be treated as PHP is to use a <Files> or <Directory> section in your .htaccess file to target those files specifically. The customary documentation on the web uses the ForceType directive for this:

<Files quux.foo>
   ForceType application/x-httpd-php
</Files>

However, for our websites, instead of ForceType, you have to use the SetHandler directive along with the full path to your php-cgi script. For a grad example:

<Files quux.foo>
  SetHandler fcgid-script
  FcgidWrapper /users/YOUR-ACCOUNT-HERE/public_html/cgi-bin/php-cgi
</Files>

ugrad accounts need to use a slightly different path:

<Files quux.foo>
  SetHandler fcgid-script
  FcgidWrapper /home/YOUR-ACCOUNT-HERE/public_html/cgi-bin/php-cgi
</Files>

If PHP Doesn't Work

First, check to make sure your ~/public_html/.htaccess and ~/public_html/cgi-bin/php-cgi files exist and have the right contents and permissions.

For grad accounts, ~/public_html/.htaccess should contain the following line:

FcgidWrapper /users/your-account/public_html/cgi-bin/php-cgi .php

For ugrad accounts, ~/public_html/.htaccess should contain the following line:

FcgidWrapper /home/your-account/public_html/cgi-bin/php-cgi .php

Make sure you replace "your-account" with your actual account name.

~/public_html/cgi-bin/php-cgi should have the following contents:

#!/bin/sh
exec /usr/bin/php-cgi

All of the files need to have specific permissions. Run the following commands from a terminal window to set their permissions appropriately:

chmod 0701 ~
chmod 0701 ~/public_html
chmod 0604 ~/public_html/.htaccess
chmod 0701 ~/public_html/cgi-bin
chmod 0700 ~/public_html/cgi-bin/php-cgi

If you are still having problems, please email support@cs.jhu.edu for assistance.