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

In this tutorial, you use an Oracle Cloud Infrastructure Free Tier account to set up an Oracle Linux instance. Then, you install an Apache web server and PHP and access your new server from the internet.

This tutorial covers all the steps necessary to set up a virtual network for your host and connect the host to the internet. Key tasks include how to:

  • Set up a compartment for your development work.
  • Install your Oracle Linux instance and connect it to your Virtual Cloud Network (VCN).
    • Set up an Oracle Cloud Infrastructure virtual cloud network and related network services required for your host to connect to the internet.
    • Set up ssh encryption keys to access your Oracle Linux Server.
  • Configure ingress rules for your VCN.
  • Configure Apache and PHP on your instance.
  • Connect to your instance from the internet.

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

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

For additional information, see:

Before You Begin

To successfully complete this tutorial, you must have the following:

Requirements
  • An Oracle Cloud Infrastructure Free Tier account. Start for Free.
  • A MacOS, Linux, or Windows computer with ssh support installed.

1. Set up a Compartment for Development

Configure a compartment for your development.

Create a Compartment

Create a compartment for the resources that you create in this tutorial.

  1. Log in to the Oracle Cloud Infrastructure Console.
  2. From the navigation menu, in the top navigation bar, go to Governance and Administration, then Identity, and select Compartments.
  3. Click Create Compartment.
  4. Fill in the following information:
    • Name: <your-compartment>
    • Description: Compartment for <your-description>.
    • Parent Compartment: <your-tenancy>(root)
  5. Click Create Compartment.
Reference

Create a compartment

2. Install your Oracle Linux Instance

Use the Create a VM Instance wizard to create a new compute instance.

The wizard does several things when installing the instance:

  • Creates and installs a compute instance running Oracle Linux.
  • Creates a VCN with the required subnet and components needed to connect your Oracle Linux instance to the internet.
  • Creates an ssh key pair you use to connect to your instance.
Review Installation Steps

To get started installing your instance with the Create a VM Instance wizard, follow these steps:

  1. From the main landing page, select Create a VM Instance wizard. Quick action menu from the main Free Tier landing page

    The Create Compute Instance is displayed. It has a section for Configure placement and hardware, Configure networking, Add SSH keys, and Configure boot volume.

  2. Choose the Name and Compartment.

    Initial Options

    • Name: <name-for-the-instance>
    • Create in Compartment: <take-default>

    Enter a value for the name or leave the system supplied default. For compartment, select the default root compartment.

  3. Review the Configure placement and hardware settings. Take the default values provided by the wizard.
    Note

    The following is sample data. The actual values change over time or differ in a different data center.

    Placement

    • Availability Domain: AD-1
    • Fault Domain: Oracle chooses the best placement.

    Image

    • Image: Oracle Linux 7.9
    • Image build: 2020.11.10-1

    Shape

    • Shape: VM.Standard.E2.1.Micro
    • OCPU Count: 1
    • Memory (GB): 1
    • Network Bandwidth (Gbps): 0.48
    Note

    For Free Tier, use Always Free Eligible options for availability domain and shape.
  4. Review the Configure networking settings. Take the default values provided by the wizard.
    Note

    The following is sample data. The actual values change over time or differ in a different data center.
    • Virtual Cloud Network: vcn-<date>-<time>
    • Subnet: vcn-<date>-<time>
    • Use network security groups to control traffic: No
    • Assign a public IPv4 address: Yes

    If you want to change the values, click the Edit in the upper right of the dialog.

  5. Review the Add SSH keys settings. Take the default values provided by the wizard.
    • Select the Generate SSH Key Pair.
    • Click the Save Private Key and Save Public Key to save the private and public SSH keys for this compute instance.

    If you want to use your own SSH keys, select one of the options to provide your public key.

    Note

    Put your private and public key files in a safe location. You cannot retrieve keys again after the compute instance has been created.
  6. Review the Configure boot volume settings. Take the default values provided by the wizard.

    Leave all check boxes unchecked.

  7. Click Create to create the instance. Provisioning the system might take several minutes.
You have successfully created an Oracle Linux instance to run your Apache Web Server.

3. Enable Internet Access

The Create a VM Instance wizard automatically creates a VCN for your VM. You add an ingress rule to your subnet to allow internet connections on port 80.

Create an Ingress Rule for your VCN

Follow these steps to select your VCN's public subnet and add the ingress rule.

  1. From the main menu, select Networking then select Virtual Cloud Networks.
  2. Select the VCN you created with your compute instance.
  3. With your new VCN displayed, click <your-subnet-name> subnet link.

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

  4. Click the Default Security List link.

    The default Ingress Rules for your VCN are displayed.

  5. Click Add Ingress Rules.

    An Add Ingress Rules dialog is displayed.

  6. Fill in the ingress rule with the following information.

    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
  7. Click Add Ingress Rule.
    Now HTTP connections are allowed. Your VCN is configured for Apache server.
You have successfully created a VCN that makes your instance available from the internet.

4. Install and Configure Apache and PHP

With your compute instance and VCN setup, install, and configure Apache web server and PHP to run on your Oracle Linux instance.

Install and Configure Apache

With your compute instance and VCN setup, install, and configure Apache web server on your Oracle Linux instance.

Follow these steps to configure your Oracle Linux instance.
  1. From the main menu, select Compute then Instances.
  2. Click the link to the instance you created in the previous step.

    From the Instance Details page look under the Instance Access 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 instance 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 instance, this command logs you into your instance. You can now issue sudo commands to install and start your server.

  6. Install Apache Server.
    sudo yum install -y httpd
  7. Enable Apache.
    sudo systemctl enable httpd

    The command returns:

    Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
    
  8. Start Apache.
    sudo systemctl restart httpd
  9. Enable HTTP connection through port 80.
    sudo firewall-cmd --add-service=http --permanent

    The commands returns:

    success
    Note

    If you intend to set up HTTPS later (recommended), also enable HTTPS connections on port 443:
    sudo firewall-cmd --add-service=https --permanent
  10. Reload the firewall.
    sudo firewall-cmd --reload
  11. 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 the public IP address assigned to your instance: http://<x.x.x.x>.

    Apache Server Test Page
    Note

    Get the IP address from your compute instance details page. From the main menu, select Compute then Instances. Select the compute instance your created. The IP address is listed under the Public IP Address field.
Congratulations! You have successfully installed Apache on your Oracle Cloud Infrastructure instance.
Install and Configure PHP

Install and configure PHP to run on your Oracle Linux instance.

  1. Configure the Oracle Linux package repos to use PHP 7.
    sudo yum install -y oracle-php-release-el7

    After the command is run, yum installs PHP 7 by default rather than PHP 5.

  2. Install PHP 7.
    sudo yum install -y php

    The command installs php, php-cli, and php-common.

  3. Restart Apache.
    sudo systemctl restart httpd
  4. Verify installation.
    php -v

    The command returns text similar to the following:

    PHP 7.4.13 (cli) (built: Nov 25 2020 21:10:33) ( NTS )
    Copyright (c) The PHP Group
    Zend Engine v3.4.0, Copyright (c) Zend Technologies
  5. Add a PHP test file to your instance.

    Create the file:

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

    The browser produces a listing of PHP configuration on your instance.

    PHP configuration Page
    Note

    After you are done testing, remove info.php from your system.
Congratulations! You have successfully installed Apache and PHP 7 on your Oracle Cloud Infrastructure instance.

What's Next

You have successfully installed and deployed an Apache web server on Oracle Cloud Infrastructure using a Linux instance.

To explore more information about development with Oracle products: