Git & Github

Git for Deployment

We'll create a repository for our local code base.

Make an PHP App

cd ~/Sites/serialapp

git init
git remote add origin git@github.com:Servers-for-Hackers/serialapp.git
git add --all
git commit -m "first commit"
git push -u origin master

This pushes the code up to GitHub!

Then we create a PHP application, which we'll use for the rest of this series as an example.

cd ~/Sites/serialapp

composer create-project laravel/laravel serialapp

# Move everything in ~/Sites/serialapp/serialapp up a level to ~/Sites/serialapp
cd ~/Sites/serialapp/serialapp
mv ./* ../
mv ./.* ../
cd ../
rm -r serialapp # remove extra directory

We can then try to run this on our local computer:

cd ~/Sites/serialapp/public
php -S 0.0.0.0:8000

Heading to localhost:8000 in your browser should show a Laravel app!

Then we can add all these changes and push them up to our respository:

git add --all
git commit -m "added laravel app"
git push origin master

Setup our Server

Next, we want to log into our server and setup our Github repository. This will let us orchestrate updating the code on our server after we push it to Github.

Install Git as user admin:

ssh serial-admin
sudo apt-get install -y git

Then login as user serial, and we'll clone the serialapp repository:

# SSH into our production app server
ssh serial-app

cd ~/serialapp.com/current/
git clone git@github.com:Servers-for-Hackers/serialapp.git repo

This fails because this server does not have access to communicate to my git repository

GitHub SSH Access

Let's create a new, passwordless, ssh key to use to communicate to GitHub:

cd ~/.ssh
ssh-keygen -t rsa -b 4096 -C "chris@serialapp.com"

Copy the contents of ~/.ssh/id_rsa.pub into GitHub for the repository, giving it read-only access.

Then try cloning that repository again!

cd ~/serialapp.com/current/
git clone git@github.com:Servers-for-Hackers/serialapp.git repo
# creates repo in ~/serialapp/current/repo

That should now work!

As of this moment, the Nginx web root is set to ~/serialapp/current/public.

As a temporary solution to get this running, let's change the Nginx configuration for the site to make our repo/public directory the web root.

This needs to be done as user admin, who can run sudo commands, so log back in as that user.

Then we can edit the Nginx configuration at /etc/nginx/sites-available/serial. Change the root directive to:

server {
    ...
    root /home/serial/serialapp.com/current/repo/public;
    ...
}

Then we can reload the Nginx configuration:

sudo service nginx reload

If we reload the browser now, we'll get a blank white page. We need to get the code base to work, which includes getting the composer dependencies.

# as user admin still
curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer
which composer # /usr/loca/bin/composer

Then install the composer dependencies:

# as user serial
cd /home/serial/serialapp.com/current/repo
composer install

We still see an error when reloading that in the browser. We'll see an error about "No supported encrypter". We need an .env file to exist, it should have a proper application key (the APP_KEY variable).

Once that exists and is set, we'll get the Laravel application to show!