How to Change Common PHP Settings on an Ubuntu 18.04 VPS or Dedicated Server

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.

Prerequisites

  • Ubuntu 18.04 server or VPS plan
  • A non-root user with sudo privileges
  • Apache web server
  • PHP
  • 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:

  1. 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.
  2. 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.
  3. 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.
  4. allow_url_fopen:  This can be off by default but you can turn it on. It assists in opening url objects like files.
  5. memory_limit: This value should be larger than upload_max_filesize. It simply sets the maximum bytes that a script can allocate.
  6. 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

Conclusion

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:

HostArmada
$2.49 /mo
Starting price
Visit HostArmada
Rating based on expert review
  • User Friendly
    4.5
  • Support
    4.5
  • Features
    4.5
  • Reliability
    4.5
  • Pricing
    4.0
Kamatera
$4.00 /mo
Starting price
Visit Kamatera
Rating based on expert review
  • User Friendly
    3.5
  • Support
    3.0
  • Features
    3.9
  • Reliability
    4.0
  • Pricing
    4.3
Hostens
$2.00 /mo
Starting price
Visit Hostens
Rating based on expert review
  • User Friendly
    4.5
  • Support
    3.8
  • Features
    4.3
  • Reliability
    4.5
  • Pricing
    4.8

* Learn more about our best recommended VPS Providers

How to Set Up WordPress Multisite on Ubuntu 18.04 with Apache Web Server

In this tutorial, we explain how to set up WordPress multisite on an Ubuntu 18.0
3 min read
Idan Cohen
Idan Cohen
Marketing Expert

How to Configure KeepAlive Settings for Apache running on an Ubuntu 22.04 VPS

This guide talks about fine-tuning KeepAlive settings on your Apache web server
2 min read
Max Ostryzhko
Max Ostryzhko
Senior Web Developer, HostAdvice CTO

How to Install Apache Cassandra on an Ubuntu 18.04 VPS or Dedicated Server

This tutorial will help you install and configure Apache Cassandra (an opensourc
3 min read
Max Ostryzhko
Max Ostryzhko
Senior Web Developer, HostAdvice CTO

How to Host Multiple Websites on an Ubuntu 18.04 VPS or Dedicated Server

This article will show you how to host two or more websites on a single Virtual
5 min read
Idan Cohen
Idan Cohen
Marketing Expert
HostAdvice.com provides professional web hosting reviews fully independent of any other entity. Our reviews are unbiased, honest, and apply the same evaluation standards to all those reviewed. While monetary compensation is received from a few of the companies listed on this site, compensation of services and products have no influence on the direction or conclusions of our reviews. Nor does the compensation influence our rankings for certain host companies. This compensation covers account purchasing costs, testing costs and royalties paid to reviewers.