Develop Locally, Test Remotely with Bitnami LAMP and Git

Introduction

When developing applications, having consistent server environments is key. This means that individual developers working on the application should use the same development environment, and development, production and test environments should also be consistent with each other. This reduces friction, simplifies the work of DevOps teams and avoids the “it works on my machine” syndrome.

Bitnami stacks provide a solution to this problem. Bitnami’s LAMP stack, for example, provides a complete, fully-integrated and ready to run LAMP development environment for multiple platforms. Since it’s the same image, just packaged differently for each platform, developers can write code on their local systems using the tools they’re most comfortable with, then transfer that code to a cloud server running the same image without ever encountering any missing libraries or incompatibilities. For a solo developer, this out-of-the-box consistency can often save hours of time and effort.

This article will demonstrate how a solo developer can use the Bitnami LAMP stack to develop an application locally and make it available for immediate testing by a client or end-user on a public cloud server. With a little help from Git (which comes pre-installed in the Bitnami LAMP stack), application changes made by the developer on his or her local machine can be immediately reflected in the public test version running in the cloud.

NOTE: The approach outlined in this article is best suited for a solo developer whose goal is to develop an application locally but also be able to deploy it to a public cloud server so that end-users can view and test it. Although this article uses the Bitnami LAMP stack and a PHP application as an example, the approach outlined here will also work for other Bitnami stacks and programming languages.

Step 1: Create a custom PHP application on the LAMP virtual machine

The first step is to create an application on your local development environment: the Bitnami LAMP virtual machine. In this example, we’ll assume that you’re creating a PHP version of the board game Twister and that you’ve decided to use Laravel as the PHP framework.

Create and edit the /opt/bitnami/apps/twister/conf/httpd-prefix.conf file and add the lines below to it. Notice that the document root for the application in this and subsequent configuration files is set to point to the public/ sub-directory of the Laravel application.

Create and edit the /opt/bitnami/apps/twister/conf/httpd-app.conf file and add the content below to it. This is the main configuration file for your application, so modify it further depending on your application’s requirements.

Once you have created the files and directories above, add the following line to the end of the main Apache configuration file at /opt/bitnami/apache2/conf/bitnami/bitnami-apps-prefix.conf, as shown below:

Include "/opt/bitnami/apps/twister/conf/httpd-prefix.conf"

Restart the Apache server:

$ sudo /opt/bitnami/ctlscript.sh restart apache

You should now be able to access the skeleton application’s welcome page at http://VM-SERVER-IP/twister. Here’s a screenshot of what you should see:

Step 2: Create a local Git repository for your application

The next step is to create a local Git repository for your application code and then commit the initial set of files to it by running the commands below. Replace the example name and email address shown below with your correct information.

By default, your changes will be committed to the repository’s master branch. However, you’d typically want to save new, untested changes in a different branch and merge them into the master branch only once you’re confident that they work as expected. With this in mind, create a dev-master branch for work in progress:

$ git checkout -b dev-master

NOTE: This dev-master branch is the branch that you’ll deploy to the cloud server for you and other users to view and test new changes in the application.

Step 3: Configure Apache on the LAMP cloud server

Once you have your local development environment up and running, the next step is to configure the Bitnami LAMP stack running on the cloud server (where you and/or end-users will test the latest changes). If you wish, you can replicate the same directory structure there but for variety, let’s assume that you want the application on the cloud server to be visible at the document root - that is, at http://CLOUD-SERVER-IP/ instead of http://CLOUD-SERVER-IP/twister.

To begin, log in to the cloud server and delete the files in the current Apache document root:

$ cd /opt/bitnami/apache2/htdocs/
$ rm -rf *

Change the Apache configuration so that the document root points to the public/ directory of the Laravel application by editing the /opt/bitnami/apache2/conf/bitnami/bitnami.conf file and replacing all instances of the path /opt/bitnami/apache2/htdocs to /opt/bitnami/apache2/htdocs/public/. Here’s an example of what the end result should look like:

NOTE: You will see a warning that the public/ directory, which is referenced as the Web server’s document root, does not exist. Ignore this warning for the moment, as this directory will be created when you deploy the application to the cloud server.

$ sudo /opt/bitnami/ctlscript.sh restart apache

Step 4: Create a bare Git repository and post-receive hook

The next step is to create a bare Git repository on the server, and a post-receive hook that will accept new commits and deploy to the server document root.

Create a local environment file at /home/bitnami/twister.env for the Laravel application and fill it with the following content. This file is Laravel-specific, so you can omit this step for non-Laravel applications:

Every time you push a new commit to this repository, the post-receive hook will check out the latest version of the dev-master branch to the /opt/bitnami/apache2/htdocs directory. It will then perform any custom deployment steps required to get your application working.

In this case, the steps are similar to those performed when you initialized the Laravel application in your local development environment. Depending on your application’s requirements, modify the steps shown above as needed.

Make the post-receive hook executable:

$ sudo chmod +x /home/bitnami/twister.git/hooks/post-receive

Step 5: Configure SSH and test the Git remote repository

The next step is to configure SSH so that it’s possible to log in to the Bitnami LAMP cloud server from the Bitnami LAMP virtual machine. This is needed so that you can push code changes from the virtual machine to the bare repository on the cloud server.

The easiest way to do this is by forwarding your SSH key on the host machine:

On your host system, add the following lines to the ~/.ssh/config file:

Next, configure the bare Git repository on the Bitnami LAMP cloud server as a remote repository and test it, as follows:

Log in to the Bitnami LAMP virtual machine. Add the cloud server as a remote server for your repository, as shown below. Replace the CLOUD-SERVER-IP placeholder with the public IP address of the cloud server:

Find the page title and change it. For example, change the default title “Laravel” to “Welcome to Twister!”.

Save the changes, commit and push:

$ git commit -a -m "Changed title"
$ git push test-server dev-master

Browse to the cloud server’s IP address and confirm that you see the modified welcome page, as shown below:

You now have a basic system in place which will allow you to develop and commit changes locally on the Bitnami LAMP virtual machine, then immediately make those changes visible on a public Bitnami LAMP cloud server that you or your clients can access for testing or review purposes.

Useful links

To learn more about the topics discussed in this guide, consider visiting the following links: