Capistrano
Capistrano is an open-source tool for executing scripts at the remote servers. Most commonly, it is used for deploying apps via SSH connection. Capistrano is written in Ruby as a component of the Ruby on Rails framework, therefore, it is widely used for Ruby apps deployment. Nevertheless, it can easily work with other programming languages, e.g. PHP.
Thus, in this instruction we will discover how to deploy a PHP application remotely, via the Capistrano tool. Initially you will need:
- an already created PHP environment with Apache application server;
- SSH public key generated and added to your platform dashboard;
- GIT repository with PHP application you would like to deploy (for now Capistrano 3 tool supports GIT VCS type only);
- local copy of this project at your computer.
Let’s get started!
Install Capistrano
1. For using Capistrano, you need to have Ruby installed at your local computer. Therefore, execute the appropriate command:
|
|
2. Then, install the Capistrano tool by entering the following command:
|
|
3. Ensure you have the config folder in the local directory with your project (as it is a default folder with configurations for Ruby on Rails). Create this folder if you don’t have it.
|
|
Capify Your Application
After installation, you need to capify your application, i.e. configure Capistrano for app deployment. To do this, navigate to the root folder of your local PHP project and execute the next command:
|
|
This will create new files and directories in your project:
- Capfile is the main Capistrano file that takes care of the required configs and globs for custom tasks.
- config/deploy/ folder with two files (staging.rb and production.rb) for an environment’s specific deployment settings.
- config/deploy.rb Ruby script which contains application configurations and Capistrano instructions.
- lib/capistrano/tasks/ folder for your custom tasks.
Set Custom Configurations
1. Navigate to the config/deploy.rb file and configure it corresponding to your settings. Initially it looks like following:
|
|
Modify the next strings:
enter a name for your application:
1
set :application, "my_app_name"
specify URL to the VSC repository with your PHP application code:
1
set :repo_url, "git@example.net:me/my_repo.git"
Note: You need to have an SSH public key attached to your GIT account (the same one that you’ve added to the platform dashboard). Otherwise, you’ll get a “Permission denied” error while trying to deploy your application.
You can also use the https: link of the following type:
|
|
In this case, authentication is not required and you can state a URL to any PHP open-source repository you would like to deploy.
- uncomment the following line and state the directory your application will be deployed to (this value is default for the platform PHP app servers):
1
# set :deploy_to, '/var/www/webroot'
- uncomment the following lines:
1 2 3
set :scm, :git set :format, :pretty set :pty, true
- delete the strings with tasks code at the end of the file (starting from namespace :deploy do command) and paste the following lines instead:
|
|
You can also configure additional settings in this file (e.g. specify a repository branch or link additional files/folders) if it is required.
Save the changes you’ve made.
2. Then navigate to the config/deploy/staging.rb file. The default content is:
|
|
Firstly, edit three role : strings in the Simple Role Syntax section by pasting {nodeid-uid@your.SSH.host} instead of {deploy@example.com}. Use the following values:
- nodeid - node ID value of the Apache application server container in your environment;
- uid - number before @ symbol in your SSH connection string. After that, modify the server settings line (Extended Server Syntax section):
- specify your SSH host, e.g. server ‘gate.jelastic.com’
- enter {nodeid}_{uid} value for user parameter, e.g. user: ‘190403-136’
Thus, your server settings line will look like following:
|
|
Finally, specify the server port which will be used for the SSH connection:
|
|
Don’t forget to save these custom configurations.
3. Open the Capfile (located in the root folder of your local project) and add the next line to it:
|
|
Configure SSH Agent
1. Ensure you have your ssh-agent up and running in your system. 2. Add your private SSH key to the agent. It should correspond to the public key that you’ve added to the dashboard.
|
|
3. You can also check if the correct key was added by entering ssh-add -l command.
Check Configurations
Now, let’s make sure that everything was configured properly.
Navigate to the root folder of your local project and run the command below:
|
|
Capistrano will connect to the remote container, create the required folders in the deployment directory (stated within set :deploy_to parameter), and check both remote and local servers for the presence of all necessary files, required rights, tools, etc.
If something is missed, you’ll receive a corresponding error message.
Deploy Application
Finally, proceed to the application deployment. To do this, execute the command below in the project’s root folder:
|
|
When this operation is successfully completed, navigate to your environment’s URL and ensure your app has been deployed.