PHP is one of the most popular server-side scripting languages. It is part of the LAMP stack applications that run fully functional dynamic websites.
Changing PHP settings is an inevitable task especially if you are running content management systems that don’t work with default settings. Sometimes, you can install different PHP versions on a server but each installation has its own configuration file.
In this guide, we are going to outline the steps needed to change common PHP settings on your Ubuntu 18.04 running Apache web server.
- Ubuntu 18.04 server or VPS plan
- A non-root user with sudo privileges
- Apache web server
- Nano text editor
Step 1: Ensure that PHP and Apache are working on your server
Run the command below to check the status of Apache on your web server
$ service apache2 status
The status should be ‘Active’ if apache installed. In case it was uninstalled on your server, run the commands below to install it.
$ sudo apt update && sudo apt upgrade
$ sudo apt-get install apache2
To check the presence of PHP on your server, run the command below:
$ php -v
You will get the PHP version on your terminal window e.g. php7.1
In case PHP is not available run the command below to install it on your server:
$ sudo apt-get install php libapache2-mod-php
Step 2: Locate the PHP configuration file
Determining the right PHP configuration file can be very confusing especially because the ‘php.ini’ file can be located on a different folder depending on the PHP version.
The correct php.ini file should be in the Apache directory (e.g. ‘/etc/php/7.1/apache2/php.ini’). This will depend on the version of PHP. For instance, in Php7.2, the configuration file is located on ‘/etc/php/7.2/apache2/php.ini’
Step 3: Edit the Php Configuration file
To edit the configuration file use a nano editor using the command below. Remember to replace 7.1 with your PHP version number
$ sudo nano /etc/php/7.1/apache2/php.ini
Once the file opens on a text editor, you can start tweaking the settings
The list below include the most common settings that are regularly changed by webmasters:
- upload_max_filesize: The value determines the maximum size of the file that can be uploaded to the web server. This is particularly useful if you are running a service like OwnCloud or large database imports.
- post_max_size: This is the maximum value that the post variable should hold. These are common with web forms. An average of 20M works well.
- register_globals: This is an internal settings that registers $_REQUEST values to global variables. When turned on, it will work for all GET, POST and Cookie http verbs.
- allow_url_fopen: This can be off by default but you can turn it on. It assists in opening url objects like files.
- memory_limit: This value should be larger than upload_max_filesize. It simply sets the maximum bytes that a script can allocate.
- max_execution_time: The default value is 30. Meaning that the maximum time a script can run before being terminated is 30 seconds. You can set a larger value like 60 if your php scripts are time intensive.
Sample ‘php.ini’ settings file
memory_limit = 24M upload_max_filesize = 20M register_globals = Off allow_url_fopen = On max_execution_time = 30 post_max_size = 20M
Remember to press CTRL + X, Y and Enter to save the PHP settings. Also, you must restart Apache for the changes to be effected using the command below:
$ sudo systemctl restart apache2
Although this is not an exhaustive list of the settings that you can change on your PHP, it serves as a basic guide for tweaking basic configuration as required by your hosting environment. This is very common when deploying applications that require larger memory limits. You may change any settings as per your need from the ‘php.ini’ file. I hope you enjoyed reading the guide.
Check out the top 3 VPS services: