Until now we have been able to use Node.js are our web server. This is because our web server wasn’t very complicated. However, as we prepare to move into more advanced topics in Oracle Cloud Infrastructure, we age going to be a better platform for hosting our web pages. Since Apache HTTPD currently enjoys about 60% of the web server market share, it makes sense that we use Apache as our web server. In addition, we will use PHP with Apache to handle our need for dynamic web pages. In this article I will cover the following:
- Stopping Our Current Node Web Servers
- Installing Apache Web Server
- Configuring our OCI environment
- Installing PHP for Apache
Stopping Our Current Node Web Servers
We need to stop the existing web servers running on port 3000. To do this we will need to turn our and disable our existing node.js web servers that listen to that address and auto-start when the server is rebooted.
SSH into each of your compute instances and perform the following commands:
sudo systemctl disable starthelloserver.service
sudo rm starthelloserver.service
The next time we reboot the server, the helloServer won't be started. This makes room for us to autostart our new Apache web server instead.
Installing Apache Web Server
To install the Apache software, we will need to SSH to each of our 2 compute instances that currently hosts our Node.js web servers. The installation command is quite simple:
sudo yum install httpd -y
After that, we need to ensure that the server starts by default if the compute instance restarts. The following command will take care of that:
sudo systemctl enable httpd
Let's also force the start of the Apache server with the following command:
sudo systemctl restart httpd
Be sure to do this for both of your compute instances. At this point, Apache is installed and running. Now we just need to configure OCI to allow web access to it through the load balancer.
Configuring our OCI Environment
Before we can access the web server through the load balancer, we need to create a backend set that listens to port 80 from the load balancer. Create a new backend set with the following attributes:
Traffic Distribution Policy: Weighted Round Robin
Enable Load Balancer Cookie Persistence: Selected
Cookie Name: userinfo
Domain Name: The domain name you used for your SSL certificate
Attributes — Secure: Checked
Attributes — HTTP Only: Checked
Interval in MS: 300000 (5 minutes)
Timeout in MS: 5000
Number of Retries: 3
Status Code: 200
URL Path (URI): /
Response Body Regex: leave blank
Now we need to add the specific backend servers to our backend set. Locate the link for the http80 backend set that you just created and add the backends.
Now we can edit our listener for the load balancer and tell it to use this new backend set instead of the old one. Edit your current listener:
And change the Backend Set to the new http80 backend set.
Now SSH to each of your instances and open port 80 in the firewall with the following commands:
sudo firewall-cmd --permanent --add-port=80/tcp
We may as well get rid of the old 3000 port while we are here.
sudo firewall-cmd --permanent --remove-port=80/tcp
Now let's activate these changes:
sudo firewall-cmd --reload
Now you should be able to open your browser to
https://<your domain name> and see the Apache home page:
Installing PHP for Apache
This follows the standard process for installing PHP for Apache.
sudo yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpmsudo yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpmsudo yum install yum-utilssudo yum-config-manager --enable remi-php73sudo yum install php php-mcrypt php-cli php-gd php-curl php-mysql php-ldap php-zip php-fileinfo
Be advised that many of these commands will require your acceptance by typing in a
y in the terminal. Now you can check the version of PHP that has been installed from the command line:
Which should show you the following:
Testing the PHP Installation
Using the ssh connection to each of your server instances, navigate to /var/www/html and create a file named info.php using the following command:
sudo vi /etc/www/html/info.php
Edit the file to contain the following:
Save the file and do the same thing in your other compute instance. Now you can point your web browser to
https://<your domain name>/info.php and see the details of your Apache and PHP installation:
Hopefully, this helped demystify the process for running PHP on OCI. Naturally, if we had a larger number of web server to manage, making these changes manually would be wasteful. A better approach at scale would be to create an image of one server that is fully configured, and use that image to create new compute instances and add them to the backed set of the load balancer.
In my next article, I will show you how to create a simple PHP website that leverages OCIs built-in Identity Cloud Service (IDCS) to help you control access to your website contents by requiring users to log into the web site.
As always, I hope you enjoyed this article and found it useful. If so please give me a clap and as always, feel free to ask any questions.