May 10, 2017

PHP App Setup & Permissions


First we installed Composer so we can get PHP dependencies:

# Become user root
sudo su

# Pipe the composer installer to php, and pass the installer some flags
curl -sS | sudo php -- --install-dir=/usr/local/bin --filename=composer


Next, we want to get a Laravel project from Github.

We'll need access to Github. I chose to create a Deploy Key within Github, so the server has read-only access to a specific repository.

# Create an ssh key in /home/fideloper/.ssh
ssh-keygen -t rsa -b 4096 -f ~/.ssh/id_rsa -C "sfh-start-here"
cat ~/.ssh/
# Copy the output into a new Deploy key within Github, under the repository settings

# Test our connection to github
ssh -T

We copy the contents of the public at/home/fideloper/.ssh/ to Github while creating the Deploy key.

Then we can clone the repository. We clone it into user fideloper's home directory since we have permission to write new files there since we are logged in as user fideloper.

# Delete old webroot, which only had an index.php file in it
# We'll put our laravel application in this location
sudo rm -rf /var/www/html

# Ensure we have git
which git
git --version
# sudo apt-get install -y git

# Go to our home directory and clone the repository into a new directory named "html"
cd ~
git clone html


Within the /etc/nginx/sites-available/default file, we update the root to point to the Laravel web root, which is the public directory.

Edit the file /etc/nginx/sites-available/default to update the root directive:

# Change:
root /var/www/html;

# To:
root /var/www/html/public;

Then test the change and reload Nginx:

sudo nginx -t
sudo service nginx reload


We're ready to setup the application. First, we'll move it from user fideloper's home directory, to the /var/www/html location that Nginx expects web files to live in.

Since the Laravel application contains a public directory meant to be the web root, our change to the Nginx configuration will be correct when it points to /var/www/html/public.

# Put the application in the /var/www directory
sudo mv ~/html /var/www/

# Create  a new .env file
cd /var/www/html
cp .env.example .env

# Install zip/unzip so composer can get Git-based dependencies
# from "dist" instead of having to clone each repository (slow!)
sudo apt-get install -y zip unzip

# Install composer dependencies
composer install

# Generate a new application key in the .env file
php artisan key:generate

# Ensure files are writable (particularly `storage` and `bootstrap` need to be writable by Laravel!)
# Otherwise we'll get the blank white screen of death

# We can see that PHP is running as user/group "www-data"
ps aux | grep php

# So we change our web files to that user/group, thus letting PHP write to those locations
sudo chown -R www-data: /var/www/html


PHP App Setup & Permissions

All Topics