Upgrade PHP version for Apache on Ubuntu server

One day, you found that your PHP version is too old and vulnerable. You have to upgrade to latest PHP version to avoid being exploited. You can follow the instructions below. Just a few commands.

Configure apache point to new version of PHP

**Important note**: List all your loaded modules in your old php by checking your phpinfo. The configure the same into new php in order to make sure your application have all needed modules for use.

a2dismod [current_php] ==> dis-mode current php version

root@ip-xxx-xx-x-xxx:/xxx/xxx/www# a2dismod php5 

perl: warning: Setting locale failed.

perl: warning: Please check that your locale settings:

 LANGUAGE = (unset),

 LC_ALL = (unset),

 LC_CTYPE = "UTF-8",

 LANG = "en_US.UTF-8"

    are supported and installed on your system.

perl: warning: Falling back to the standard locale ("C").

Module php5 disabled.

To activate the new configuration, you need to run:

  service apache2 restart

a2enmod [new_php] ==> en-mode to new php version

root@ip-xxx-xx-x-xxx:/xxxx/xxxx/www# a2enmod php5.6 

perl: warning: Setting locale failed.

perl: warning: Please check that your locale settings:

 LANGUAGE = (unset),

 LC_ALL = (unset),

 LC_CTYPE = "UTF-8",

 LANG = "en_US.UTF-8"

    are supported and installed on your system.

perl: warning: Falling back to the standard locale ("C").

Considering dependency mpm_prefork for php5.6:

Considering conflict mpm_event for mpm_prefork:

Considering conflict mpm_worker for mpm_prefork:

Considering conflict mpm_itk for mpm_prefork:

Module mpm_prefork already enabled

Considering conflict php5 for php5.6:

Enabling module php5.6.

To activate the new configuration, you need to run:

  service apache2 restart

You have to restart your apache to take effect.

root@ip-xxx-xx-x-xxx:/xxxx/xxxx/www#  service apache2 restart

 * Restarting web server apache2 

You can now check your phpinfo to see the new php version and loaded modules

Configure console PHP to new version of PHP for your cronjob or cli program

root@ip-xxx-xx-x-xxx:/xxxx/xxxx/www# ln -sf /usr/bin/php[version] /etc/alternatives/php

You can use ‘which’ php command to see what is your current php command point to

That’s all. Hope it help.

Upgrade PHP version for Apache on Ubuntu server

How to fix PHP Composer Update “cannot allocate memory” error

You develop your PHP application on your local machine and everything works perfectly. Then you want to release for the public use. You purchase a VPS then install related services, start a web server, git pull your code into, finally run composer update…and whoops an error of memory is printed out your console.

Pasted_Image_3_29_18__3_07_PM

This is because your VPS is too small or by any certain reasons that your VPS swap memory is out. There is 2 ways to tackle this issue.

  • Upgrade your VPS or purchase a new one that have enough RAM for swap.
  • Hack your swap memory inside your old VPS with no bucks cost.

The second options may sound more sense right, let’s do it. It’s easy 🙂

The following will help you to hack on your VPS swap memory

1. Initialize your swap.1 file with size of 1Gb
root@ip-xxx-xx-6-164:/# sudo /bin/dd if=/dev/zero of=/var/swap.1 bs=1M count=1024

You will see the result
1024+0 records in
1024+0 records out
1073741824 bytes (1.1 GB) copied, 13.6493 s, 78.7 MB/s

2. Make your swap on swap.1 file
root@ip-xxx-xx-6-164:/# sudo /sbin/mkswap /var/swap.1

You will see the result
Setting up swapspace version 1, size = 1048572 KiB
no label, UUID=be28fbf5-51ce-49bd-b46d-6b45770a6624

3. Activate your swap
root@ip-xxx-xx-6-164:/# sudo /sbin/swapon /var/swap.1

That’s all. Run your compose update again.

How to fix PHP Composer Update “cannot allocate memory” error