Open In App

How to deploy PHP apps into Cloud ?

Last Updated : 26 Sep, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

PHP is a server-side scripting language that can be used for developing web applications or websites. In this article, we will see how to deploy the PHP applications in the Cloud. To deploy PHP apps on the cloud, we can use any cloud service provider. Here, we will be using the AWS EC2 Instance based on Ubuntu 18.04 LTS. Below are the required steps to deploy any PHP application in detail.

Steps to deploy PHP App on Cloud:

Step 1: Create and Launch Cloud Instance and Select Network access to Open i.e accessible by all and allow HTTP, and HTTPS traffic so we can use the PHP app on the web.

Network Settings

Step 2: Go to the folder where the downloaded key file is stored. (Downloaded when the instance was created) & Press Shift +Right Click, then select Open in Terminal.

Open The Terminal to connect to Cloud Instance

Step 3: Type this command in the given format  

< ssh -i "key.pem" user@dns >

The DNS and user details to connect can be found in Connect Menu of Instance.

Enter the SSH command to connect to the Cloud instance.

Step 4: Now, Run the following commands:

sudo apt-get update

Update The Packages installed in Cloud Machine.

sudo apt-get install apache2

Install Apache2.

sudo apt-get install php libapache2-mod-php

Install PHP Library for Apache2 Server.

sudo service apache2 restart

Restart Apache2 Server.

Step 5: Now, PHP is ready to use, go to directory /var/www/html.

Traverse to Project Directory

Step 6: Run command the below command:

sudo rm index.html

This will delete the default HTML page of the apache.

Delete Default File.

Step 7: Run Command the below command:

sudo chmod 777 /var/www/html

This gives write and read full permissions to the current user.

Now, create file index.php using the command “cat > index.php” & paste the code of your PHP app into the terminal after running the command. Here, we are creating a simple Calculator-based PHP Application. After pasting the code in terminal, press Enter and then Ctrl + D, which will save the file.

Create New Project File and Paste The Code.

Example: In this example, we have created a simple calculator app with basic arithmetic operations in PHP.

PHP




<!DOCTYPE html>
 
<head>
    <title>
          Calculator App in PHP
      </title>
</head>
<?php
    $first= $_POST['first'];
    $second= $_POST['second'];
    $operator = $_POST['operator'];
    $result = '';
    if (is_numeric($first) && is_numeric($second)) {
        switch ($operator) {
            case "+":
               $result = $first + $second;
                break;
            case "-":
               $result = $first - $second;
                break;
            case "*":
                $result = $first * $second;
                break;
            case "/":
                $result = $first / $second;
        }
    }
     
?>
 
<body>
    <div id="page-wrap">
        <h1>Calculator in PHP</h1>
        <form action=""
              method="post"
              id="calculator">
            <p>
                <input type="number"
                       name="first"
                       id="first"
                       required="required"
                       value="<?php echo $first; ?>" />
                    <b>Enter First Value</b>
            </p>
            <p>
                <input type="number"
                       name="second"
                       id="second"
                       required="required"
                       value="<?php echo $second; ?>" />
                    <b>Enter Second Value</b>
            </p>
            <p>
                <input readonly="readonly"
                       name="result"
                       value="<?php echo $result; ?>">
                    <b>Result</b>
            </p>
                <input type="submit"
                       name="operator"
                       value="+" />
                <input type="submit"
                       name="operator"
                       value="-" />
                <input type="submit"
                       name="operator"
                       value="*" />
                <input type="submit"
                       name="operator"
                       value="/" />
        </form>
    </div>
</body>
</html>


Step 8: Enter the public IP Address or Public DNS of the EC2 instance and you will see the PHP app is deployed.

Go to Public Address of Cloud and Access the PHP App.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads