Setup Local PHP Dev Server on Linux in Easy Steps

Setting up a local web development server for PHP in Linux can be done using Apache or Nginx web server. Here’s a step-by-step tutorial on how to set up a local PHP web development server using Apache on a Linux distribution such as Ubuntu or Debian:

Step 1: Install Apache web server

Install Apache web server and PHP by running the following command in the terminal:

sudo apt-get update && sudo apt-get install apache2 php libapache2-mod-php

Step 2: Start the server

Once the installation is complete, start the Apache web server by running the following command:

sudo service apache2 start

Step 3: Check if it’s working

Now that the Apache web server is running, you can check to make sure it’s working by going to http://localhost in your web browser. You should see the default Apache page.

Step 4: Configure Apache

Next, you need to configure Apache to handle PHP files. To do this, open the Apache configuration file:

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

Step 5: Move PHP Handler

In this file, you will see a list of file types that Apache handles. You need to move the PHP handler to the top of the list so that Apache will handle PHP files first. It should look like this:

<IfModule mod_dir.c>
    DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm
</IfModule>

Step 6: Restart Apache Server

Save the file and exit the editor. Then, restart the Apache web server:

sudo service apache2 restart

Step 7: Test Local Development Server

Now, you can create a new PHP file to test your local PHP development environment. For example, create a file called info.php in the Apache web root directory:

sudo nano /var/www/html/info.php

Step 8: Save File for Testing

Add the following code to the file and save it:

<?php phpinfo(); ?>

Step 9: Test your Environment

Now, you can test your PHP environment by visiting the following URL in your web browser: http://localhost/info.php

You should see a page that displays information about your PHP installation, including version, configuration settings, and modules. Now you can start creating your PHP projects in the /var/www/html folder and access it in the browser by going to http://localhost/[your-project-name]

You can also configure your Apache server to run multiple projects by creating virtual host, refer to the apache documentation for more details on that.

Note that the above instructions are for Ubuntu or Debian based distributions, the commands and file paths may vary slightly for other distributions.

Master with Our Course!

Hrs of course contents, Modules, and lessons. Learn from industry experts. Perfect for .

Enroll now to Get Started!


Source link