PHP Posts

post

Recently,  I've started working on one project where we need to set up a virtual host with HTTPS because I need to run that project with expose and Shopify in my local development machine. I've wamp 3.2.3 on my local machine.

I spend a lot of time for setup it. so, I thought I should write one article for step by step guide. So, I show you in this article how to setup HTTPS for a local machine.

Step 1 - Install Wamp

Install wamp server if not installed in your local machine. you can download the latest version of the wampserver from here. wampserver is available in 32 bit and 64 bit. make sure you select the correct version of the wampserver based on your operating system (window)'s version.

Step 2 - Install OpenSSL

OpenSSL is an open-source command-line tool that is used to generate the SSL certificate and private key. OpenSSL is available in both versions 32 and 64 bit. download the latest version of OpenSSL from here.


I hope you successfully installed OpenSSL on your machine. let's take the next step

Step 3 - Create a Private key

Open your terminal as an Administrator otherwise you will get a permission denied error. also, you can provide permission to the OpenSSL directory and run the terminal on normal mode.

Now, let go to where we installed OpenSSL

cd C:\Program Files\OpenSSL-Win64\bin

Let's create a private key which is 2048 bits encryption.  fire one by one the following two commands to create it.

openssl genrsa -aes256 -out private.key 2048
openssl rsa -in private.key -out private.key


Your private.key is successfully generated here C:\Program Files\OpenSSL-Win64\bin 

Step 4 - Create an SSL Certificate

Let's create a certificate using the following command,

openssl req -new -x509 -nodes -sha1 -key private.key -out certificate.crt -days 36500

You need to enter a detail looks like


You can verify here 

Step 5 - Move both Private Key and a Certificate

Open a directory D:\wamp64\bin\apache\apache2.4.46\conf (Based on where your wamp is installed) and create a `key` directory.

Now, move both files to the `key` directory.

Step 6 - Configure Your httpd.conf File

Open your `D:\wamp64\bin\apache\apache2.4.46\conf\httpd.conf` (the drive should be where your wamp is installed) and un-comment the following 3 lines one by one.

LoadModule ssl_module modules/mod_ssl.so
Include conf/extra/httpd-ssl.conf
LoadModule socache_shmcb_module modules/mod_socache_shmcb.so

Step 7 Configure Your httpd-ssl.conf File

Open your `D:\wamp64\bin\apache\apache2.4.46\conf\extra\httpd-ssl.conf` (the drive should be where your wamp is installed) and change the all following lines.

DocumentRoot "${INSTALL_DIR}/www"
ServerName localhost:443
ServerAdmin admin@example.com
SSLCertificateKeyFile "${SRVROOT}/conf/key/private.key"
SSLCertificateFile "${SRVROOT}/conf/key/certificate.crt"

Make sure, these following all lines are set or not. if not, add it as well.

SSLSessionCache "shmcb:${SRVROOT}/logs/ssl_scache(512000)"
CustomLog "${SRVROOT}/logs/ssl_request.log" \
          "%t %h %{SSL_PROTOCOL}x %{SSL_CIPHER}x \"%r\" %b"

Step 8 Configure a Virtual Host

Hope you have created a virtual host. if not, create a virtual host using virtual host manager which is provided by wamp.

Open an `D:\wamp64\bin\apache\apache2.4.46\conf\extra\httpd-vhosts.conf` and update your virtual host

Change the  port *:80 to *:443

add following lines into the VirtualHost.

SSLEngine on
SSLCertificateFile "${SRVROOT}/conf/key/certificate.crt"
SSLCertificateKeyFile "${SRVROOT}/conf/key/private.key"

Now, the code of VirtualHost looks like, 

Now, we are done. Let's restart a wamp server.

If you see a green WAMP icon everything should be right. If the icon is orange there is a problem with your syntax somewhere. 

Open terminal and go to the `D:\wamp64\bin\apache\apache2.4.46\bin` and run `httpd -t` in the command prompt and if there are any syntax errors they will be listed.

if fine then open `https://ladumor.test` on the browser

January 30, 20213 minutesauthorShailesh Ladumor
post

We used Amazon SNS into one of our project where we need to receive instant updates to the various different microservices from other microservices. I was not able to find any good article which highlights everything about SNS integration into PHP or Laravel. This can be most confusing who is working with SNS for the first time. so I decided to write one which can help others. Following things are really important before we get started.

  • What exactly SNS is?
  • Where/When you can use it?
  • How does it work?
  • How to integrate it with PHP?

I will try to explain in a little bit brief here about SNS and will focus more on integration and problem that we tried to solve.

1. What exactly AWS SNS is?

AWS SNS stands for Amazon Simple Notification Service. It's a distributed pub/sub messaging system for microservices. Where publisher can publish a message to different subscribers with various kind of subscribers including SMS, HTTP Endpoints or Webhooks, Amazon SQS, Lambda, Mobile Push etc. We will cover HTTP only subscribers in this post.

Kind of pub/sub where the publisher can publish a message and multiple interested subscribes can subscribe to that. You can read more on AWS Website Here.

2. When you need AWS SNS?

When your project is distributed into various microservices and if you need to communicate from one or multiple microservices to other microservices then SNS comes to play a really good role.

Our Problem:

In our case, our project contains 3 different systems, where authentication + user management is handled by one central system. But each system has its local users table, where minimal users data is cached for fast data retrieval and table joins. so it doesn't need to query the central user system for each and every query.

If any new user is created by Admin on a central user management system, we need to notify other systems that new user is created and they update their local users table.

3. How does it work?

Amazon SNS provides topics. You need to create a Topic and then define subscribers either programmatically or manually from Amazon Console. It depends on your use case. If you want dynamically add subscribers then go that way or if you have some fixed microservices then you can define it from console directly. This process works as follows:

  1. Create a Topic int AWS Console
  2. Create an HTTP subscriber
  3. SNS will send a SubscriptionConfirmation message to your HTTP Webhook
  4. Your microservice needs to call SubscribeURL
  5. Your subscription will be confirmed

This is the initial setup process. Once this is done. Everytime when any new message is published to Topic, it will call your webhook with that Message.

4. How to integrate it with PHP (Or any other PHP Frameworks like Laravel)?

Amazon has a very good PHP Library for their AWS products. Recently for SNS they have created another light-weight library to handle SNS Messages.

So we will use the following libraries in our integration.

Solution:

I will try to highlight the solution that we used to fix the above-mentioned problem. Check the following diagram:

 

As you can see, we set up and SNS Topic and created two HTTP/HTTPS subscribers for two different microservices. When an admin user creates new user into the system, we publish a message to SNS Topic which sends an update to both different microservices.

Now, let's jump into the code.

Publishing a message:

You need to add aws/aws-sdk-php into your project. You can find installation steps on Github Repo. Also, you need to be familiar with AWS authentication process. These things are explained pretty well here. Collect all the things you need in terms of credentials.

  • Key
  • Secret
  • Region
  • Topic ARN

Following code hooked up into our central auth system. You can do this in from wherever you want to publish your message. Create a client and then publish a message. You can pass two things into SNS Message. Subject & Message body. Message body got several options. We will use pure JSON string way for simplification.

use Aws\Credentials\Credentials;
use Aws\Sns\SnsClient;

$client = new SnsClient([
    'version' => '2010-03-31',
    'region' => $amazonRegion,
    'credentials' => new Credentials(
        $awsKey,
        $awsSecret
    )
]);

$subject = 'You got a new SNS Message';

$message = json_encode(['message' => 'this is my first message via SNS Topic']);

$client->publish([
    'TopicArn' => 'your-sns-topic-arn-here',
    'Message' => $message,
    'Subject' => $subject
]);

Generally, in our integration, what we did was, we used Subject to specify the type of event. Like Users.create, Users.update, Users.delete and Message used to contain user information. You can customize it based on your use-case.

That's it. Your message is published to a topic.

Handle Incoming SNS Message:

To handle SNS messages we need to integrate aws/aws-php-sns-message-validator into our project.

SNS will call our webhook for multiple kinds of events. It comes with Type param into the message body.

  • SubscriptionConfirmation
  • Message
  • UnsubscriptionConfirmation

Based on the Type parameter, we need to take relevant action. We have used the following code into our microservice webhook handlers.

use Aws\Sns\Message;
use Aws\Sns\MessageValidator;

try {
    // Retrieve the message
    $message = Message::fromRawPostData();

    // make validator instance
    $validator = new MessageValidator();

    // Validate the message
    if ($validator->isValid($message)) {
        if ($message['Type'] == 'SubscriptionConfirmation') {
            // if it's subscription or unsubscribe event then call SubscribeURL
            file_get_contents($message['SubscribeURL']);
        } elseif ($message['Type'] === 'Notification') {
            $subject = $message['Subject'];
            $messageData = json_decode($message['Message']);
            // use $subject and $messageData and take relevant action
        }
    }
} catch (Exception $e) {
    // Handle exception
}

As you can see, based on Type, we are performing different operations.

This way, all our microservice can communicate in very effective and highly available way to each other. Hope this helps :).

February 23, 20195 minutesauthorMitul Golakiya