Step-by-Step Guide: How to Install Apache and Run a PHP Website

 This guide will walk you through the installation of the Apache web server and configuring it to run a PHP-based website on your local machine or server. This setup is commonly referred to as a LAMP stack (Linux, Apache, MySQL, PHP), but we will focus on Apache and PHP for this tutorial.

Step 1: Install Apache Web Server

1.1 Update the Package List (for Linux Systems like Ubuntu)
Before installing Apache, ensure that your system packages are up-to-date. Open the terminal and run:

sudo apt update


1.2 Install Apache
Next, install the Apache server by running:

sudo apt install apache2

Once the installation is complete, Apache should start automatically. You can verify if it is running by checking the status:

sudo systemctl status apache2

Step 2: Install PHP

2.1 Install PHP
Install PHP along with its necessary libraries by running the following command:

sudo apt install php libapache2-mod-php php-mysql

2.2 Verify PHP Installation

Check the PHP version to confirm that it’s successfully installed:

php -v


Step 3: Configure Apache to Run PHP
3.1 Configure Apache to Prefer PHP Files
To make Apache prefer PHP files over other files like HTML, edit the configuration file:

sudo nano /etc/apache2/mods-enabled/dir.conf

Change the line to make sure PHP comes before index.html:
<IfModule mod_dir.c>
    DirectoryIndex index.php index.html
</IfModule>

Press CTRL+X, then Y, and hit Enter to save the file.

3.2 Restart Apache
For changes to take effect, restart the Apache server:

sudo systemctl restart apache2

Step 4: Create a Test PHP File

4.1 Create a PHP File in Apache’s Root Directory
Now, let’s create a test PHP file in the web root directory (/var/www/html/):
sudo nano /var/www/html/index.php

Write the following PHP code:

<?php
phpinfo();
?>

Save and exit the file.

4.2 Check if PHP is Running
Open your browser and go to http://localhost/index.php. You should see the PHP info page, which confirms that Apache is correctly processing PHP.

Step 5: Deploy Your PHP Website

5.1 Move Your PHP Website Files
To deploy your PHP website, copy all the files from your project folder into Apache’s root directory:

sudo cp -r /path/to/your/php-website/* /var/www/html/


5.2 Set Correct Permissions
Make sure Apache has the correct permissions to access your files:

sudo chown -R www-data:www-data /var/www/html/
sudo chmod -R 755 /var/www/html/

5.3 Open the Website in a Browser
Now, you can access your PHP website by going to http://localhost/ in your browser.

Step 6: (Optional) Enable Apache Rewrite Module

If your PHP website uses URL rewriting, you’ll need to enable the Apache rewrite module:

sudo a2enmod rewrite

sudo systemctl restart apache2

#ApacheInstallation #PHPWebsiteSetup #LAMPStackGuide #WebDevelopmentBasics #RunPHPOnApache #ApacheAndPHP #LocalServerSetup #PHPDeployment #ServerConfiguration #WebHostingTutorial




No comments:

Post a Comment

IF you any query about any project related than write your comment in comment box