Linux Tactic

Setting Up PHP on Your Web Server: Installation Configuration and Testing

If you’re setting up a web server, you’ll almost certainly need PHP. PHP is a server-side scripting language that enables websites to be dynamic and interactive.

In this article, we’ll show you how to install PHP 7.2 on Debian 9 and configure Apache to run PHP. Installing PHP 7.2 on Debian 9:

To install PHP 7.2 on Debian 9, we’ll be using Ondrej Sury’s repository.

Ondrej Sury is a Debian developer who maintains the latest versions of PHP packages. The first step is to add the Ondrej Sury repository to your sources list.

You’ll need to install software-properties-common first, so execute the command:

“`

sudo apt-get install software-properties-common

“`

Once this package is installed, add the Ondrej Sury repository to your sources list with the following command:

“`

sudo add-apt-repository ppa:ondrej/php

“`

You’ll be prompted to press Enter to add the repository. Afterward, you’ll need to update your apt-get package list before continuing with the installation.

“`

sudo apt-get update

“`

Next, we can install PHP 7.2 and a few required packages by running the following command:

“`

sudo apt-get install php7.2-fpm php7.2-cli php7.2-common php7.2-mysql php7.2-json php7.2-opcache php7.2-readline

“`

This command installs several packages, including the PHP 7.2 FPM (FastCGI Process Manager) and CLI (Command Line Interface) packages, which are required to run PHP on Apache. Verifying Installation:

Once PHP 7.2 is installed, we need to verify the installation by creating a simple PHP file.

We can use the following two commands to create a test file named info.php:

“`

cd /var/www/html

sudo nano info.php

“`

Add the following line to the file:

“`

phpinfo();

?>

“`

Save the file and exit. Now, open your web browser and navigate to http://your_server_ip/info.php.

You should see a page with information about your PHP installation. Configuring Apache to Run PHP:

To configure Apache to run PHP, we need to install the Apache PHP module and enable the php7.2 module.

Installing Apache PHP Module:

We need to install Apache PHP module, so execute the following command:

“`

sudo apt-get install libapache2-mod-php7.2

“`

This command installs the Apache PHP module for PHP 7.2.

Enabling PHP7.2 Module and Restarting Apache:

After installing the Apache PHP module, we need to enable the php7.2 module and restart Apache. Execute the following command:

“`

sudo a2enmod php7.2

“`

This command enables the php7.2 Apache module, which is required to run PHP on Apache.

Finally, restart the Apache service with the following command:

“`

sudo systemctl restart apache2

“`

Conclusion:

In this article, we’ve shown you how to install PHP 7.2 on Debian 9 and configure Apache to run PHP. By following these steps, you’ll have a functional PHP environment that can handle most web applications.

Configuring Nginx to Run PHP:

Nginx is a popular web server that is known for its high performance and low memory usage. To run PHP on Nginx web server, you’ll need to install PHP FPM package, configure the Nginx server block, and restart the Nginx service.

1. Installing PHP FPM package:

To install PHP FPM package, run the following command:

“`

sudo apt-get install php-fpm

“`

This command installs the PHP FPM package, which is required to run PHP on Nginx. 2.

Checking and Restarting PHP FPM Service:

After installing the PHP FPM package, you’ll need to check if the PHP FPM service is running and restart it if necessary. You can do this by running the following command:

“`

sudo systemctl status php-fpm

“`

This command will display the current status of the PHP FPM service. If it’s not running, you can start it by executing the following command:

“`

sudo systemctl start php-fpm

“`

3. Configuring Nginx Server Block to Process PHP Files:

Once you have installed PHP FPM and ensured it is running, you can configure the Nginx server block to process PHP files.

To do this, we will be using the `fastcgi_pass` directive to point Nginx to PHP-FPM socket. In the `server` block, add the following lines:

“`

location ~ .php$ {

include fastcgi_params;

fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

fastcgi_param PATH_INFO $fastcgi_path_info;

}

“`

In the above configuration, we’re telling Nginx to forward all `.php` requests to PHP-FPM.

The `fastcgi_pass` line indicates where the FPM server is listening for requests. In this case, it’s a Unix socket, but it can be configured to use TCP sockets as well.

4. Restarting Nginx Service:

After configuring the Nginx server block to process PHP files, you’ll need to restart the Nginx service to apply the changes.

You can do this by running the following command:

“`

sudo systemctl restart nginx

“`

Installing PHP Extensions:

PHP extensions are additional modules that can be installed to extend the functionality of PHP. They’re often required by applications and scripts that your web server will run.

For example, if you’re using WordPress, you’ll need to install the mysql and gd extensions to ensure that WordPress can connect to your database and generate images. 1.

Installing Additional PHP Extensions:

To install additional PHP extensions, you can use the following command:

“`

sudo apt-get install php-[extension-name]

“`

Replace `[extension-name]` with the name of the extension you want to install. For example, to install the MySQL extension, you’d run:

“`

sudo apt-get install php-mysql

“`

2. Examples of Installing MySQL and GD Extensions:

To install the MySQL and GD extensions, run the following commands:

“`

sudo apt-get install php-mysql

sudo apt-get install php-gd

“`

The `php-mysql` extension allows PHP to communicate with MySQL databases, while the `php-gd` extension adds support for image manipulation functions. Conclusion:

By following the steps outlined in this article, you should now know how to configure Nginx to run PHP and install additional PHP extensions.

These steps should be sufficient to get you up and running with a functioning PHP environment that can handle most web applications. Testing PHP Processing:

After installing and configuring PHP on your server, it is important to test that PHP processing is functioning correctly.

You can do this by creating a test file, accessing it in a web browser, and viewing PHP’s configuration information. 1.

Creating a Test File:

To create a test file, you’ll need to create a file with a `.php` extension and insert a simple PHP script. You can create the file in your web server’s root directory.

This can be done using a text editor or command line interface. “`

sudo nano /var/www/html/test.php

“`

Once you have created and opened the file in your editor, insert the following code:

“`

phpinfo();

?>

“`

This script will display information about your PHP configuration.

2. Accessing the Test File in a Browser:

After creating your test file, you’ll need to access it from a web browser.

To do this, simply open your web browser and navigate to the URL where your test file is located. For example, if your server’s IP address is `192.168.0.100` and you created your test file in the `test.php` file in your root directory, you would access it by entering the following in your web browser’s address bar:

“`

http://192.168.0.100/test.php

“`

This will display the output of your PHP script in your web browser.

3. Viewing PHP Configuration Information:

After accessing your test file in your web browser, you should see a page with a variety of information about your PHP installation.

This includes things such as the version of PHP installed, information about various extensions that are loaded, and more. One way to view the PHP configuration information is by using the `phpinfo()` function.

This function outputs a lot of information about PHP, including configuration settings, environment variables, and more. You can also access the PHP configuration information via the command line by typing the command `php -i` at a shell prompt.

This will display a comprehensive list of information about your PHP installation. Conclusion:

In this article, we’ve covered how to test PHP processing on your server.

We’ve shown you how to create a test file, access it in a web browser, and view PHP configuration information. By following these steps, you should be able to verify that PHP is installed and functioning correctly on your server.

This is an essential step in setting up a web server and ensuring that it can run PHP-based applications and websites. In this article, we’ve explored how to install, configure, and test PHP on a web server.

We covered the steps required to install PHP 7.2 on Debian 9 and configure Apache and Nginx to run PHP. We also discussed how to install additional PHP extensions and test PHP processing by creating a test file and accessing it in a web browser.

It’s essential to have a functioning PHP environment to run dynamic and interactive websites. By following the steps outlined in this article, you can ensure that your web server is capable of running PHP-based applications.

Remember to check your PHP processing before running a website to avoid potential issues and enjoy a smooth and functional web server.

Popular Posts