Current Location: Home> Latest Articles> How to Change the Port Number in PHP7 (Apache Server Guide)

How to Change the Port Number in PHP7 (Apache Server Guide)

gitbox 2025-08-04

How to Change the Port Number in PHP7

PHP is a widely used server-side scripting language for developing web applications. By default, PHP communicates with the web server through port 80. However, in cases where port 80 is already in use or for security purposes, you may need to change this port. This guide explains how to change the port number in PHP7 when running on an Apache server.

Locate the php.ini Configuration File

First, you need to locate PHP's configuration file, php.ini, which contains various PHP settings. On a Linux system, you can use the following command to find it:

find / -name php.ini

This command searches your entire system and returns the path to php.ini.

Edit the php.ini File

Once you've found the file, open it using a text editor like vi or nano. For example:

vi /etc/php/7.0/apache2/php.ini

Look for the line that sets the listening port and update it to your preferred port number:

listen = 127.0.0.1:8080

In this example, the port is changed to 8080, but you can use any valid port.

Restart the Apache Server

After editing the configuration, restart Apache to apply the changes by running the following command:

sudo service apache2 restart

Wait for the server to restart completely.

Verify the Port Change

To ensure the changes are effective, open your web browser and navigate to the following URL:

http://localhost:8080/info.php

If the page loads correctly, the port has been successfully changed.

Conclusion

By locating the php.ini file, updating the port setting, restarting Apache, and verifying via the browser, you can easily change the PHP7 port number. This is especially useful for avoiding port conflicts or setting up multiple development environments.