SOP for Creating, Deploying & Managing (Laravel) Websites on Bluehost Shared
Tags: laravel
Published: 2021-11-25 15:19:35


> 📎 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](https://www.sitepoint.com/deploy-website-using-laravel-git/). 

## 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`

```bash
ssh your_usernam@50.88.888.88
cd repo
```

Initialize a new repository.

```bash
git init --bare --shared sitename.git
```
> Note this is outside of your public folder structure.


#### 3. Add remote to your local project

Go to local website folder (on your computer).
```bash
cd ~/"Path/to/websites/sitename"
```

Add the repo as origin
```bash
git remote add origin your_usernam@50.88.888.88:~/repo/sitename.git
```

Force push the site and set origin as upstream 
```bash
git push -f --set-upstream origin master
```

*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` 
```bash
ssh your_usernam@50.88.888.88
cd repo
```

Now initialize your repository.
```bash
git init --bare --shared sitename.git
```
> Note this is outside of your public folder structure.


#### 2. Clone to create local website

Go to local website files.
```bash
cd ~/"Path/to/websites"
```

Now clone the central `--bare` repository you just created and logout of this SSH session.
```bash
git clone your_usernam@50.88.888.88:~/repo/sitename.git sitename
```

#### 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)
```bash
ssh your_usernam@50.88.888.88
```

Go to live website files 
```bash
cd ~/public_html/
```

Now clone the central repository for the site
```bash
git clone your_usernam@50.88.888.88:~/repo/sitename.git sitename
```

### 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
```bash
cd ~/public_html/sitename/
```

Rename the example env file
```bash
mv .env.example .env
```

Edit the .env file adding relevant production values
```bash
vim .env
```

> 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
```bash
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}"

...
```

> 💡 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.

```bash
/usr/local/bin/ea-php80 /opt/cpanel/composer/bin/composer install --optimize-autoloader --no-dev
```

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

```bash
/usr/local/bin/ea-php80 /opt/cpanel/composer/bin/composer install
```

Generate the site key.

```bash
php artisan key:generate
```

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

```bash
php artisan migrate:fresh --seed
```

*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.

```bash
cd public
vim .htaccess
```

From this...

```bash
<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>
```

To this...

```bash
<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>
```

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](https://laravel.com/docs/8.x/deployment#optimization). 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
```bash
git push origin master
```

### 2. Pull it into the Live website

```bash
ssh your_usernam@50.88.888.88
```

Go to live website files 

```bash
cd ~/public_html/sitename
```

Pull updated site in from repository 

```bash
git pull origin master
```

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