Posted on
Web Development

Installing and configuring PHP on Linux

Author
  • User
    Linux Bash
    Posts by this author
    Posts by this author

Comprehensive Guide to Installing and Configuring PHP on Linux for Web Developers

Linux, with its robust security and outstanding performance, is a favored operating system for many web developers. When paired with PHP, one of the most popular scripting languages used in web development, it forms a powerful platform for building efficient web applications. In this guide, we'll walk through everything you need to know to successfully install and configure PHP on a Linux system.

Step 1: Choose Your Linux Distro

First and foremost, you must decide on a Linux distribution. While Ubuntu, Debian, CentOS, and Fedora are popular choices among developers for their support and ease of use, your specific requirements and preference will guide this choice.

Step 2: Updating Your System

Before installing new packages, it's a good idea to update your system. For Debian-based distributions like Ubuntu, you can use:

sudo apt update && sudo apt upgrade -y

For RPM-based distributions like CentOS or Fedora, use:

sudo yum update

or

sudo dnf update

For openSUSE, you should use zypper:

sudo zypper update

Step 3: Install PHP

Once your system is up-to-date, you can proceed to install PHP. Most Linux distributions include PHP in their official package repositories.

On Ubuntu and other Debian-based distributions:

sudo apt install php

On CentOS:

Enable the EPEL repository, then install PHP using YUM:

sudo yum install epel-release
sudo yum install php

On Fedora:

sudo dnf install php

On openSUSE:

sudo zypper install php7

After installation, you can check the version of PHP installed:

php -v

Step 4: Configure PHP (php.ini)

PHP's configuration file, php.ini, allows you to customize your PHP installation. This file is typically located in /etc/php/7.x/cli/ on Ubuntu systems and similar paths on others, where 7.x is your PHP version.

You might want to make changes to some common settings:

  • max_execution_time: This sets the maximum time in seconds a script is allowed to run before it is terminated by the parser.

  • memory_limit: This sets the maximum amount of memory in bytes a script is allowed to allocate.

  • display_errors: Useful for debugging, it specifies whether errors should be printed to the screen as part of the output or hidden from the user.

Edit these settings using a text editor:

sudo nano /etc/php/7.x/cli/php.ini

Make the necessary changes, for example:

memory_limit = 256M
display_errors = On
max_execution_time = 30

Save and exit the editor.

Step 5: Install Additional PHP Modules

Depending on your development needs, you might require additional PHP modules (e.g., MySQL, GD, XML, mbstring). To install these, you first need to find the appropriate module name and then install it using the package manager. Here’s how you can search and install:

Searching for a PHP Module:

Ubuntu/Debian:

apt-cache search php- | less

CentOS/Fedora:

yum search php-

or

dnf search php-

openSUSE:

zypper search php7-

Installing a PHP Module:

Assuming you need php-mbstring, you would run:

Ubuntu/Debian:

sudo apt install php-mbstring

CentOS/Fedora:

sudo yum install php-mbstring

or

sudo dnf install php-mbstring

openSUSE:

sudo zypper install php7-mbstring

Step 6: Testing PHP

To ensure PHP is installed and configured correctly, create a test PHP file:

echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/phpinfo.php

Now access this file through your web browser using your server's IP address followed by /phpinfo.php. This should display the PHP configuration page.

Step 7: Integrating PHP with a Web Server

Finally, if you're running a web server (Apache or Nginx), you will need to make sure it's configured to process PHP files. This usually involves tweaking the server configuration files to handle .php files using the PHP processor.

For Apache:

Enable the PHP module and restart Apache:

sudo a2enmod php7.x
sudo systemctl restart apache2

For Nginx:

You will need to add a server block or modify an existing one in your site configuration that looks something like this:

location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/var/run/php/php7.x-fpm.sock;
}

Don't forget to replace 7.x with your PHP version number.

Wrapping Up

You now have PHP installed and configured on your Linux system along with some additional modules to kick start your web development projects. Whether you are setting up a personal blog, a corporate website, or a complex web application, Linux and PHP can provide a solid foundation to build upon. Remember, the configuration can vary significantly based on the specific Linux distribution and your individual requirements, so always refer to the official documentation for detailed guidance.

Further Reading

For further reading on related topics, consider these resources:

  • Understanding Linux Distributions: Familiarize yourself with various types of Linux distributions to choose the best one for your project needs. Read more here.

  • PHP Configuration Tips: Dive deeper into advanced configurations and optimization techniques for PHP on Linux servers. Explore this guide.

  • Introduction to Apache and Nginx with PHP: Learn how to integrate PHP with the most popular web servers, Apache and Nginx. Apache details and Nginx details.

  • Securing PHP on Linux Servers: Discover security best practices for running PHP in a Linux environment. Check out these guidelines.

  • Performance Tuning of PHP Applications: Boost your PHP applications' performance by optimizing your code and server settings. Read more here.

These articles provide a blend of conceptual understanding and practical tips to enhance your work with PHP and Linux.