Programming is one of the desired skills in the tech world today. Did you know that all the applications and systems around you such as Facebook, Twitter, Whatsapp, Netflix e.t.c were carefully coded using different programming languages? Programming can be defined as writing a set of instructions for a computer to execute. These instructions are written in a machine language. There are several programming languages, the most popular ones are Java, JavaScript, Python, Ruby, C, C++, PHP etc.
Hypertext Preprocessor commonly abbreviated as PHP is an open-source general-purpose scripting language. This language is preferred for web development and can also be used with HTML. PHP is a bit different from other languages such as JavaScript in the sense that the codes are executed on the server side thus generating HTML scripts which are then sent to the client. There are several sites that have been developed with PHP, these include OpenCart, Joomla, WordPress, PyroCMS, OctoberCMS, ExpressionEngine, Magento, Contao etc.
PHP is preferred by many people around the world due to its simple code, which newcomers get along with really fast. Moreover, it is flexible, powerful and platform-independent. Today, we will learn how to host PHP applications on Linux with Apache / Nginx Web Server.
1. Install PHP on Linux System
The first step is to ensure that PHP is installed on your Linux system. This page provides you with the best guides on how to install PHP on your system.
- How To Install PHP 8.2 on Rocky Linux 8 / AlmaLinux 8
- How To Install PHP 8.2 on CentOS 7 / RHEL 7
- How To Install PHP 8.2 on Debian
- How To Install PHP 8.2 on Ubuntu
Once the desired PHP version is installed, you can verify with the command:
$ php -v
PHP 8.2.1 (cli) (built: Jan 3 2023 18:40:55) (NTS gcc x86_64)
Copyright (c) The PHP Group
Zend Engine v4.2.1, Copyright (c) Zend Technologies
2. Create PHP application
Once PHP has been installed, you can choose any of the desired web servers to host your application. But first, you need to create a PHP application.
For this guide, we will create the application in the /var/www/html directory.
sudo mkdir -p /var/www/html/php
Now this will act as the directory for our PHP application. Set the desired permissions for the path:
sudo chmod -R 775 /var/www/html/php
Now navigate to this directory and create a sample index.php file as shown:
cd /var/www/html/php
sudo vim index.php
In the file, we will add the PHP script for a simple HelloWorld app:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>PHP - Hello, World!</title>
</head>
<body>
<h1><?php echo 'Hello, World! PHP is Amazing!'; ?></h1>
</body>
</html>
This code looks like a regular HTML document, the only difference is the part with <?PHP and />. The code between the tags is what we want to be executed by PHP.
If try executing this code, we will see an HTML code since the terminal doesn’t know how to render HTML to the web.
$ php index.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>PHP - Hello, World!</title>
</head>
<body>
<h1>Hello, World! PHP is Amazing!</h1>
</body>
</html>
3. Host PHP application with Apache / Nginx Web Server
To be able to view the application on the web, you need a web server. For this guide, I will demonstrate how to host the application with both Nginx and Apache.
Option 1 – Host a PHP application on Linux with Apache
To host a PHP application with Apache, you need to install the web server and all the required modules. This can be done with the command;
##On Debian/Ubuntu
sudo apt install libapache2-mod-php8.2
##On Rhel/Rocky/Alma Linux/CentOS
sudo yum install httpd
Once installed, you can make the desired configurations for your application:
##On Debian/Ubuntu
sudo vim /etc/php/8.2/apache2/php.ini
##On Rhel/Rocky/Alma Linux/CentOS
sudo vim /etc/php.ini
Add the desired configurations to the file:
max_execution_time = 300
upload_max_filesize = 100M
post_max_size = 128M
date.timezone = Africa/Nairobi
On Rhel-based systems, you need to edit the file below to enable the PHP files to be loaded.
$ sudo vim /etc/httpd/conf/httpd.conf
# LoadModule foo_module modules/mod_foo.so
AddHandler php-script .php
Save the file and restart the service:
##On Debian/Ubuntu
sudo systemctl restart apache2
##On Rhel/Rocky/Alma Linux/CentOS
sudo systemctl restart php-fpm httpd
Now we will create a VirtualHost file for the application.
##On Debian/Ubuntu
sudo vim /etc/apache2/sites-available/test.conf
##On Rhel/Rocky/Alma Linux/CentOS
sudo vim /etc/httpd/conf.d/test.conf
Add the lines below to the file:
<Directory /var/www/html/php>
Require all granted
</Directory>
<VirtualHost *:80>
ServerName test.geeksforgeeks.org
ServerAlias www.test.geeksforgeeks.org
ServerAdmin admin@localhost
DocumentRoot /var/www/html/php
</VirtualHost>
On Debian-based, activate the site:
sudo ln -s /etc/apache2/sites-available/test.conf /etc/apache2/sites-enabled/
sudo unlink /etc/apache2/sites-enabled/000-default.conf
Restart the service:
##On Debian/Ubuntu
sudo systemctl restart apache2
##On Rhel/Rocky/Alma Linux/CentOS
sudo systemctl restart php-fpm httpd
Allow the port through the firewall
#For UFW
sudo ufw allow 80
##For Firewalld
sudo firewall-cmd --add-port=80/tcp --permanent
sudo firewall-cmd --reload
Now access the site using the URL http://IP_Address
Option 2 – Host a PHP application on Linux with Nginx
It is also possible to host the application using Nginx. First, install the web server on your system.
##On Debian/Ubuntu
sudo apt install nginx php8.2-fpm
##On Rhel/Rocky/Alma Linux/CentOS
sudo yum install nginx php-fpm
Once Nginx and the appropriate PHP-FPM module, you can make the desired PHP configurations.
##On Debian/Ubuntu
sudo vim /etc/php/8.2/fpm/php.ini
##On Rhel/Rocky/Alma Linux/CentOS
sudo vim /etc/php.ini
In the file, add the lines:
upload_max_filesize = 20M
short_open_tag = On
memory_limit = 256M
max_execution_time = 360
date.timezone = Africa/Nairobi
Save the file and restart the service:
sudo systemctl restart nginx php*-fpm.service
Now we will create a virtual host file for Nginx:
##On Debian/Ubuntu
sudo vim /etc/nginx/sites-available/test.conf
##On Rhel/Rocky/Alma Linux/CentOS
sudo vim /etc/nginx/conf.d/test.conf
Add the lines below to the file.
server {
listen 80;
server_name test.geeksforgeeks.org;
root /var/www/html/php;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_index index.php;
fastcgi_pass unix:/run/php-fpm.www.sock;
}
}
On Debian-based, replace /run/php-fpm.www.sock with /run/php/php8.2-fpm.sock. Save the file then enable the site:
sudo ln -s /etc/nginx/sites-available/test.conf /etc/nginx/sites-enabled/
sudo unlink /etc/nginx/sites-enabled/default
Restart the services.
sudo systemctl restart nginx php*-fpm.service
Allow HTTP, through the firewall and access the application using the URL http://IP_Address
Recommended books to read:
- Best Books to learn Web Development – PHP, HTML, CSS, JavaScript and jQuery
- Best Books To Master Web Design
- Best Books To Learn CSS & CSS3
- Best Books To Learn HTML & HTML5
- Best Apache and Nginx reference Books
Verdict
That marks the end of this guide on how to host PHP Applications on Linux with Apache / Nginx Web Server. I hope this was helpful.
See more on this page: