Zend Framework 2 on a shared host

October 02, 2014

This topic is one I encountered a lot a few years ago, but one I found little discussion or examples for. Today I’m going to walk through how to get the Zend Framework 2 Skeleton Application up and running on a shared hosting provider, including hosting providers such as Hostgator or GoDaddy.

Prerequisites

  • Your host must allow you to use .htcaccess files in Apache. I’ve never seen a hoster that doesn’t allow this, but many have it disabled by default, and I certainly haven’t used more than a fraction of the hosters out there today. Contact your hosting provider if you aren’t sure.
  • You need to be to SSH into your server, or at least be allowed to run a few specific commands on your directory via terminal. This is a little more restrictive, with some hosters refusing to allow this.

Overview

Step 1. Copy your ZF2 application into your host Step 2. Run composer on your host Step 3. Configure your .htaccess file

Step 1. Copy your ZF2 application to your host

Next thing to do is connect to your hosting by whatever means they offer you. 99.9% offer FTP access to your storage, but if they offer you a different alternative, that wont cause any problems. Basically what we have to do copy everything into the root directory of our host. Your structure should look a bit like this:

Public_HTML/
    +Application/
    +Config/
    +Module/
    +Public/
    +Vendor/
    Init_Autoloader.php
    composer.json
    composer.phar

If it looks like the above once everything has uploaded, that’s great.

Now, you might not have Composer (composer.phar) in your directory. If you don’t you should go and grab it manually, and copy it over. The reason being that your host probably doesn’t allow you to run PEAR on your host, so you have to obtain composer manually. Also, if you are missing the Vendor folder, don’t worry, this is generated in the next step.

Step 2. Run composer on your host

Okay, so now we have our application on the host, but we don’t yet have ZF2 or it’s dependencies. It’s time to ssh into your server, or access terminal however your host allows/suggests. You need to navigate to the root, where composer.phar and composer.json are, and run this command: ./composer.phar install

Step 3. Configure your .htaccess file

We need to create a new file called .htaccess (unless it already exists). Once created we need to edit it, so open it in any text editor you like. It basically needs to look like this:

AddType application/x-httpd-php53 .php .php5
ReWriteEngine On
RewriteRule ^\.htaccess$[F]
RewriteCond %{REQUEST\_URI} =””
RewriteRule ^.\*$ /public/index.php [NC,L]
RewriteCond %{REQUEST\_URI} !^/public/.\*$
RewriteRule ^(.\*)$ /public/$1
RewriteCond %{REQUEST\_FILENAME} -f
RewriteRule ^.\*$[NC,L]
RewriteRule ^public/.\*$ /public/index.php [NC,L]

One thing to note, this is configured to work with hosting provider 123-reg. The first line: x-httpd-php53 may need to be changed depending on what version of PHP your host is running. You can find out from them. e.g. PHP 5.5 would change that line to x-httpd-php55.

Once added, save it and add upload it also to the root directory. Within a couple of minutes (normally instantly) visit your website, and you should see the skeleton/your application running on your shared hosting. Database access is left for you to configure, as connection settings are largely framework independent.