# How to Change PHP Version & Settings

PHP is a widely used scripting language for web development, and different applications may require different PHP versions or settings to function properly. Whether you need to upgrade for security reasons, downgrade for compatibility, or modify settings for performance, this guide will show you how to change your PHP version and customize settings on different platforms.

***

#### Why Change PHP Version?

Changing the PHP version can be necessary for several reasons:

* **Application Compatibility**: Some websites and applications require a specific PHP version.
* **Security Updates**: Newer PHP versions include important security patches.
* **Performance Optimization**: Later PHP versions improve performance and reduce resource usage.
* **Feature Support**: Some functions and libraries are only available in newer versions of PHP.

***

#### How to Check Your Current PHP Version

Before making changes, check the current PHP version running on your server.

**Check PHP Version via Command Line**

If you have SSH access, run the following command:

```bash
php -v
```

This will display the installed PHP version.

**Check PHP Version via cPanel**

1. Log in to **cPanel**.
2. Navigate to **Software > Select PHP Version**.
3. Your current PHP version will be displayed at the top of the page.

**Check PHP Version via a PHP Script**

1. Create a new file called `phpinfo.php`.
2. Add the following code:

   ```php
   <?php
   phpinfo();
   ?>
   ```
3. Upload the file to your website’s root directory.
4. Visit `https://yourdomain.com/phpinfo.php` in your browser.

***

#### How to Change PHP Version

**Change PHP Version in cPanel**

1. Log in to **cPanel**.
2. Go to **Select PHP Version** under the **Software** section.
3. Use the dropdown menu to choose your desired PHP version.
4. Click **Set as Current** to apply the change.
5. If needed, enable or disable PHP extensions.

**Change PHP Version in Plesk**

1. Log in to **Plesk**.
2. Navigate to **Websites & Domains > PHP Settings**.
3. Use the dropdown menu to select a different PHP version.
4. Click **Apply** or **OK** to save changes.

**Change PHP Version Using .htaccess (Apache Servers)**

If your server allows `.htaccess` overrides, add this line:

```apache
AddHandler application/x-httpd-php81 .php
```

Replace `php81` with your desired PHP version.

**Change PHP Version via Command Line (Linux)**

For VPS or dedicated servers:

1. List available PHP versions:

   ```bash
   update-alternatives --list php
   ```
2. Change PHP version:

   ```bash
   sudo update-alternatives --set php /usr/bin/php8.1
   ```
3. Verify the change:

   ```bash
   php -v
   ```

***

#### How to Modify PHP Settings

Certain applications may require modifications to PHP settings like memory limit, max execution time, and file upload size.

**Modify PHP Settings via cPanel**

1. Log in to **cPanel**.
2. Go to **Select PHP Version > Options**.
3. Adjust settings such as:
   * `memory_limit`
   * `upload_max_filesize`
   * `post_max_size`
   * `max_execution_time`
4. Save changes.

**Modify PHP Settings via php.ini**

For servers with direct access to PHP configuration files:

1. Locate the `php.ini` file:

   ```bash
   find /etc -name php.ini
   ```
2. Edit it using a text editor:

   ```bash
   sudo nano /etc/php/8.1/apache2/php.ini
   ```
3. Modify values, for example:

   ```ini
   memory_limit = 256M
   max_execution_time = 300
   upload_max_filesize = 64M
   ```
4. Save and restart the web server:

   ```bash
   sudo systemctl restart apache2
   ```

**Modify PHP Settings via .htaccess**

If you do not have access to `php.ini`, modify settings via `.htaccess`:

```apache
php_value memory_limit 256M
php_value max_execution_time 300
php_value upload_max_filesize 64M
```

Save the file and check if the changes take effect.

**Modify PHP Settings via CLI**

For CLI applications:

```bash
php -d memory_limit=256M script.php
```

***

#### Troubleshooting PHP Version Issues

**"500 Internal Server Error" After Changing PHP Version**

* Ensure that required PHP extensions are enabled.
* Check the error log (`/var/log/apache2/error.log` or `/var/log/nginx/error.log`).
* Try switching back to a previous PHP version to see if the issue resolves.

**"php.ini Changes Not Taking Effect"**

* Restart Apache or Nginx:

  ```bash
  sudo systemctl restart apache2
  ```

  ```bash
  sudo systemctl restart nginx
  ```
* Check for multiple `php.ini` files using:

  ```bash
  php --ini
  ```
* Verify changes with `phpinfo()`.

**"Extensions Not Loading"**

* Enable necessary PHP extensions in **cPanel > Select PHP Version**.
* Install missing extensions via CLI:

  ```bash
  sudo apt install php8.1-mbstring
  ```
* Restart PHP services after installing extensions.

***

#### Conclusion

Changing PHP versions and modifying settings is essential for compatibility, security, and performance. Whether using **cPanel**, **Plesk**, **CLI**, or **.htaccess**, following the correct steps ensures a smooth transition. Regularly updating to supported PHP versions and optimizing settings helps keep your applications running efficiently.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://learn.sitecove.com/how-to-guides/web-hosting/server-and-hosting-settings/how-to-change-php-version-and-settings.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
