How To Receive Emails From Your PHP OpenShift Application Using Swift Mailer & GMail


There are times when we need emails to be sent/received from our PHP applications hosted on OpenShift. My particular example is based on a feedback form that I have put on my own PHP based web-application hosted on OpenShift http://www.howmuchisaved.in. Using this form, I can receive email whenever users submit their feedback to me. If your applications is hosted on your own server, you can easily setup Postfix to start receiving mails but things start to get tricky when you have little to no control on the web-server which runs your application. OpenShift doesn’t allow you to configure Postfix or any such utilities. It even doesn’t give you root access to install & configure dependencies. So, you are left with very little options. And this is where Swift Mailer comes to rescue. In this tutorial I will explain how you can leverage Swift Mailer PHP library to be able to receive mails. Please note that this tutorial can be followed for other restrictive hosting solutions as well.

Requirements : –

For this tutorial, you will need one GMail account (more on it below) & one PHP application hosted on OpenShift or any cloud platform.

Steps to be performed : –

Configure Google Security settings

Since we are using GMail as our SMTP, all emails will be delivered/relayed via smtp.gmail.com. By default, Google doesn’t allow less-secure apps (such as your OpenShift app) to access your account. So, go to https://www.google.com/settings/security/lesssecureapps and turn it on. Its better to create a new Google account & turn this setting on for that particular account. Choice is yours, so act accordingly.

Download Swift Mailer PHP Library

Download it from here https://github.com/swiftmailer/swiftmailer & place it inside your project directory.

Or if you have git installed on your machine, you can always use git clone https://github.com/swiftmailer/swiftmailer inside your project directory. This will create a new directory inside your project. Rename it so that its easy to reference Swift Mailer inside your code. I renamed mine as swiftmailer.

Edit PHP code

Now, with Swift Mailer library in place, edit your PHP script that handles e-mail functionality. Below is the sample code snippet from my GitHub repository’s file.

https://github.com/shashank-ssriva/HowMuchISaved/blob/openshift-version/howmuchisaved/send_feedback_mail.php

You may fork or download my entire project if you so wish 🙂 This version is fully functional and is already hosted on OpenShift (as told in introduction above).

require_once 'swiftmailer/lib/swift_required.php';
      //allow less secured app in Gmail settings for this to work.
      //Also use port 465 and ssl if it doesn't work.
      $transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 587, "tls")
      ->setUsername('your gmail username')
      ->setPassword('your gmail password');

You can see in the snippet above how you need to organise your Swift Mailer directory. You also need to enter your GMail credentials there.

Test your application

After making changes to the code, fire up your application in a browser & check if you received email from this page or not. Please note the line #22 in above
script. It mentions the email-address where you will be receiving e-mails from your application. Change this to yours if not already done.

That’s it 🙂 If everything has been configured correctly, you will start to receive e-mails from this application. You may use my repository for fully functional application & its code.

Host a PHP/MySQL Application/Dynamic Website on RedHat OpenShift


I had no idea that I can host my PHP/MySQL based application on OpenShift for free until I had to do some work on it a few weeks back. While working on some Automation with OpenShift, I came up with the idea to host my own app on it 😉

If you have a small PHP application or some dynamic PHP based website that you want to host on OpenShift for free, please read on.

So, here is how to do it.

Go to OpenShift Console

Login to your OpenShift account & go to console https://openshift.redhat.com/app/console/applications. You will see a button called “Add Application” like below. Click on it.

Applications___OpenShift_Online_by_Red_Hat.jpg

Choose PHP

Choose any of the PHP versions.

Create_a_New_Application___OpenShift_Online_by_Red_Hat.jpg

Enter a Public URL

Use a unique URL for your application. If you have not created a domain earlier, create one before this step.

Create_a_New_Application___OpenShift_Online_by_Red_Hat.png

Choose Scaling

If you want an HAProxy Loadbalancing, choose Scale with web-traffic as seen above. Please bear in mind that if your application is based on MySQL or any other Database, don’t scale it and go with default. Otherwise you will need to use HAProxy server’s IP or FQDN in your code for database connection string.

Choose Region

Free tier of OpenShift doesn’t allow to choose anything except first 2 options.

Create_a_New_Application___OpenShift_Online_by_Red_Hat.png

Create Application

Click “Create Application” button to create your application. This may take a few minutes.

Get_Started___OpenShift_Online_by_Red_Hat.png

Install Git client for your OS

Install Git client by following instructions shown there on the screen. Also, make note of the git clone command. Using these details, you will clone a repository from OpenShift to your machine. From this repository, you can push to OpenShift after making changes to code. Follow the instructions you see after clicking “Create Application” button.

Add Database gear

Add any of the Database gears. I have personally chosen MySQL 5.5. Make note of the details like database user & password. You will have to use these values in your code.

php___OpenShift_Online_by_Red_Hat.png

Push/Publish Code

Add your code project to this repository (created above). Edit your database connection string in code using the values you got from last step. Then run below commands to push it to OpenShift & restart Apache there. Whenever you make changes to code or add a new file/directory to your local repository, don’t forget to follow below steps.

git add .

git commit -am "adding project data"

git push

You are done 🙂 You will see your application/site hosted on OpenShift now 🙂

I hope you liked this post. Catch you soon with another post!

Install & Setup SonarQube on Ubuntu for Code Analysis


SonarQube is Code Quality testing solution which lets you analyse the quality of your code, detect bugs and much more to improve overall health of your code.

SonarQube comes in 2 variants. It can be accessed online using the URL https://sonarqube.com/ and it can also be hosted on your own server. In this tutorial, I am demonstrating how you can install & setup SonarQube on your own Ubuntu server to check your code’s quality 🙂

Lets start!

Lab Description : –

Ubuntu 14.04 64 bit server with 2 GB RAM.

MySQL version 5.6.33 with InnoDB storage engine.

SonarQube version 6.2.

My PHP project located at DocumentRoot. You can choose any location for code analysis.

Please note that SonarQube needs atleast 2 GB of RAM, so please make sure you have enough of it.

Steps to be followed : –

SonarQube by default uses its internal H2 database but we will be using MySQL for this. Choose any database of your choice.

1. Download SonarQube & SonarQube Scanner.

Use the links provided to download both the products.

2. Unpack them.

Unpack both of them to any location where you can locate them easily. I chose my home-directory for it. It will create 2 directories sonarqube-6.2 & sonar-scanner-2.8.

root@shashank-dbserver:/home/shashank# unzip Downloads/sonarqube-6.2.zip

root@shashank-dbserver:/home/shashank# unzip Downloads/sonar-scanner-2.8.zip

It will be good if you create aliases for above 2 directories or add them to your PATH.

3. Create MySQL Database & User.

Create a new database called sonar in MySQL (or any other DBMS of your choice). Then create a user sonarqube & grant it all privileges for sonar database.

mysql> create database sonar;
Query OK, 1 row affected (0,01 sec)

mysql> use sonar;
Database changed
mysql> CREATE USER 'sonarqube'@'localhost' IDENTIFIED BY 'sonarqube';
Query OK, 0 rows affected (0,02 sec)

mysql> GRANT ALL PRIVILEGES ON sonar.* to 'sonarqube'@'localhost';
Query OK, 0 rows affected (0,00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0,00 sec)

4. Edit sonar.properties file.

Edit this file inside sonarqube-6.2/conf directory to enter database details. Make sure to put user-name & password you created in last step. Below is the snippet. Edit values accordingly.

# DATABASE
#
# IMPORTANT: the embedded H2 database is used by default. It is recommended for tests but not for
# production use. Supported databases are MySQL, Oracle, PostgreSQL and Microsoft SQLServer.

# User credentials.
# Permissions to create tables, indices and triggers must be granted to JDBC user.
# The schema must be created first.
sonar.jdbc.username=sonarqube
sonar.jdbc.password=sonarqube

#----- Embedded Database (default)
# H2 embedded database server listening port, defaults to 9092
#sonar.embeddedDatabase.port=9092
#----- MySQL 5.6 or greater
# Only InnoDB storage engine is supported (not myISAM).
# Only the bundled driver is supported. It can not be changed.
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance

5. Create & Edit sonar-project.properties file.

Create this file inside your code’s project & enter values accordingly. See below snippet. Give your project a unique Project Key for SonarQube to uniquely identify it.

# must be unique in a given SonarQube instance
sonar.projectKey=exclaimadeasy
# this is the name and version displayed in the SonarQube UI. Was mandatory prior to SonarQube 6.1.
sonar.projectName=ExClaiMadEasy
sonar.projectVersion=1.0

# Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows.
# Since SonarQube 4.2, this property is optional if sonar.modules is set.
# If not set, SonarQube starts looking for source code from the directory containing
# the sonar-project.properties file.
sonar.sources=.

# Encoding of the source code. Default is default system encoding
#sonar.sourceEncoding=UTF-8

6. Start SonarQube.

Start it by executing sonarqube-6.2/bin/sonar.sh start command.

root@shashank-dbserver:/home/shashank/sonarqube-6.2/bin/linux-x86-64# ./sonar.sh start
Starting SonarQube...
Started SonarQube.

7. Access SonarQube via browser.

Open your browser & enter localhost:9000. Then click login at the top-right corner. Credentials are admin/admin.

8. Start SonarQube Scanner to analyse your code.

Execute below command to start SonarQube scanner from within your project directory.

root@shashank-dbserver:/var/www/bills/html/CabBIlls# /home/shashank/sonar-scanner-2.8/bin/sonar-scanner

It will start scanning your project’s code. Once its done scanning, you will see output similar to below. Click the link provided there to see your report.

INFO: Analysis report uploaded in 240ms
INFO: ANALYSIS SUCCESSFUL, you can browse http://localhost:9000/dashboard/index/exclaimadeasy
INFO: Note that you will be able to access the updated dashboard once the server has processed the submitted analysis report
INFO: More about the report processing at http://localhost:9000/api/ce/task?id=AVqskPfd6DjWymbXBiOQ
INFO: Task total time: 18.806 s
INFO: ------------------------------------------------------------------------
INFO: EXECUTION SUCCESS
INFO: ------------------------------------------------------------------------

Video Tutorial : –

I have also posted a video on my YouTube channel to demonstrate all the steps. You can watch it below.

Caveats : –

Video shown above only demonstrates the basic code analysis, even though my project is PHP based. For PHP projects (or any other non-default languages), please download the plugin(s) and place that in SonarQube_HOME/extensions/plugins directory. After that restart SonarQube by executing sonar.sh restart command. PHP plugin can be downloaded from https://docs.sonarqube.org/display/PLUG/SonarPHP

Also, in the video above, I missed to uncomment MySQL jdbc conncection URL but same can be seen uncommented in snippet I pasted in step 3 😉

I hope you liked this post. See you later 🙂

Create a Server Health Report (HTML) Using Shell Script


Shell scripts are insanely powerful & convenient. We all know it 😉 Much of the beauty in shell scripts lies in the way they can be used to automate many aspects of System Administration. As a SysAdmin, you might have been asked to prepare health-reports on a regular basis. Today, I wrote one such script that will generate an HTML health-report containing some vital system information. Lets see how it works 🙂

Lab Description : –

Ubuntu 14.04 Server. Environment : – Bash shell

Instructions : –

Download or clone my GitHub repository from below location.

https://github.com/shashank-ssriva/Linux-Server-HTML-Health-Report-Using-Shell-Script

Place the syshealth.sh file anywhere you want. I prefer keeping it under my home-directory but you may keep it anywhere.

Make it executable (if not already).

You may either run/execute it manually or you may also put it in a CRON job. I have chosen to generate the report twice a day, but its entirely upto you 🙂

Video Tutorial : –

To see the script in action, watch the video below on my YouTube Channel.

Additional Notes : –

I have kept the script & report minimal since I wrote it today only. You may customize it further so as to suit your needs. Sky is the limit 😉