DEV Community

Robert Wayne
Robert Wayne

Posted on

SOP for Creating, Deploying & Managing (Laravel) Websites on Bluehost Shared

๐Ÿ“Ž I created this for my own standard operating procedure. Mainly, so I don't forget how to create and launch new sites! It was suggested that I share it with the community. That said, I'm not an expert. Your results may vary. And please I am always open to feedback for improving the process.

My Method/Workflow

I have a git repository folder on my production server, Bluehost. My account is a shared account, Pro Web Hosting. I create a repository there and also a live public folder for new sites.

Typical workflow... start making a site locally โ†’ push updates to the repository โ†’ shh into the live folder and pull the site in from the repository. I was originally inspired by this guide.

Creating

You must have ssh access enabled on your Bluehost account. Use your Bluehost ip address and username... your_usernam@50.88.888.88

My Git Workflow

Use this method if you already started building the site locally.

1. You already started developing your Laravel App.

You have committed a git project locally.

2. Initialize a -bare repository

SSH into Bluehost "production server" and locate the directory you would like your central repository. I have a folder called Repo

ssh your_usernam@50.88.888.88
cd repo
Enter fullscreen mode Exit fullscreen mode

Initialize a new repository.

git init --bare --shared sitename.git
Enter fullscreen mode Exit fullscreen mode

Note this is outside of your public folder structure.

3. Add remote to your local project

Go to local website folder (on your computer).

cd ~/"Path/to/websites/sitename"
Enter fullscreen mode Exit fullscreen mode

Add the repo as origin

git remote add origin your_usernam@50.88.888.88:~/repo/sitename.git
Enter fullscreen mode Exit fullscreen mode

Force push the site and set origin as upstream

git push -f --set-upstream origin master
Enter fullscreen mode Exit fullscreen mode

There's probably a better way but this seems to work great for this use case.

Alternate Git workflow when starting from the repo

Use this method if you don't already have a dev site locally.

1. Initialize your -bare repository

SSH into Bluehost "production server" and locate the directory you would like your central repository. I have a folder called Repo

ssh your_usernam@50.88.888.88
cd repo
Enter fullscreen mode Exit fullscreen mode

Now initialize your repository.

git init --bare --shared sitename.git
Enter fullscreen mode Exit fullscreen mode

Note this is outside of your public folder structure.

2. Clone to create local website

Go to local website files.

cd ~/"Path/to/websites"
Enter fullscreen mode Exit fullscreen mode

Now clone the central --bare repository you just created and logout of this SSH session.

git clone your_usernam@50.88.888.88:~/repo/sitename.git sitename
Enter fullscreen mode Exit fullscreen mode

3 .Start developing your Laravel App.

Celebrate and start making something.

Deploying

1. Clone from repo to create live website

(if you are not already logged into bluehost via ssh)

ssh your_usernam@50.88.888.88
Enter fullscreen mode Exit fullscreen mode

Go to live website files

cd ~/public_html/
Enter fullscreen mode Exit fullscreen mode

Now clone the central repository for the site

git clone your_usernam@50.88.888.88:~/repo/sitename.git sitename
Enter fullscreen mode Exit fullscreen mode

2. Bluehost admin tasks

From the Bluehost admin website we will create a database for the project, assign a domain, and add emails.

As an aside, I did read its possible to ssh a database into Bluehost but haven't taken the time to see if/how it can be automated when making a Laravel project.

a. Assign a domain OR create subdomain

  • ASSIGN A DOMAIN
    From the Domains sub menu choose Assign. Follow directions to assign your domain to Bluehost. When you get to Step 4: Choose Addon Directory and Sub-domain select Use an existing directory and choose the /sitename/public/ from the populated dropdown.

    OR

  • CREATE A SUBDOMAIN
    From the Domains sub menu choose subdomains. Make the Home folder is /sitename/public/

b. Create any site related Email Addresses from the cPanel

The cPanel has been moved to the "Advanced" menu tab.

If you plan to send site notifications using Bluehost smtp you must setup an email for it using the site domain or use an email already related to your account.

c. Create Database via cPanel

  1. cpanel -> MySQL Databases โ†’ Create New Database
  2. After created go back to the Databases page and add yourself as a user to the new database with all privileges.

3. Edit the site .env file

Using ssh go to live website files

cd ~/public_html/sitename/
Enter fullscreen mode Exit fullscreen mode

Rename the example env file

mv .env.example .env
Enter fullscreen mode Exit fullscreen mode

Edit the .env file adding relevant production values

vim .env
Enter fullscreen mode Exit fullscreen mode

Quick Reminder Crib Sheet for VIM: i to edit (insert), esc to stop editing, :wq to write and quit, OR :q to quit without saving

Add the new database credentials and app into to .env

APP_NAME=Sitename
APP_ENV=production
APP_KEY=
APP_DEBUG=true
APP_URL=https://sitename.com

...

DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=your_username_sitename
DB_USERNAME=your_username
DB_PASSWORD=your_blue_password

...

MAIL_MAILER=smtp
MAIL_HOST=mail.sitename.com
MAIL_PORT=465
MAIL_USERNAME=notifications@sitename.com
MAIL_PASSWORD=thEemailPaSsowrd
MAIL_ENCRYPTION=ssl
MAIL_FROM_ADDRESS=notifications@sitename.com
MAIL_FROM_NAME="${APP_NAME}"

...
Enter fullscreen mode Exit fullscreen mode

๐Ÿ’ก Remember: Bluehost databases are automatically prefixed with your_username_

โ›” Come back and set to app_debug to false once you confirm the site works.

๐Ÿ“จ For emails to work via Bluehost smtp it may require a chat with support for them to edit your site permissions for emails using their tool putty. If you get a swift error when the site is trying to sending emails. Chat up support!

4. Composer Install and such...

Here we use a modified version of Laravel's, composer install. (check with Bluehost for your correct version/path) This may just be my forcing php 8, idk.

/usr/local/bin/ea-php80 /opt/cpanel/composer/bin/composer install --optimize-autoloader --no-dev
Enter fullscreen mode Exit fullscreen mode

OR if you have dev dependency you haven't sorted out yet

/usr/local/bin/ea-php80 /opt/cpanel/composer/bin/composer install
Enter fullscreen mode Exit fullscreen mode

Generate the site key.

php artisan key:generate
Enter fullscreen mode Exit fullscreen mode

Migrate the database, here I'm also seeding it with defaults

php artisan migrate:fresh --seed
Enter fullscreen mode Exit fullscreen mode

A warning will be shown... Are you sure! You're in production.

5. Edit the .htaccess

Here we include the php8 version handler and force the site to use https. You may potential be able to skip this step.

cd public
vim .htaccess
Enter fullscreen mode Exit fullscreen mode

From this...

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews -Indexes
    </IfModule>

    RewriteEngine On

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (.+)/$
    RewriteRule ^ %1 [L,R=301]

    # Send Requests To Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>
Enter fullscreen mode Exit fullscreen mode

To this...

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews -Indexes
    </IfModule>

    RewriteEngine On

    # FORCE HTTPS
    RewriteCond %{HTTPS} off
    RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (.+)/$
    RewriteRule ^ %1 [L,R=301]

    # Send Requests To Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

# This handler adds the correct php [version?]
<IfModule mime_module>
    AddHandler application/x-httpd-ea-php80 .php .php8 .phtml
</IfModule>
Enter fullscreen mode Exit fullscreen mode

POW, The site is working! Visit the url for you new site.

Here you can run any other cache suggestions from Laravel's deployment section. I wait, until I know the site works properly.

Managing/Updating

[ This section is a work in progress]

1. Push New Code to the Repository

From your local project

git push origin master
Enter fullscreen mode Exit fullscreen mode

2. Pull it into the Live website

ssh your_usernam@50.88.888.88
Enter fullscreen mode Exit fullscreen mode

Go to live website files

cd ~/public_html/sitename
Enter fullscreen mode Exit fullscreen mode

Pull updated site in from repository

git pull origin master
Enter fullscreen mode Exit fullscreen mode

That's all for now. I hope this helps. Please let me know your thoughts and/or suggestions.

Discussion (0)