{"id":696,"date":"2024-09-22T17:01:08","date_gmt":"2024-09-22T17:01:08","guid":{"rendered":"https:\/\/webhostingpune.co.in\/blog\/?p=696"},"modified":"2024-09-22T17:01:11","modified_gmt":"2024-09-22T17:01:11","slug":"how-to-install-and-configure-php","status":"publish","type":"post","link":"https:\/\/webhostingpune.co.in\/blog\/how-to-install-and-configure-php\/","title":{"rendered":"How to install and configure PHP"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">PHP\u00a0is a general-purpose scripting language well-suited for Web development since PHP scripts can be embedded into HTML. This guide explains how to install and configure PHP in an Ubuntu System with Apache2 and MySQL.<\/p>\n\n\n\n<div class=\"wp-block-rank-math-toc-block\" id=\"rank-math-toc\"><h2>Table of Contents<\/h2><nav><ul><li><a href=\"#install-php\">Install PHP<\/a><\/li><li><a href=\"#install-optional-packages\">Install Optional Packages<\/a><\/li><li><a href=\"#configure-php\">Configure PHP<\/a><\/li><li><a href=\"#test-your-setup\">Test your setup<\/a><\/li><\/ul><\/nav><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"install-php\">Install PHP<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">PHP is available on Ubuntu Linux, but unlike Python (which comes pre-installed), must be manually installed.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To install PHP \u2013 and the Apache PHP module \u2013 you can enter the following command into a terminal prompt:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt install php libapache2-mod-php<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"500\" height=\"500\" src=\"https:\/\/webhostingpune.co.in\/blog\/wp-content\/uploads\/2024\/09\/install-php-e1672380294649.jpg\" alt=\"\" class=\"wp-image-703\" style=\"width:378px;height:auto\" srcset=\"https:\/\/webhostingpune.co.in\/blog\/wp-content\/uploads\/2024\/09\/install-php-e1672380294649.jpg 500w, https:\/\/webhostingpune.co.in\/blog\/wp-content\/uploads\/2024\/09\/install-php-e1672380294649-300x300.jpg 300w, https:\/\/webhostingpune.co.in\/blog\/wp-content\/uploads\/2024\/09\/install-php-e1672380294649-150x150.jpg 150w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"install-optional-packages\">Install Optional Packages<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The following packages are optional and can be installed if you need them for your setup.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>PHP-CLI<\/strong><br>You can run PHP scripts via the Command Line Interface (CLI). To do this, you must first install the\u00a0<code>php-cli<\/code>\u00a0package. You can install it by running the following command:<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt install php-cli<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>PHP-CGI<\/strong><br>You can also execute PHP scripts without installing the Apache PHP module. To accomplish this, you should install the\u00a0<code>php-cgi<\/code>\u00a0package via this command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt install php-cgi<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>PHP-MySQL<\/strong><br>To use MySQL with PHP you should install the\u00a0<code>php-mysql<\/code>\u00a0package, like so:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt install php-mysql<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>PHP-PgSQL<\/strong><br>Similarly, to use PostgreSQL with PHP you should install the\u00a0<code>php-pgsql<\/code>\u00a0package:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt install php-pgsql<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"configure-php\">Configure PHP<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">If you have installed the&nbsp;<code>libapache2-mod-php<\/code>&nbsp;or&nbsp;<code>php-cgi<\/code>&nbsp;packages, you can run PHP scripts from your web browser. If you have installed the&nbsp;<code>php-cli<\/code>&nbsp;package, you can run PHP scripts at a terminal prompt.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">By default, when\u00a0<code>libapache2-mod-php<\/code>\u00a0is installed, the Apache2 <a href=\"https:\/\/www.squarebrothers.com\/vps-hosting-india\/\" target=\"_blank\" rel=\"noopener\">web server<\/a> is configured to run PHP scripts using this module. First, verify if the files\u00a0<code>\/etc\/apache2\/mods-enabled\/php8.*.conf<\/code>\u00a0and\u00a0<code>\/etc\/apache2\/mods-enabled\/php8.*.load<\/code>\u00a0exist. If they do not exist, you can enable the module using the\u00a0<code>a2enmod<\/code>\u00a0command.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"225\" height=\"225\" src=\"https:\/\/webhostingpune.co.in\/blog\/wp-content\/uploads\/2024\/09\/images.png\" alt=\"\" class=\"wp-image-704\" srcset=\"https:\/\/webhostingpune.co.in\/blog\/wp-content\/uploads\/2024\/09\/images.png 225w, https:\/\/webhostingpune.co.in\/blog\/wp-content\/uploads\/2024\/09\/images-150x150.png 150w\" sizes=\"auto, (max-width: 225px) 100vw, 225px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Once you have installed the PHP-related packages and enabled the Apache PHP module, you should restart the Apache2 web server to run PHP scripts, by running the following command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo systemctl restart apache2.service<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"test-your-setup\">Test your setup<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">To verify your installation, you can run the following PHP\u00a0<code>phpinfo<\/code>\u00a0script:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n  phpinfo();\n?><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You can save the content in a file \u2013\u00a0<code>phpinfo.php<\/code>\u00a0for example \u2013 and place it under the\u00a0<code>DocumentRoot<\/code>\u00a0directory of the Apache2 web <a href=\"https:\/\/www.webhostingpune.co.in\/web-hosting\/\">hosting server<\/a>. Pointing your browser to\u00a0<code>http:\/\/hostname\/phpinfo.php<\/code>\u00a0will display the values of various PHP configuration parameters.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP\u00a0is a general-purpose scripting language well-suited for Web development since PHP scripts can be embedded into HTML. This guide explains how to install and configure PHP in an Ubuntu System with Apache2 and MySQL. Install PHP PHP is available on Ubuntu Linux, but unlike Python (which comes pre-installed), must be manually installed. To install PHP \u2013 and the Apache PHP module \u2013 you can enter the following command into a terminal prompt: Install Optional Packages The following packages are optional and can be installed if you need them for your setup. PHP-CGIYou can also execute PHP scripts without installing the Apache PHP module. To accomplish this, you should install the\u00a0php-cgi\u00a0package via this command: PHP-MySQLTo use MySQL with PHP you should install the\u00a0php-mysql\u00a0package, like so: PHP-PgSQLSimilarly, to use PostgreSQL with PHP you should install the\u00a0php-pgsql\u00a0package: Configure PHP If you have installed the&nbsp;libapache2-mod-php&nbsp;or&nbsp;php-cgi&nbsp;packages, you can run PHP scripts from your web browser. If you have installed the&nbsp;php-cli&nbsp;package, you can run PHP scripts at a terminal prompt. By default, when\u00a0libapache2-mod-php\u00a0is installed, the Apache2 web server is configured to run PHP scripts using this module. First, verify if the files\u00a0\/etc\/apache2\/mods-enabled\/php8.*.conf\u00a0and\u00a0\/etc\/apache2\/mods-enabled\/php8.*.load\u00a0exist. If they do not exist, you can enable the module using the\u00a0a2enmod\u00a0command. Once you have installed the PHP-related packages and enabled the Apache PHP module, you should restart the Apache2 web server to run PHP scripts, by running the following command: Test your setup To verify your installation, you can run the following PHP\u00a0phpinfo\u00a0script: You can save the content in a file \u2013\u00a0phpinfo.php\u00a0for example \u2013 and place it under the\u00a0DocumentRoot\u00a0directory of the Apache2 web hosting server. Pointing your browser to\u00a0http:\/\/hostname\/phpinfo.php\u00a0will display the values of various PHP configuration parameters.<\/p>\n","protected":false},"author":2,"featured_media":700,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","footnotes":""},"categories":[1],"tags":[],"class_list":["post-696","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/webhostingpune.co.in\/blog\/wp-json\/wp\/v2\/posts\/696","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webhostingpune.co.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webhostingpune.co.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webhostingpune.co.in\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/webhostingpune.co.in\/blog\/wp-json\/wp\/v2\/comments?post=696"}],"version-history":[{"count":3,"href":"https:\/\/webhostingpune.co.in\/blog\/wp-json\/wp\/v2\/posts\/696\/revisions"}],"predecessor-version":[{"id":705,"href":"https:\/\/webhostingpune.co.in\/blog\/wp-json\/wp\/v2\/posts\/696\/revisions\/705"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/webhostingpune.co.in\/blog\/wp-json\/wp\/v2\/media\/700"}],"wp:attachment":[{"href":"https:\/\/webhostingpune.co.in\/blog\/wp-json\/wp\/v2\/media?parent=696"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webhostingpune.co.in\/blog\/wp-json\/wp\/v2\/categories?post=696"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webhostingpune.co.in\/blog\/wp-json\/wp\/v2\/tags?post=696"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}