WordPress, Git, and Submodule: Installing wordpress locally using submodule and deploying to production server.

LOCAL SETUP

mkdir ~/local-wp && cd local-wp
touch readme.md
git add readme.md
git commit -m 'init commit'
touch .gitignore

Add following lines to `.gitignore`, personally I prefer ignoring `wp-config.php` as it contains sensitive info and database credentials:

# OSX files
.DS_Store

# intellij project settings
.idea/

wp-config.php

We are using wordpress github repo as a subrepository in our project.

git submodule add git://github.com/WordPress/WordPress.git wp

As wordpress is quite a big repo it might take a little while to clone the whole repo. Commit upon completion:

git add -A
git commit -m 'wordpress submoudle added'

Personally I prefer not to keep `wp-config.php` file in the git as it might expose sensitive database info. Therefore we’ll create same `wp-config.php` file in the server as local upon deployment.

cp wp/wp-config-sample.php wp-config.php [this config file is for local env. this file will be ignored by git.]

Now as wordpress is a subrepository and wp-config, index, and other core files will be the in the root directory of the project, we have to arrange things little bit to accommodate. We’ll manage our themes and plugins outside

cp -R wp/wp-content ./wp-content

Let’s clean up the `wp-content` little bit in order to save committing time.

rm wp-content/plugins/hello.php
rm -rf wp-content/themes/twentyten
rm -rf wp-content/themes/twentytwelve
rm -rf wp-content/themes/twentythirteen

If you prefer your custom theme put it under `wp-content/themes` directory.

Let’s commit then

git add -A
git commit -m 'default wp-content directory added.'

As a final structure of the project, `index.php` has to be placed in the root directory:

cp wp/index.php .
git add index.php
git commit -m 'index.php added.'

Basic skeleton of the project is ready. Now we have to configure these files to let wordpress know where to go to for everything.

### CONFIGURATION

In `index.php`:

// replace this line with the next line
require( dirname( __FILE__ ) . '/wp-blog-header.php' );
require( dirname( __FILE__ ) . '/wp/wp-blog-header.php' )

// And then commit
git commit -am 'index point to correct wp loaction.'

Place the following code in the in `wp-config.php` right below `@package WordPress`:

define('WP_SITEURL', 'http://' . $_SERVER['SERVER_NAME'] . '/wp');
define('WP_HOME', 'http://' . $_SERVER['SERVER_NAME']);

This let’s wordpress know that the core files are in the `wp` directory but the site will be served at the root of the project directory.

We also have to tell wordpress about the `wp-content` directory as we have moved it out of the `wp` directory and placed it in the root directory by adding following code to `wp-config.php`:

define('WP_CONTENT_DIR', $_SERVER['DOCUMENT_ROOT'] . '/wp-content');
define('WP_CONTENT_URL', 'http://' . $_SERVER['SERVER_NAME'] . '/wp-content');

And finally configure your database connection in the following section:

/** The name of the database for WordPress */
define( 'DB_NAME', 'db_name_here' );

/** MySQL database username */
define( 'DB_USER', 'db_user_here' );

/** MySQL database password */
define( 'DB_PASSWORD', 'db_passwd_here' );

/** MySQL hostname */
define( 'DB_HOST', 'localhost' );

### DEPLOY TO THE SERVER
It is assumed that your server accepts SSH connection and git is already installed in the server.

ssh -p 2222 user@server.com [supply port with -p flag if it's not default ssh port i.e. 22]
mkdir bare.git && cd bare.git
mkdir mysite && cd mysite
git init --bare
touch hooks/post-receive

Put the following code in `hooks/post-receive` script:

cd /path/to/bare.git/mysite

# SET GIT VARIABLES";
GIT_WORK_TREE=/path/to/your/live/files/ [e.g. /var/www/html]
GIT_DIR=/path/to/your/live/files/.git

# GO TO WEBSITE AND PULL";
cd /path/to/your/live/files

echo "PULLING FROM BARE";
git pull /path/to/bare.git/mysite

echo "UPDATING SUBMODULE";
git submodule update --init --recursive

echo "SUBMODULE UPDATED";

Make the post-receive file executable:

chmod +x post-receive

Now go to `/path/to/your/live/files` and make it a git repo:

git init

Reason to make root directory of the site a git repo is because you cannot execute submodule command (i.e. `git submodule update –init –recursive`) from a bare repo.

As we said `wp-config.php` will be ignored by git, therefore we have to create it in the server:

cd /path/to/your/live/files
touch wp-config.php
nano wp-config.php
// copy the content of the local wp-config.php except database connection
// credentails unless both of them are same for local and remote.
cltr + o
cltr + x

Now your remote server is ready for deploying. Switch back to local working directory:

git remote add pord ssh://user@server.com:/path/to/bare.git/mysite
git push prod master

Push all your changes and see the magic! If everything works well you should see an output as following:

user@server.com's password:
Counting objects: 75, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (69/69), done.
Writing objects: 100% (75/75), 691.05 KiB | 6.71 MiB/s, done.
Total 75 (delta 8), reused 0 (delta 0)
remote: PULLING FROM BARE
remote: From /path/to/bare.git/mysite
remote: * branch HEAD -> FETCH_HEAD
remote: UPDATING SUBMODULE
remote: Submodule 'wp' (git://github.com/WordPress/WordPress.git) registered for path 'wp'
remote: Cloning into '/path/to/your/live/files/wp'...
remote: Submodule path 'wp': checked out 'e2719c4e909b87a82df7bc12e1ba21ae4d1f5fd0'
remote: SUBMODULE UPDATED
To ssh://server.com:/path/to/bare.git/mysite
* [new branch] master -> master

Leave a Reply

Your email address will not be published. Required fields are marked *