Scripting and Development Guide

From Site5Wiki

Jump to: navigation, search

Contents

PHP

The php scripting language is available on all Site5 hosting accounts. You do not need to do anything specific to your account. Just name your script file with a .php extension and it will be processed as a php script. php4 is the default used at Site5.

Using a custom php.ini file

There are many reasons why you may want to use a custom php.ini file. The php.ini file is used to alter the php default parameters. There is a robust Site5 Forum discussion on this topic here:

http://forums.site5.com/showthread.php?t=3689

Using php5

php4 is the default at Site5. If you want to use php5 you can do so using either one of two methods:

  1. name your scripts .php5
  2. put this line in the htaccess file in public_html, then all php scripts on the site will use php5:
   AddHandler application/x-httpd-php5 .php

To avoid receiving a "No input file specified" error, you will need to add the following line to your public_html directory's .htaccess file:

   RewriteEngine On
   Rewritecond %{REQUEST_FILENAME} !-f
   RewriteRule \.php$ /error404.html

Parsing html files as php

If you want to parse html files through php you can add this line to your htaccess file:

Important Note: Having your web site configured to parse regular .html documents as PHP documents will result in some degradation to performance. The parser will be called regardless of if there is PHP within the document and thus it is strongly recommended that you avoid this type of configuration if at all possible.

 AddHandler application/x-httpd-php .php .html

If you want to use php5 then it would be:

 AddHandler application/x-httpd-php5 .php .html

Embedding PHP Scripts Into HTML

The easiest way to handle this would be to convert your SSI include calls to PHP, that way you avoid the need to double-parse each page served. Although includes through SSI are easy, they are just as easy in PHP. Simply convert them to:

 include("/path/to/file.html");

That will also allow you to put PHP code in the included file as well, which could be handy.

If you really need both, you can write a PHP wrapper script to call load the file through the web server (so you get the server parsing) and then run it through PHP using eval(). The code would look like this:

 $code = file_get_contents("http://yoursite.com/some_file.html"); eval('?>' . $code);

Just put that PHP code in a file like some_file.php, and have your visitors go to some_file.php. The page they see will have been parsed twice.

Originally found in this forum thread.

PHP code snippets

MySQL

The MySQL Database is available on all Site5 hosting accounts. You do not need to do anything specific to your account.

Setting up a MySQL Database

There are three steps to setting up a MySQL database.

  1. SiteAdmin > MySQL databases > manage databases, you must create the database.
  2. SiteAdmin > MySQL databases > manage databases, you must create a user.
  3. SiteAdmin > MySQL databases > manage databases, you must grant permission for the user to access the database.

What is my server name?

Typically a script configuration will require you to specify the MySQL server. The server should be specified as "localhost" when configuring scripts that run on your account, as the MySQL database runs on the same server where your files are served.

If you will be accessing the database remotely (I.E. through a client on your local computer or from an application hosted on a different Site5 account or third-party hosting service), you need to use your domain name as the server name.

Ruby on Rails

Current Versions

All Site5 servers currently have the below versions of Ruby and Rails installed.

  • Ruby: v1.8.4
  • Rails: v1.1.6

The following gems are also installed by default (with current versions installed----Carla 13:58, 20 February 2007 (EST))

  • actionmailer (1.1.5, 1.1.3, 1.0.0): Service layer for easy email delivery and testing.
  • actionpack (1.11.2, 1.11.0, 1.9.0): Web-flow and rendering framework putting the VC in MVC.
  • actionwebservice (1.0.0, 0.9.3, 0.8.0): Web service support for Action Pack.
  • activerecord (1.13.2, 1.13.0, 1.11.0): Implements the ActiveRecord pattern for ORM.
  • activesupport (1.2.5, 1.2.3, 1.1.0): Support and utility classes used by the Rails framework.
  • ferret (0.11.0-rc1): Powerful search capabilities.
  • mysql (2.7): MySQL/Ruby provides the same functions for Ruby programs that the MySQL C API provides for C programs.
  • Rails (1.0.0, 0.14.3, 0.13.0): Web-application framework with template engine, control-flow layer, and ORM.
  • rake (0.7.0, 0.6.2, 0.5.4): Ruby based make-like utility.
  • sources (0.0.1): Provides download sources for remote gem installation

Additional versions of Rails and gems may be installed on the server depending on the requests of the user base hosted there. However, if you require another version of Rails or gems that are not listed above we recommend that you install it locally on your account so that your applications are not effected by problems caused by any changes made to the globally installed Rails system on the server.

Configure your Rails Application for Production Mode with FastCGI

So you've provisioned your application and you're ready to go live. Simply follow the steps outlined below and you'll be up and running in Production mode using FastCGI in no time at all!

    1. Make sure you production database is properly configured ( and of course populated ) by reviewing the settings in your "APPNAME/config/database.yml".
    2. Un-comment the "#ENV['RAILS_ENV'] ||= 'production'" line within your "APPNAME/config/environment.rb".
    3. Within your "APPNAME/public/.htaccess" modify the following line:
"RewriteRule ^(.*)$ dispatch.cgi [QSA,L]"
To:
"RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]"


That's it! You're done! Fire up your application by visiting the URL you've previously configured it to function under.

Installing Ruby Gems

If your application requires extra ruby gems installed we would recommend that you install them locally to your account rather then request that customer service install them globally for you. Installing them locally will allow you more freedom to modify the gems as well as install gems that are not located in the public gem repository. Site5 administrators will be unable to install any gems which are not located in the public gem repository for security reasons.

A member of our forums, dusty.doris, has compiled a wonderful Howto Install your own Gems for Rails Guide.

You may also want to check out The New user's guide to freezing a Ruby on Rails application which was created by another member of our forums by the name of davesailor. This guide describes how to freeze your application along with the gems that it uses to ensure that it remains in a working state.

Testing Before Changing DNS — Ruby and /~username

Adam Greenfield notes in this forums post that to you need to use a rewritebase of /~username:

RewriteEngine on
RewriteBase /~username

Replace username with the correct one for your account.

Example of How to Install a Basic RoR Application

For this example, we'll be installing Solunas 2 in order to demonstrate the installation of an application that has a fairly basic setup from downloading to a working installation.

  • This example assumes Solunas 2 (RoR) is already downloaded to your local computer. The release/filename for this tutorial is solunas2.0.1.zip, however that may change in the future.
  • For this example, the user name will be "myuser" without the quotes. When applying this to your own account, this would change to the same username as used for your primary FTP username.


First, create a directory in your account for this application. This should NOT be under public_html, for this example we will create the "apps" directory under document root:

 mkdir ~/apps


# Upload the solunas2.0.1.zip file to the above location. Use any method you like.


Now, change to the above directory and unarchive it:

 cd ~/apps
 unzip solunas2.0.1.zip


This will create the sub-directory "solunas" which is part of the archive, change to the new directory.

 cd solunas


Now, with any new rails application it is good practice to ensure the correct permissions on files and directories. This can be done multiple ways, however the below should work for the majority of rails applications. This should be done from the root of the application's directory, and since we're already in that directory, lets proceed:

 find . -type d -exec chmod 755 {} \;
 find . -type f -exec chmod 644 {} \;
 chmod -R 755 script
 chmod 755 public/dispatch.*


Now, since we want to run in *FastCGI, lets modify the .htaccess file to do this. First we'll also change to the "public" directory, noting that in this example we'll use vi, however you can use any editor you feel like:

 cd public
 vi .htaccess


Image:Warning.png This tutorial assumes familiarity with VI, please use any editor you are familiar with.

Change the line

 "RewriteRule ^(.*)$ dispatch.cgi [QSA,L]"

to

 "RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]"

save the file, and and quit.


Note that the only change above was simply changing "dispatch.cgi" to "dispatch.fcgi" in the mentioned line.

Now, depending on your application you may already know which rails version is required for an application to run -- if you're developing your own, it would be the same version you're developing under. Since we don't know what version is required for Solunas 2, running the dispatch.fcgi script will let us know:

 ./dispatch.fcgi


The above will output:

 "Cannot find gem for Rails =1.1.6: Install the missing gem with 'gem install -v=1.1.6 rails',
 or change environment.rb to define RAILS_GEM_VERSION with your desired version."


Since Rails version 1.1.6 appears required, freeze it to the application's vendor directory. This should be done even if the version of rails is available on the server, to ensure future compatibility and to avoid any potential issues with server changes.

Change back to the applications root directory, which should be one level up:

 cd ..


Run svn export to download the needed version of rails and place it in vendor/rails.

 svn export http://dev.rubyonrails.org/svn/rails/tags/rel_1-1-6 vendor/rails


Now, the basics for any ruby application have been completed. Lets continue with the actual Solunas setup instructions provided on their website.

Create DB and Mysql user using your SiteAdmin or cPanel control panel (depending on account type), assign the newly created Mysql user to the newly created database giving it "all" permissions. Take note of the username, password, and database name you create, this will be needed later.


Now, moving back to the shell/terminal. Lets edit the database.yml file to setup the new database login details.

 vi config/database.yml

#Enter appropriate details per the above, save, and quit. You should enter the details in both production and development in the configuration file, but NOT under the "test" section.


Now, we'll import the default data that Solunas provided for the database.

 mysql --user=myuser_USER --pass=MYPASS myuser_MYDB < db/solunas.sql 

(again, replace appropriate details with created details earlier)


Now, the final step is to create a symlink under public_html to be able to view the application from the web.

 ln -s ~/apps/solunas/public ~/public_html/solunas


The installation is complete at this point, visit your new installation of Solunas 2:

http://mydomain.com/solunas/

More Information

More programming and installation tips, tricks, and instruction may be found on this wiki's Ruby page.

Using Subversion

Site5 customer and forum member patram1121 was nice enough to document the process of setting up and using Subversion on a Site5 account. Rather than copying and pasting his tutorial here, we figured we'd just send you directly to the source (which is a thread at the Site5 forums):

http://forums.site5.com/showthread.php?t=11008

The great thing is that if you have problems or questions, you can post a reply and patram1121 will receive notification of such and possibly be kind enough to help you out.

Subversion with Rails and Capistrano (formerly Switchtower)

If you're going to be hitting the Rails development train at full speed, you'll probably want all the cool toys that make using Rails such a joy. Site5 forum user jbnorthrup has documented the process marvelously in the following forum thread:

http://forums.site5.com/showthread.php?t=7348

The thread has grown quite a bit since the initial post, but include lots of additional troubleshooting info and even instructions on how to get Subclipse (an extension of the Eclipse IDE that uses Subversion) working with your new setup.

When it comes to Capistrano on Site5, forum user aficionado did his research and wrote the authoritative source on the topic: http://www.thebitguru.com/articles/9

Note: The latest versions of Capistrano use a set of permissions that do not work well with the SuExec environment, under which FastCGI runs. You can put this in your deploy.rb file to prevent Capistrano from changing the permissions of your files and directories:

task :set_permissions do
donothing = true
end


Image:Tag_red.png Related wiki pages: MySQL. PHP. Ruby, Ruby on Rails, Rails, Gems, Ferret, ERuby, PEAR, .htaccess

Personal tools