February 18, 2015

Deploying with SCP

Learn a basic way to deploy files to your server with secure copy.

We'll also cover some basic SSH operations we can use with the scp command.We cover using Secure Copy (scp) to upload files to a server and deploy a site.

This is great for static sites, or simple PHP sites. If a service needs restarting, files need reloading, or files need updating from version control, you may want to use a more refined tool, or others in conjunction with something like scp.

First we try to login into the server to test the SSH connection:

# Doesn't work for me, I have too many SSH keys
ssh root@104.236.85.162

# Tell SSH not to use ssh keys, jump to password-based authentication
ssh -o "PubkeyAuthentication no" root@104.236.85.162

The server sas Nginx installed with some basic HTML files at /var/www/html:

cd /var/www/html
ll

We can use the same SSH trick with scp (which uses SSH), to upload our files from the current directory on our local computer.

# scp src dest
# The remote server can be the source or the destination, in this case
# the remote server is the destination
scp -o "PubkeyAuthentication no" ./* root@104.236.85.162:/var/www/site/

Note that uploaded files are owned by the user we logged in as (root). We can see that Nginx is run as user/group "www-data". Check that out in Nginx configuration: cat /etc/nginx/nginx.conf.

To keep files owned by the same run-as user/group as Nginx, we can change the permissions within the server:

# On the remote server
chown -R www-data:www-data

Locally, we can do the same using a "one-off" command with SSH:

# From our local computer
# run the chown command on the remote server
ssh -o "PubkeyAuthentication no" root@104.236.85.162 "chown -R www-data:www-data /var/www/site"

We can do this all within a shell script deploy.sh, created on our local computer.

#!/usr/bin/env bash
scp -o "PubkeyAuthentication no" ./* root@104.236.85.162:/var/www/site/
ssh -o "PubkeyAuthentication no" root@104.236.85.162 "chown -R www-data:www-data /var/www/site"

Current

Deploying with SCP

All Topics