Developers Guide. Creating Code Repository (Git) Setting-up Virtual Host in Staging Server Automated deployment using Capistrano (staging server only) Sonny Cabali Version 0.2 Changes: Version Description 0.1 0.2 Initial copy -- added version description -- added shared folders/files section under Automated deployment Creating Git Repository User: gituser Group: gituser Repository server: 10.2.128.1 (ev-master) Location: /var/gitrepo Password: g1tus3r gituser is the repository owner and the delegated user for deployment in staging server (ev-master) All users that will need to have an access to the git repository should belong to the same group (gituser). To create a repository: Assuming you are logged as gituser: 1. Go to repository location cd /var/gitrepo 2. Create repository folder. By convention, this folder should end with ".git" i.e. mkdir myproject1.git 3. Initialize the repo by issuing the command below. cd myproject1.git git init --bare --shared Then you can clone this empty repository using one of the following command git clone {username}:{password}@10.2.128.1:/var/gitrepo/myproject1.git {foldername} git clone {username}@10.2.128.1:/var/gitrepo/myproject1.git {foldername} # it will prompt for password git clone {server-config-name}:/var/gitrepo/myproject1.git {foldername} i.e. Assuming all your sub-domain are located at /usr/local/var/subdomain cd /user/local/var/subdomain git clone sonny.cabali:123456@10.2.128.1:/var/gitrepo/myproject1.git myproject This will clone the myproject1.git to /user/local/var/subdomain/myproject1 git clone sonny.cabali@10.2.128.1:/var/gitrepo/myproject1.git myproject Same as above, but the console will prompt for your password git clone ev-master:/var/gitrepo/myproject1.git myproject Same as above but we use the ssh config file to supply parameters (see creating ssh config file) Creating user that has access to Git repository: 1. First create the user using the useradd command (You must be root) 2. For the user to be able to push and pull data from the repo, he must be part of the gituser group (that is the group name of the user we assigned as designated git admin) As root, issue this command. usermod –a –G {GITGROUP} {USER} usermod –a –G gituser carmela Creating SSH Config File The purpose of this config file is to create a connection key that will supply the username, password, host, port and even the identity file to the server for easy ssh connection. You can use this to connect to server via console, use in scp or git as parameters. For Linux/Windows: 1. In your home directory, create .ssh folder. It should be owned by the user and should have an ACL of 700. 2. Create a “config “ file with ACL of 644 3. Type the following in the config file a. Host <hostname> b. User <username> c. IndentityFile ~/.ssh/{ssh pub key} Example: - Assuming I already had my ssh key generated and stored under .ssh file (id_rsa and id_rsa.pub) (see Creating Open SSH key and configuring the server to auto login for instruction) #SSH Config File Host ev-master HostName 10.2.128.1 User sonny.cabali IdentityFile ~/.ssh/id_rsa To test (assuming your public key is set in the remote host), issue the command: ssh ev-master You should be able to connect to server without issuing username and password. Creating Open SSH key and configuring the server to auto login For Linux: 1. In console issue the following command ssh-keygen –t rsa 2. When asked to enter the file in which to save the key, just press enter to use the default value. 3. When asked for paraphrase, just press enter to provide none 4. After this, two files will be generated: id_rsa (private) and id_rsa.pub (public). 5. Login to the server (ev-master) using your account. In your home directory, create .ssh directory (ACL 700) then create a file and name it authorized_keys (with ACL 644) 6. Copy the contents of id_rsa.pub to authorized_keys. 7. Configure your config file (see Creating SSH Config File) 8. Test it by issuing the following command ssh ev-master This should login you to server without asking password. For Windows: 1. Use the puTTYgen tool to generate SSH keys. You can download it here. 2. For more information on generating key using puTTYgen, use this link. Setting up Virtual Host in Staging Server The ev-master server is setup with Apache Web Server 2.2.22 located at /etc/apache2 The current configuration allow setting up of virtual host by providing configuration file in directory /etc/apache2/sites-enabled. The file should end with extension .conf and should be symbolically linked to /etc/apache2/sites-available to activate. The following could be use as template in creating virtual host configuration. <VirtualHost *:80> ServerAdmin webmaster@localhost ServerName myproject.ev-master DocumentRoot /var/subdomain/myproject/web <Directory /var/subdomain/myproject/web> Options Indexes FollowSymLinks MultiViews AllowOverride All Order allow,deny Allow from all DirectoryIndex index.php index.html </Directory> ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost> Next, place your application file in /var/subdomain folder (as indicated in the config file above). Then edit the server’s hosts file (/etc/hosts requires root access) to include the following. Restart apache service for it to take effect. 127.0.0.1 ev-master Then to access it to your local machine, you need to also add those entries in your local hosts file (requires root access). For Linux, the location of hosts file is in /etc/hosts For Windows user, edit the file C:\windows\system32\drivers\etc\hosts Then add the following at the bottom of this file: 10.2.128.1 myproject.ev-master You can now access it in your local browser using the following url. http://myproject.ev-master IMPORTANT!!! If you are going to implement an auto-deployment script using Capistrano, you need to edit the conf file (in sites-available folder) to include current folder after the parent folder of your application. i.e. Assuming your application folder is myproject and the directory where the index.php is located at its subfolder web: /var/subdomain/myproject/web Then the new parameter for directory should be /var/subdomain/myproject/current/web Automated Deployment using Capistrano Checklist: Ruby version 1.9 or latest Capistrano 3.1.0 or higher Capistrano-ext 1.2.1 After installing ruby, you can install the Capistrano by issuing the following command. gem install capistratno capistrano-ext Full detail of Capistrano is located at this link. Below are the highlights of what should be the value of configuration. 1. Go to parent directory of the application then issue the following command to initialized Capistrano. cd /var/subdomain/myproject cap install This will create files with the following structure: |----- Capfile |----- config | |----- deploy | | |----- production.rb | | |----- staging.rb | |----- deploy.rb | |----- lib |----- capistrano |------- task The /config/deploy.rb file Below are the configuration based on the existing eastcube site # Set the title (any title) set :application, 'Eastcube' # the repository is residing on the same server as the staging server set :repo_url, 'file:///var/gitrepo/eastcube.git' set :scm, :git set :branch, 'master' # location where the file will be copied, eastcube is the parent directory set :deploy_to, '/var/subdomain/eastcube' # the following are server specific configurations. Normally it contains the server config and database config set :linked_files, %w{application/config/config.php application/config/database.php} # Default value for linked_dirs, commonly used for uploaded files, cache and logs # set :linked_dirs, %w{web/upload} # Default value for keep_releases is 5 set :keep_releases, 5 The /config/deploy/staging.rb # this will prompt for the password of the user designated in deployment. In ev-master case it is gituser set :password, ask('Server password:', nil) server 'ev-master', user: 'gituser', roles: %w{web app}, password: fetch(:password) To start the deployment, issue the following command cap staging deploy The first parameter “staging” corresponds to the environment configuration which is the /config/deploy/staging.rb To issue a rollback command cap staging deploy:rollbak IMPORTANT! Before you issue the initial deploy command, make sure that all the files and folder declared as shared are in the remote server. (Those files and directories used as parameter in /config/deploy.rb’s :linked_files and :linked_dirs) To do this, create a folder under the parent directory and name it as shared. Then put those directories and files under that folder. The Shared Folder/Files The shared folder/files are created under the parent directory of the application to contain all files used for initial setup and server specific configurations. The following are the directories default present in the initial setup Parent Folder |-------- db |-------- staging db folder contains initial dump sql files staging folder contains staging specific configuration folders and files which are declared in the config/deploy.rb files IMPORTANT!!! During or before the deployment, the environment specific folder (i.e. staging) is copied in the set deployment directory (see deploy_to in config/deploy.rb ) as shared.