I have used Capistrano3 to deploy the latest version of WordPress on my own host, so I will keep a log of the steps and advance my understanding of the deployment tool "Capistrano". As a preliminary preparation, I launched the deployment environment as a t2.micro instance on Amazon EC2 and listed it with the WordPress running environment (Apache + MySQL + PHP), Rubygem (and RVM), and Capistrano3 installed ( I would like to summarize the preliminary preparation procedures in this area as TIPS at a later date, but I will omit it this time).
Now, let's get into the procedure. To get started, create a directory for your deployment project and install Capistrano.
$ cd ~
$ mkdir test-project
$ cd test-project
$ cap install STAGES=test
mkdir -p config/deploy
create config/deploy.rb
create config/deploy/test.rb
mkdir -p lib/capistrano/tasks
Capified
Capistrano3 has the multi-stage deployment feature turned on by default, so simply 'cap install' will create a production stage and a staging stage. This time I'm going to try to deploy on my own localhost only for testing, so I'll specify the test stage-only project as an argument to the environment variable 'STAGES'1。
Next, perform the initial configuration of Capistrano. If necessary, edit the 'Capfile' created directly under the project directory.
$ vim Capfile
Since we will not be using RVM or RBENV for deployment, there are no edits2.
Next, edit 'config/deploy/test.rb' to configure the deployment environment (server).
$ vim config/deploy/test.rb
----
role :app, %w{deploy@localhost}
role :web, %w{deploy@localhost}
role :db, %w{deploy@localhost}
set :ssh_options, {
keys: %w(/home/deploy/.ssh/id_rsa),
forward_agent: true,
}
Then, set the deployment content in 'config/deploy.rb'.
$ vim config/deploy.rb
----
# config valid only for Capistrano 3.1
lock '3.2.1'
# Application name to deploy
set :application, 'wordpress'
# Distributor URL and filename of the latest version of WordPress core files
set :wget_url, 'http://ja.wordpress.org/'
set :wget_file, 'latest-ja.tar.gz'
# Theme file GitHub URL from which it is distributed (optional)
#set :repo_url, 'git@example.com:me/my_repo.git'
# Branch name of the GitHub repository to be deployed (optional)
#set :branch, 'master'
# Database management account (users and users with database creation permissions)
set :db_user, 'root'
set :db_user_passwd, 'password'
# WordPress database information (create if you don't have one)
set :wp_db_name, 'wp_test_db'
set :wp_db_user, 'wp_user'
set :wp_db_user_passwd, 'wp_user_pass'
set :wp_db_prefix, 'test_'
# Deploy to document root
set :deploy_to, '/var/www/html'
# Version control in git
#set :scm, :git
# Default value for :format is :pretty
set :format, :pretty
# Specify the log output level to debug mode
set :log_level, :debug
# Default value for :pty is false
# set :pty, true
# Default value for :linked_files is []
set :linked_files, %w{config/database.yml}
# Default value for linked_dirs is []
set :linked_dirs, %w{bin log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system public/assets}
# Default value for default_env is {}
# set :default_env, { path: "/opt/ruby/bin:$PATH" }
# Generations of generation management. This time, it will be managed by two generations
set :keep_releases, 2
# Delete Capistrano default tasks
framework_tasks = [:starting, :started, :updating, :updated, :publishing, :published, :finishing, :finished]
framework_tasks.each do |t|
Rake::Task["deploy:#{t}"].clear
end
Rake::Task[:deploy].clear
desc 'Deploy the WordPress site'
task :deploy do
on roles(:web) do
# Download the latest version of WordPress from the official WordPress website and expand the archive to your home directory
deploy_log = capture "wget #{fetch(:wget_url)}#{fetch(:wget_file)}; tar -zxvf #{fetch(:wget_file)}"
archive_dirname = fetch(:wget_file).gsub(/.tar.gz/, '')
# Copy all downloaded WordPress sources to the document root
# * Permission required for the deployment user to write to the document root
deploy_log = capture "cp -fpr #{fetch(:application)}/* #{fetch(:deploy_to)}"
# Create a WordPress configuration file if you don't have it
wp_config = fetch(:deploy_to)+"/wp-config.php"
unless test "[ -f #{wp_config} ]"
execute "mv #{fetch(:deploy_to)}/wp-config-sample.php #{wp_config}"
end
# Create a .htaccess for permalink rewrite and give write permission if you don't have it
htaccess = fetch(:deploy_to)+"/.htaccess"
unless test "[ -f #{htaccess} ]"
execute "touch #{htaccess}; chmod a+w #{htaccess}"
end
# Delete archive directories left in the home directory
deploy_log = capture "rm -rf #{fetch(:application)}"
info deploy_log
end
end
Try to deploy it in practice.
DEBUG[a7ae646a] Running /usr/bin/env wget http://ja.wordpress.org/latest-ja.tar.gz; tar -zxvf latest-ja.tar.gz on localhost
DEBUG[a7ae646a] Command: wget http://ja.wordpress.org/latest-ja.tar.gz; tar -zxvf latest-ja.tar.gz
~ (omitted) ~
DEBUG[a7ae646a] Finished in 3.837 seconds with exit status 0 (successful).
DEBUG[d19f20d5] Running /usr/bin/env cp -fpr wordpress/* /var/www/html on localhost
DEBUG[d19f20d5] Command: cp -fpr wordpress/* /var/www/html
DEBUG[d19f20d5] Finished in 0.150 seconds with exit status 0 (successful).
DEBUG[dd0f38a4] Running /usr/bin/env [ -f /var/www/html/wp-config.php ] on localhost
DEBUG[dd0f38a4] Command: [ -f /var/www/html/wp-config.php ]
DEBUG[dd0f38a4] Finished in 0.102 seconds with exit status 1 (failed).
INFO[35f0bcff] Running /usr/bin/env mv /var/www/html/wp-config-sample.php /var/www/html/wp-config.php on localhost
DEBUG[35f0bcff] Command: mv /var/www/html/wp-config-sample.php /var/www/html/wp-config.php
INFO[35f0bcff] Finished in 0.102 seconds with exit status 0 (successful).
DEBUG[822f4fb0] Running /usr/bin/env [ -f /var/www/html/.htaccess ] on localhost
DEBUG[822f4fb0] Command: [ -f /var/www/html/.htaccess ]
DEBUG[822f4fb0] Finished in 0.107 seconds with exit status 0 (successful).
DEBUG[25219b94] Running /usr/bin/env rm -rf wordpress on localhost
DEBUG[25219b94] Command: rm -rf wordpress
DEBUG[25219b94] Finished in 0.111 seconds with exit status 0 (successful).
INFO
The above log was output, and the latest version of WordPress was successfully deployed to the document root. That's it for this time. Next time, I'll try to create a database and insert the initial configuration as a last-minute task, then rewrite the WordPress configuration file and download the WordPress theme from GitHub.
Reference Sites
- Introductory Capistrano 3 ~ I want to erase all manual work before it is born.
- Getting Started with Capistrano Deployments for Ruby Beginners
- Guide to Capistrano 3
- Comfortable deployment life with Capistrano 3!