Setting up a Laravel application on a Dreamhost shared hosting account

December 8, 2014

Like any other kind of website, there are tons of ways to host a Laravel application. This post outlines and provides a tutorial for getting a Laravel application up and running quickly on a Dreamhost shared hosting account.

I like Dreamhost for shared hosting because they have provided me with a fast, stable and reliable service for over 6 years and they maintain an impeccable level of customer support-- moreso than any other company that I have dealt with. They also give you hosting for an unlimited number of domains, give you unlimited data storage, provide reasonably priced domain registrations with free whois anonymity, and have an admin panel with a user interface that in my mind is even easier to use than the standard shared hosting cpanel.

This post does not cover dedicated or VPS hosting solutions (although most of it does also apply to Dreamhost's VPS hosting). For dedicated hosting I use Digital Ocean and Laravel Forge-- also both incredible services. In a later post I will go over how to use these services to host a Laravel application.

1. Signing up for Dreamhost

If you already have a Dreamhost account, you may skip ahead to the next step. If not, then you can sign up here. The signup process is pretty straightforward. You will first land on a screen like this:

Dreamhost Landing Page

Click "Get Started" and follow the steps to create an account, choose a domain (accounts come with a free domain registration) and payment plan, and enter payment details.

When you're done signing up, log in to the Dreamhost panel with the account you just created.

2. Setting up hosting for your domain

Once you're signed in to your Dreamhost panel, the first thing to do is make sure you have a user account set up to log in over SSH. On the left panel select "Users" and "Manage Users", and on the screen that comes up click on "Add A New User".

Dreamhost User Setup

If you already have a user set up click on "Edit" next to his name to make sure he is granted shell access. You will want to make sure the "Shell user" option is selected. Make sure you remember the password you set up on this screen. You'll use it later to connect to your shell account.

Dreamhost Add Shell User

Once a user is set up, you may add hosting for your domain or a subdomain. On the left panel select "Domains" and "Manage Domains", and on the screen that comes up click on "Add Hosting to a Domain / Sub-Domain".

Dreamhost Manage Domains

There are a few important details to take note of on this screen. Input your full domain or subdomain to host, and then select the user that you created earlier. For Laravel to run it is critical that you set "PHP Mode" to one of the PHP 5.4.x options. Once everything is filled out, click "Fully host this domain". Dreamhost will take a few minutes to get things set up, which we'll come back to in a minute.

3. Initializing Laravel project and checking into Git

I'll assume that you already have a Laravel 4 project running on your local development machine. If not, there are some good instructions on the Laravel site to help get you started. I'll also assume that you're going to check your project in to Git. If you installed Laravel with composer, we'll just need to make one minor adjustment to the .gitignore file.

Running a composer update or a composer install without a lock file sometimes causes your shared hosting account to run out of memory. To get around this you'll just need to commit your composer.lock file to git.

In the project root folder, open .gitignore in your favorite editor. Simply remove the line that says composer.lock

/bootstrap/compiled.php
/vendor
composer.phar
composer.lock   <-- REMOVE THIS LINE
.env.*.php
.env.php
.DS_Store
Thumbs.db

Finally, just add, commit, and push all files up to your git repository, and you're ready to set up the project on your shared host.

4. Checking out Laravel project on shared host and running composer

The next step is to log in to your Dreamhost shell account and pull down the Laravel project from git.

In step 2 you set up a user in the Dreamhost panel. Go back there now and navigate to the "Manage Users" screen.

Dreamhost manage users

Make a note of the "User" and "Machine" names and use these to ssh to your shared host.

ssh [YOUR USERNAME]@[YOUR MACHINE NAME].dreamhost.com

If you're using Windows and don't have ssh on your command line, you can connect with PuTTY.

Enter your password that you set up in step 2. You should be welcomed on to your Dreamhost server. If you get an error, then check the password you used on the "Manage Users" screen from step 2.

As of this writing, Dreamhost has made PHP 5.4 the default executable for the CLI version of PHP on their servers. However, you may want to check this to be sure.

$ php -v
PHP 5.4.20 (cli) (built: Nov 13 2013 20:44:52)
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies

Hopefully you see 5.4.x. If not, then simply substitute php-5.4 everywhere you would use php on the command line.

There is one tweak to getting php to function properly on dreamhost, and that is telling it where to find the phar extension. Look for a directory on your server called ~/.php/5.4. If it doesn't exist, then create it.

$ mkdir ~/.php/5.4

Then just add one line to phprc.

$ echo "extension = /usr/local/php54/lib/php/extensions/no-debug-non-zts-20100525/phar.so" >> ~/.php/5.4/phprc

Clone your Laravel Git repository:

$ cd
$ git clone [YOUR GIT REPO URL]

Assuming your git repository is called my-git-repository, install composer with:

$ cd my-git-repository
$ curl -sS https://getcomposer.org/installer | php
or
$ curl -sS https://getcomposer.org/installer | php-5.4
(both should work)

Pull in all of the composer dependencies:

$ php composer.phar install
or
$ php-5.4 composer.phar install

5. Linking Laravel project to public web directory

By this time, Dreamhost should have finished setting up your domain or subdomain. Assuming your domain is called my.domain.com, you should see it in your home directory on your shared host:

$ cd 
$ ls
my.domain.com

If you were using this domain for something else already and want to host your Laravel project in a subfolder, then all you need to do here is add a symbolic link to the subfolder name:

$ cd
$ ln -s /home/[YOUR USERAME]/my-git-repository/public my.domain.com/[SUBFOLDER NAME]

However, if this domain is currently empty, you can move the Dreamhost-created folder aside and recreate it as a symbolic link:

$ cd
$ mv my.domain.com my.domain.com.bak
$ ln -s /home/[YOUR USERNAME]/my-git-repository/public my.domain.com

That's it! Your app is all set up. You should be able to visit it at http://my.domain.com, but you'll see an error since your database isn't set up yet.

6. Setting up your database and running migrations

Next we need to set up your database connection parameters. The best way to manage sensitive properties that your application needs without storing them in a git repository is to set up environment variables. Laravel Forge lets you do this easily with your dedicated server. Since we're using shared hosting this isn't an option.

My preferred option is to create an application environment for Dreamhost, and manually copy the database.php file to your shared server as a one-time operation.

Back on your local development machine, take the Dreamhost host machine name (the one you just ssh'd to, ie. [YOUR MACHINE NAME], leaving off the .dreamhost.com), create a new Laravel application environment for dreamhost. In your Laravel project, open up bootstrap/start.php. Find the Application Environments section and add a new environment:

$env = $app->detectEnvironment(array(

    'local' => array('homestead'),
    'dreamhost' => array('[YOUR DREAMHOST MACHINE NAME HERE]'),

));

Create a folder called app/config/dreamhost and copy app/config/database.php into it.

$ mkdir app/config/dreamhost
$ cp app/config/database.php app/config/dreamhost/

In addition, create a .gitignore file in this folder to prevent your database config from being added to git.

$ echo '*' > app/config/dreamhost/.gitignore
$ echo '!.gitignore' >> app/config/dreamhost/.gitignore 

Add, commit, and push these changes to git. Log in to your shared host and pull these changes down.

$ ssh [YOUR USERNAME]@[YOUR MACHINE NAME].dreamhost.com
$ cd [LARAVEL PROJECT FOLDER NAME]
$ git pull
You'll likely need to enter your git password here
$ exit

You now need to go back to your Dreamhost panel to set up a MySQL database. On the left panel select "Goodies" and "MySQL Databases", and on the screen that comes up click on "Add New Hostname".

Dreamhost MySQL Setup

The hostname can be anything. Just remember this for later.

Once the MySQL hostname has been created, scroll to the bottom of this page and find the "Create a new MySQL database" section.

Dreamhost MySQL Details

There are no showstoppers here. Just keep track of the username, password, and database name that you create here so you can add them to your Laravel config. Click "Add new database now!".

Back on your local development machine, edit the database.php file that you put into your app/config/dreamhost folder. Just look for the mysql definition, and add the parameters that you just set up in the Dreamhost panel.

'mysql' => array(
    'driver'    => 'mysql',
    'host'      => '[YOUR DREAMHOST MYSQL HOSTNAME]',  // IMPORTANT - THIS CANNOT BE 'localhost'
    'database'  => '[YOUR DREAMHOST MYSQL DATABASE NAME]',
    'username'  => '[YOUR DREAMHOST MYSQL DATABASE USER]',
    'password'  => '[YOUR DREAMHOST MYSQL PASSWORD]',
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix'    => '',
),

The .gitignore that you created earlier should prevent you from checking this file into git. Just copy it up to your shared host:

$ cd [LARAVEL PROJECT ROOT FOLDER]
$ scp app/config/dreamhost/database.php [YOUR USERNAME]@[YOUR MACHINE NAME].dreamhost.com:~/[LARAVEL PROJECT FOLDER NAME]/app/config/dreamhost/database.php

Once again, log in to your shared host. Your database config should be in place and you can now run your Laravel migrations.

$ ssh [YOUR USERNAME]@[YOUR MACHINE NAME].dreamhost.com
$ cd [LARAVEL PROJECT FOLDER NAME]
$ php artisan migrate
or 
$ php-5.4 artisan migrate

Hopefully, you'll see a successful migration message, and you can now visit your working app at http://my.domain.com!

Conclusion

Running a Laravel application on a shared hosting provider such as Dreamhost is a good way to get a site live without a high level of cost. If you already have a Dreamhost account and domain, you can use one of your unlimited number of subdomains to host a new application for just about anything. This is good for prototyping, validating an idea, or just giving a client or your company really cheap hosting with your unlimited domains/subdomains. If you end up deploying to multiple domains/subdomains, you don't have to create new users or new mysql hosts each time unless you really want to.

If you are thinking about signing up for Dreamhost, you can use my dreamhost coupon or click any of the other Dreamhost promo code links on this page. You will get a $90 discount which gives you hosting for $2.45/month.

If you're planning on hosting a larger site or need dedicated hosting for any reason, stay tuned for a future post on deploying a Laravel application with Forge and Digital Ocean.

 

comments powered by Disqus

About This Site

This site was designed by We Are How.

This site is powered by Sculpin static site generator and the source is available here.

Yotta = 10^24, or 1 000 000 000 000 000 000 000 000, the largest metric prefix.


Contact

Get in touch to find out how we can help you refine your vision and implement a dynamite product that will help your business grow. Our agile product development process is thoughtfully designed to give clients ongoing feedback and visibility from project inception to completion.