Free Tier: Install Apache and PHP on an Oracle Linux VM

In this tutorial, you use an Oracle Cloud Infrastructure Free Tier account to setup an Oracle Linux 7.7 virtual machine (VM). Then, you install an Apache web server and PHP and access your new server from the internet. Finally, this tutorial covers all the steps necessary to setup a virtual network for your host and connect the host to the internet. Key tasks include how to:

  • Setup ssh encryption keys to access your Oracle Linux Server.
  • Setup an Oracle Cloud Infrastructure virtual cloud network (VCN) and related network services required for your host to connect to the internet.
  • Install your Oracle Linux VM and connect it to your VCN.
  • Configure Apache and PHP 7 on your VM.
  • Connect to your VM from the internet.

Here is a simplified diagram of the setup for your Linux VM.

A diagram of the components needed to run a Apache webserver on Oracle Cloud Infrastructure

For additional information, see:

1. Create SSH Encryption Keys

Create ssh encryption keys you can use to log into your VM.
  • If you are using MacOS or Linux, open a terminal window in the directory where you want to store your keys, and issue the following OpenSSH command:
    ssh-keygen -t rsa -N "" -b 2048 -C <my-key> -f <my-key>

    The command generates some random text art used to generate the keys. When complete, you should have two files:

    • The private key file: <my-key>
    • The public key file: <my-key>.pub

    You use these files to connect to your Compute instance.

  • On Windows, open the puttygen.exe key generation tool.
    1. At the bottom of the screen, accept the default configuration values:
      • Type of Key: RSA (default, generates a SSH-2 RSA key)
      • Number of bits: 2048
    2. Click Generate.
    3. Move the mouse around the screen to randomly generate a key.

      Once complete, the key information is displayed.

      Note

      Do not set a passphrase for your keys.
    4. Save the following information.
      • From the Public key text box at the top of the dialog, copy the text and save it into a file named <your-key-name>.pub. Use this file when you configure ssh for your VM.
      • Click Save Public Key. Name the file <your-key-name>.pem This file is a different format than the previous step. Some tools prefer the public key in this PEM format.
      • Click Save Private Key. If prompted, do not set a password. Name the file <your-key-name>. This creates a <your-key-name>.ppk file that is the private key format for putty.

      You use these files to connect to your Compute instance.

You have generated the required encryption keys.

Follow this link for more detailed information on generating ssh encryption keys.

2. Create your Virtual Cloud Network (VCN)

Setup a VCN to connect your Oracle Linux instance to the internet. Configure all the components needed to create your virtual network.

  1. From the main landing page, select Set up a network with a wizard. Quick action menu from the main Free Tier landing page
  2. In the Start VCN Wizard workflow, select VCN with Internet Connectivity and then click Start VCN Wizard .
  3. In the configuration dialog, fill in the VCN Name for your VCN. Your Compartment is already set to its default value of <your-tenancy> (root).
  4. In the Configure VCN and Subnets section, keep the default values for the CIDR blocks:
    • VCN CIDR BLOCK: 10.0.0.0/16
    • PUBLIC SUBNET CIDR BLOCK: 10.0.0.0/24
    • PRIVATE SUBNET CIDR BLOCK: 10.0.1.0/24
    Note

    Notice the public and private subnets have different network addresses.
  5. For DNS RESOLUTION uncheck USE DNS HOSTNAMES IN THIS VCN.Picture shows the USE DNS HOSTNAMES IN THIS VCN option is unchecked.
  6. Click Next.

    The Create a VCN with Internet Connection configuration dialog is displayed (not shown here) confirming all the values you just entered.

  7. Click Create to create your VCN.

    The Creating Resources dialog is displayed (not shown here) showing all VCN components being created.

  8. Click View Virtual Cloud Network to view your new VCN.

    Your new VCN is displayed. Now you need to add a security rule to allow HTTP connections on port 80.

  9. With your new VCN displayed, click on your Public subnet link.

    The public subnet information is displayed with the Security Lists at the bottom of the page. There should be a link to the Default Security List for your VCN.

  10. Click the Default Security List link.

    The default Ingress Rules for your VCN are displayed.

  11. Click Add Ingress Rules.

    An Add Ingress Rules dialog is displayed.

  12. Fill in the ingress rule with the following information. Once all the data is entered, click Add Ingress Rules.

    Fill in the ingress rule as follows:

    • Stateless: Checked
    • Source Type: CIDR
    • Source CIDR: 0.0.0.0/0
    • IP Protocol: TCP
    • Source port range: (leave-blank)
    • Destination Port Range: 80
    • Description: Allow HTTP connections

    Once you click Add Ingress Rule, HTTP connections are allowed. Your VCN is configured for Apache server.

You have successfully created a VCN that your VM available from the internet.

3. Install your Oracle Linux VM

Install your Oracle Linux VM to host your Apache web server.
  1. Open the Oracle Cloud Infrastructure main menu.
  2. Select Compute then Instances.
  3. From the list of instances screen click Create Instance.

    The Create Compute Instance dialog is displayed. Notice the Show Shape, Network and Storage Options should be expanded to configure the virtual machine.

  4. Fill in the fields for the Create Compute Instance dialog with the following data:

    Initial Options

    • Name of your Instance: <name-for-the-instance>
    • Choose an Operating System: Oracle Linux 7.7
    • Availability Domain: <select-a-domain>
    • Instance Shape: VM.Standard.E2.1.Micro: Virtual Machine, 1 core OCPU, 1 GB Memory, 0.48 Gbps network bandwidth (the default)

    Configure Networking

    • VIRTUAL CLOUD NETWORK COMPARTMENT: (take default)
    • VIRTUAL CLOUD NETWORK: <VCN-you-created>
    • SUBNET COMPARTMENT: (take default)
    • SUBNET: <public-subnet-you-created>
    • USE NETWORK SECURITY GROUPS TO CONTROL TRAFFIC: Unchecked
    • ASSIGN A PUBLIC IP ADDRESS: Selected/Checked

    Additional Options

    • Boot Volume: All options Unchecked
    • Add SSH Keys: Add the public key file you created in part 1 of this tutorial.
  5. Click Create to create the instance. Provisioning the system may take several minutes.
You have successfully created an Oracle Linux VM to run your Apache Web Server.

4. Install and Configure Apache and PHP

Next, install and configure Apache web server and PHP to run on your Oracle Linux VM.
  1. From the main menu select Compute then Instances.
  2. Click on the link to the instance you just created in the previous step.

    From the Instance Details page look under the Primary VNIC Information section. Write down the public IP address the system created for you. You use this IP address to connect to your instance.

  3. Open a Terminal or Command Prompt window.
  4. Change into the directory where you stored the ssh encryption keys you created in part 1.
  5. Connect to your VM with this ssh command
    ssh -i <your-private-key-file> opc@<x.x.x.x>

    Since you identified your public key when you created the VM, this command should log you into your VM. You can now issue sudo commands to install and start your server.

  6. Install Apache Server.
    sudo yum install httpd
    Note

    You may need to type "y" a few times to accept the packages that are installed to the VM.
  7. Next enable and start Apache.
    $ sudo systemctl enable httpd
    Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
    $ sudo systemctl restart httpd
  8. Enable HTTP connection via port 80.
    $ sudo firewall-cmd --permanent --add-port=80/tcp
    success
    $ sudo firewall-cmd --reload
  9. You can now test your server.

    You can test your server from the command line with curl localhost. Or, you can connect your browser to your the public IP address assigned to your VM: http://<x.x.x.x>.

    Apache Server Test Page
  10. Install PHP 7 with the following series of commands.
    sudo yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
    $ sudo yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm
    sudo yum install yum-utils
    sudo yum-config-manager --enable remi-php73
    sudo yum install php php-mcrypt php-cli php-gd php-curl php-mysql php-ldap php-zip php-fileinfo
    Note

    The last line installs popular PHP modules.
  11. Verify installation and restart Apache.
    $ php -v
    PHP 7.3.15 (cli) (built: Feb 18 2020 09:25:23) ( NTS )
    Copyright (c) 1997-2018 The PHP Group
    Zend Engine v3.3.15, Copyright (c) 1998-2018 Zend Technologies
    $ sudo systemctl restart httpd
  12. Add a PHP test file to your VM.

    Create the file:

    sudo vi /var/www/html/info.php
  13. In the file input the following text and save the file:
    <?php
    phpinfo();
    ?>
  14. Connect to http://<your-public-ip-address>/info.php.

    The browser should produce a listing of PHP configuration on your VM.

    PHP configuration Page
Congratulations! You have successfully installed Apache and PHP 7 on your Oracle Cloud Infrastructure VM.