{"id":222,"date":"2019-04-23T09:52:36","date_gmt":"2019-04-23T09:52:36","guid":{"rendered":"https:\/\/scriptut.com\/?p=222"},"modified":"2024-05-25T11:37:50","modified_gmt":"2024-05-25T11:37:50","slug":"install-php-5-6-php-7-php-7-3-ubuntu","status":"publish","type":"post","link":"https:\/\/scriptut.com\/php\/install-php-5-6-php-7-php-7-3-ubuntu\/","title":{"rendered":"How to Install PHP 5.6, PHP 7.0, and PHP 7.2 on Ubuntu"},"content":{"rendered":"\n<p>Are you looking to install multiple PHP versions on your Ubuntu system? Managing different PHP versions can be crucial for various development environments. This guide will walk you through the process of installing PHP 5.6, PHP 7.0, and PHP 7.2 on an Ubuntu system. We will also cover how to switch between these versions seamlessly.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step-by-Step Guide to Install PHP 5.6, PHP 7.0, and PHP 7.2 on Ubuntu<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Prerequisites<\/h3>\n\n\n\n<p>Before we begin, ensure your system meets the following prerequisites:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>A running instance of Ubuntu (preferably a LTS version for stability).<\/li>\n\n\n\n<li>Root or sudo access to perform administrative tasks.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Step 1: Disable Unnecessary Apache Modules<\/h2>\n\n\n\n<p>To avoid conflicts, it&#8217;s essential to disable certain Apache modules if they are enabled. Run the following command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo a2dismod proxy_fcgi proxy\nsudo service apache2 restart<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Step 2: Add the Ond\u0159ej Sur\u00fd PPA<\/h2>\n\n\n\n<p>Ond\u0159ej Sur\u00fd maintains a widely-used PPA (Personal Package Archive) with various PHP versions. Add this repository to your system:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo add-apt-repository ppa:ondrej\/php\nsudo apt-get update<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Step 3: Install PHP Versions<\/h2>\n\n\n\n<p>Install PHP 5.6 and PHP 7.0 along with their common extensions. This setup also includes PHP 7.2:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt-get install php5.6 php7.0 php7.2 php5.6-mysql php-gettext php5.6-mbstring php-mbstring php7.0-mbstring php-xdebug libapache2-mod-php5.6 libapache2-mod-php7.0 libapache2-mod-php7.2<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Step 4: Switching PHP Versions<\/h2>\n\n\n\n<p>To switch between PHP versions, you need to configure both Apache and the Command Line Interface (CLI).<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Switching from PHP 5.6 to PHP 7.0<\/h4>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Apache:<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo a2dismod php5.6\nsudo a2enmod php7.0\nsudo service apache2 restart<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>CLI:<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo update-alternatives --set php \/usr\/bin\/php7.0<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Switching from PHP 7.0 to PHP 5.6<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Apache:<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo a2dismod php7.0\nsudo a2enmod php5.6\nsudo service apache2 restart<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>CLI:<\/strong><\/h4>\n\n\n\n<p>sudo update-alternatives &#8211;set php \/usr\/bin\/php5.6<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Switching from PHP 7.2 to PHP 5.6<\/h2>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Apache:<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo a2dismod php7.2\nsudo a2enmod php5.6\nsudo service apache2 restart<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>CLI:<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo update-alternatives --set php \/usr\/bin\/php5.6<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Ensuring a Smooth PHP Environment<\/h2>\n\n\n\n<p>After switching PHP versions, it\u2019s crucial to ensure that your web applications and scripts are compatible with the version in use. Here are some tips to maintain a smooth PHP environment:<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Testing Your PHP Installation<\/h2>\n\n\n\n<p>Create a simple PHP info file to test which PHP version is currently active:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Navigate to your web server\u2019s root directory (e.g., <code>\/var\/www\/html<\/code>).<\/li>\n\n\n\n<li>Create a file named <code>info.php<\/code> with the following content:<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\nphpinfo();\n?>\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Open your web browser and navigate to <code>http:\/\/your-server-ip\/info.php<\/code>. This page will display detailed information about the PHP version and modules currently active.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Managing PHP Extensions<\/h2>\n\n\n\n<p>Each PHP version may require different extensions for your applications to run correctly. Use the following commands to install or enable extensions:<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>For PHP 5.6:<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt-get install php5.6-&lt;extension><\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>For PHP 7.0:<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt-get install php7.0-&lt;extension><\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>For PHP 7.2:<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt-get install php7.2-&lt;extension>\n<\/code><\/pre>\n\n\n\n<p>Replace <code>&lt;extension><\/code> with the desired extension name (e.g., <code>curl<\/code>, <code>gd<\/code>, <code>intl<\/code>).<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Troubleshooting Common Issues<\/h2>\n\n\n\n<p><strong>Issue: PHP Version Not Switching Correctly<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Ensure you restart Apache after switching versions.<\/li>\n\n\n\n<li>Verify the CLI version by running <code>php -v<\/code>.<\/li>\n<\/ul>\n\n\n\n<p><strong>Issue: Missing Extensions<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Confirm the extension is installed and enabled for the active PHP version.<\/li>\n\n\n\n<li>Check the PHP error logs for more detailed error messages.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Automating PHP Version Switching<\/h2>\n\n\n\n<p>For developers frequently switching PHP versions, automating the process can save time. Consider creating bash scripts to handle the commands:<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Example Script to Switch to PHP 5.6:<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/bin\/bash\nsudo a2dismod php7.0\nsudo a2enmod php5.6\nsudo service apache2 restart\nsudo update-alternatives --set php \/usr\/bin\/php5.6\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Example Script to Switch to PHP 7.0:<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/bin\/bash\nsudo a2dismod php5.6\nsudo a2enmod php7.0\nsudo service apache2 restart\nsudo update-alternatives --set php \/usr\/bin\/php7.0\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Save these scripts in a convenient location and grant execute permissions:<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>chmod +x switch-to-php5.6.sh\nchmod +x switch-to-php7.0.sh<\/code><\/pre>\n\n\n\n<p>Run the script whenever you need to switch PHP versions.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>Installing and managing multiple PHP versions on Ubuntu is straightforward with the right steps. By using the Ond\u0159ej Sur\u00fd PPA, you can easily install PHP 5.6, PHP 7.0, and PHP 7.2. Switching between these versions for both Apache and CLI is essential for maintaining different development environments. Always test your applications after switching versions to ensure compatibility and smooth functionality. Follow this guide, and you&#8217;ll have a flexible PHP environment up and running on your Ubuntu system.<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Set up a flexible PHP environment on Ubuntu by installing PHP 5.6, PHP 7.0, and PHP 7.2. Follow our detailed guide to switch between these PHP versions seamlessly for both Apache and CLI.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-222","post","type-post","status-publish","format-standard","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/scriptut.com\/wp-json\/wp\/v2\/posts\/222","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/scriptut.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/scriptut.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/scriptut.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/scriptut.com\/wp-json\/wp\/v2\/comments?post=222"}],"version-history":[{"count":2,"href":"https:\/\/scriptut.com\/wp-json\/wp\/v2\/posts\/222\/revisions"}],"predecessor-version":[{"id":390,"href":"https:\/\/scriptut.com\/wp-json\/wp\/v2\/posts\/222\/revisions\/390"}],"wp:attachment":[{"href":"https:\/\/scriptut.com\/wp-json\/wp\/v2\/media?parent=222"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/scriptut.com\/wp-json\/wp\/v2\/categories?post=222"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/scriptut.com\/wp-json\/wp\/v2\/tags?post=222"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}