Free Tier: Install Apache and PHP on an Ubuntu VM

In this tutorial, you use an Oracle Cloud Infrastructure Free Tier account to setup an Ubuntu 18.04 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 Ubuntu 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 Ubuntu 18.04 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.

  1. 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.

  2. On Windows, open the puttygen.exe key generation tool.
    1. At the bottom of the screen, take 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 display.

      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 Ubuntu instance to the internet. You 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:

    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 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 on 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:
    • 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 will make your VM available from the internet.

3. Install your Ubuntu VM

Install your Ubuntu VM to host your Apache web server.

  1. From the Oracle Cloud Infrastructure main menu, select Compute then Instances.
  2. 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.

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

    Initial Options

    • Name of your Instance: <name-for-the-instance>
    • Image or Operating System (Click Change Image): Canonical Ubuntu 18.04
    • 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: <VCN-you-created>
    • SUBNET COMPARTMENT: (take default)
    • SUBNET: <public-subnet-you-created>
    • ASSIGN A PUBLIC IP ADDRESS: Selected/Checked

    Additional Options

    • Boot Volume: All options Unchecked
    • Add SSH Keys: Add the public key file (.pub) you created in part 1 of this tutorial.
  4. Click Create to create the instance.

    Provisioning the system may take several minutes.

You have successfully created an Ubuntu 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 Ubuntu Linux VM.

To install and setup Apache and PHP, perform the following steps:

  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 will 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> ubuntu@<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 apt update
    sudo apt install apache2

    You may need to type "y" a few times to accept the packages that are installed to the VM.
  7. Next start Apache.
    $ sudo systemctl restart apache2
  8. Update firewall settings.

    The Ubuntu firewall is disabled by default. However, it is still necessary to update your iptables configuration to allow HTTP traffic. Execute the following commands to update iptables.

    $ sudo iptables -I INPUT 6 -m state --state NEW -p tcp --dport 80 -j ACCEPT
    $ sudo netfilter-persistent save

    The commands add a rule to allow HTTP traffic and saves the changes to the iptables configuration files.

  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>. The page should look similar to: Apache Server Test Page

  10. Install PHP 7 with the following commands.
    $ sudo apt install libapache2-mod-php7.2
    $ sudo apt install php7.2-curl php7.2-gd php7.2-zip
  11. Verify installation and restart Apache.
    $ php -v
    $ sudo systemctl restart apache2
  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:
  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.

What's Next

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

To explore more information about development with Oracle products check out these sites: