PHP, a powerful scripting language for web development, is a cornerstone for many dynamic websites and web applications. If you’re setting up a development environment on Ubuntu Linux, installing PHP on Ubuntu is a crucial step. This guide will walk you through the process, explaining each step in detail and offering options for both Apache and Nginx web servers.
Understanding Your Needs
Before diving into the installation, it’s essential to consider your specific requirements. Here are some key factors to ponder:
- PHP Version: Ubuntu repositories typically include a stable, but not always the latest, version of PHP. If you need a cutting-edge version, you’ll need to add an external repository like Ondřej Surý’s PPA.
- Web Server: While PHP can function independently, it’s often used in conjunction with a web server like Apache or Nginx to handle web requests and deliver content. Choose the server that best suits your project needs and expertise.
- Additional Modules: PHP offers a vast array of modules for various functionalities. Identify any modules essential for your project during installation.
Understanding the Options: Apache vs. Nginx
Before diving into installation, let’s understand the two popular web server choices:
- Apache: This widely used, open-source web server offers a robust feature set and is known for its stability and ease of configuration.
- Nginx: Known for its efficiency and speed, Nginx is a powerful web server that excels in handling high traffic volumes.
Choosing the right web server depends on your specific needs:
- If you prioritize ease of use and a comprehensive feature set, Apache might be a better fit.
- If performance and handling high traffic are your primary concerns, Nginx is a strong contender.
This guide will provide instructions for both Apache and Nginx, allowing you to select the option that aligns with your project requirements.
Prerequisites
- Ubuntu System: This guide assumes you have a running Ubuntu system with root or sudo privileges.
- Terminal Access: We’ll be using the terminal for installation and configuration.
How to Install PHP on Ubuntu Linux
Option 1: Install PHP with Apache Support
Step 1: Update Package Lists
Before installing any software, it’s good practice to update the package lists to ensure you’re working with the latest versions. Open your terminal and run the following command:
sudo apt update
sudo apt upgrade
Step 2: Install PHP package:
Use the following command to install the base PHP package:
sudo apt install php libapache2-mod-php
Step 3: Install Additional PHP Modules (Optional):
The base PHP package provides core functionality. You can install additional modules based on your application needs. Here are some common examples:
php-mysql
– Enables interaction with MySQL databases.php-cgi
– Enables execution of PHP scripts using CGI (Common Gateway Interface).php-mbstring
– Provides multi-byte string handling for character encodings.php-curl
– Enables fetching data using URL.
You can install multiple modules with a single command, separated by spaces:
sudo apt install php-mysql php-cgi php-mbstring php-curl
Step 4: Verify PHP Installation:
Once the installation is complete, verify it by running the following command:
php -v
This should display the installed PHP version and configuration information.
The exact output of the php -v
command will vary depending on the specific version of PHP installed on your system. However, it will generally follow this format:
PHP 8.1.12 (cli) (built: Dec 3 2023 00:35:03 +0300) (NTS)
Copyright (c) 2023 The PHP Group
Zend Engine v4.1.12 (internal build 20231203 003503, VC14 x64)
Here’s a breakdown of the elements:
PHP <version> (cli) (built: <date> <time> <additional information>)
Copyright (c) <year> The PHP Group
Zend Engine v<version> (<compilation mode>)
<version>
: This represents the installed PHP version (e.g., 7.4.3, 8.1.12).<date>
and<time>
: This shows the date and time the PHP binary was built.<additional information>
: This section might contain details like thread safety or API version information.<year>
: This indicates the copyright year for the PHP Group.<compilation mode>
: This specifies how the Zend Engine was compiled (e.g., with VC14 for Visual Studio 2015).
Step 5: Configure Apache for PHP (if using Apache)
Enable PHP Module for Apache:
By default, the PHP module might not be automatically enabled for Apache. Use the following command to activate it:
sudo a2enmod php
Restart Apache:
After enabling the module, restart Apache for the changes to take effect:
sudo systemctl restart apache2
Option 2: Install PHP with Nginx Support
Important Note: If you plan to use Nginx as your primary web server, it’s recommended to not install the default php
package, as it might configure Apache by default.
Step 1: Install PHP-FPM:
Nginx typically uses PHP-FPM (FastCGI Process Manager) to handle PHP requests. Install it with the following command:
sudo apt install php-fpm
Step 2: Install Additional PHP Modules (Optional):
Similar to Apache, install any additional PHP modules you need using apt install
with the desired module names.
Step 3: Verify PHP Installation:
The verification process remains the same. Use php -v
to confirm the installed PHP version.
php -v
Step 4: Configure Nginx for PHP-FPM (if using Nginx)
Create a Server Block Configuration:
Create a new server block configuration file for your website. For example, you can name it your-website.conf
and place it in the /etc/nginx/conf.d
directory. Use a text editor like nano to create the file:
sudo nano /etc/nginx/conf.d/your-website.conf
Configure the Server Block:
In the configuration file, add the following content, replacing your_domain
with your actual domain name and /path/to/your/website
with the directory containing your website files:
server {
listen 80;
listen [::]:80;
server_name your_domain; # Replace with your actual domain name
root /var/www/html;
# Add index.php to setup Nginx, PHP & PHP-FPM config
index index.php index.html index.htm index.nginx-debian.html;
location / {
# Consider customizing the try_files directive for specific error handling
try_files $uri $uri/ =404;
}
# pass PHP scripts on Nginx to FastCGI (PHP-FPM) server
location ~ \.php$ {
include snippets/fastcgi-php.conf;
# Adjust socket path based on your PHP version
fastcgi_pass unix:/run/php/php8.1-fpm.sock;
}
# deny access to Apache .htaccess on Nginx with PHP,
# if Apache and Nginx document roots concur
location ~ /\.ht {
deny all;
}
}
Test Nginx Configuration:
Before restarting Nginx, test the configuration for syntax errors:
sudo nginx -t
If there are no errors, restart Nginx to apply the changes:
sudo systemctl restart nginx
Step 5: Test PHP with Your Web Server (Apache or Nginx)
Create a Test PHP File:
Create a file named info.php
in your website directory (e.g., /path/to/your/website
) with the following content:
<?php
phpinfo();
?>
Access the Test Script:
Open a web browser and navigate to http://your_domain/info.php
(replace your_domain
with your actual domain name). You should see the PHP information page displayed, confirming a successful PHP installation and configuration.
How to Install The Latest Version of PHP on Ubuntu Linux
The default method using the Ubuntu repositories might not provide the absolute latest version of PHP. Here’s how to install the latest version of PHP on Ubuntu using Ondřej Surý’s PHP repository:
Step 1: Update Package Lists
As always, update your package lists before installing anything new:
sudo apt update
Step 2: Add Ondřej Surý’s PHP PPA
Ondřej Surý’s PPA (Personal Package Archive) provides various PHP versions for Ubuntu. Add the repository for the desired PHP version (e.g., PHP 8.2) using the following command:
sudo add-apt-repository ppa:ondrej/php8.2 # Replace 8.2 with your desired version
Important Note: Double-check the available versions on https://launchpad.net/~ondrej/+archive/ubuntu/php before adding the repository.
Step 3: Update Package Lists Again
After adding the PPA, update the package lists to include the new repository:
sudo apt update
Step 4: Install PHP
sudo apt install php8.2 # Replace 8.2 with your desired version
This will install the core PHP package along with some common modules. You can install additional modules as needed using apt install
followed by the desired module names (e.g., php8.2-mysql
).
Step 5: Install Additional PHP Modules
Similar to the previous guide, you can install additional modules based on your needs:
sudo apt install php8.2-mysql php8.2-cgi php8.2-mbstring php8.2-curl # Replace with desired modules
Step 6: Verify PHP Installation
Confirm the installation by running:
php -v
This should display the installed PHP 8.2 (or whichever version you chose) and its configuration details.
Conclusion
Following these steps, you’ll have successfully installed PHP on your Ubuntu server and configured it to work with either Apache or Nginx. With this foundation, you can start developing and deploying PHP-based web applications!