How To Setup a Custom Local Webserver Using Apache, MySql And PHP For Offline Development

Share:

In this tutorial we will be discussing on creating a local apache based web-server for development purpose only. We will also create a database server and use PHP as a server side scripting language.

Why Local Web Server ?

  1. No need of a domain and web hosting.
  2. Easy development and testing of web app.
  3. Portable and Updated Components

Here's HOW ?

We will be focusing on windows only.

First Thing First:
We will need the following binaries.
  1. Apache HTTP Server Download Link
  2. MySql Database Server Download Link
  3. PHP Download Link
  4. Visual C++ Runtime Components
Related: Create a Animated Rotating Circle Menu Using Basic CSS

Now, first create a directory where you want to setup the server. ("E:\My Server" :- Example).
Extract the archive, you have downloaded, in the directory. Here, I have used (httpd-2.4.29-o102m-x86-vc14.zip) ,the binaries compiled by Visual Studio 2015. Also install the associated VC++ Redistributable (here VC++ 2014). You can also download the binaries provided by the Apache Foundation.
Also, Download the MySql Archive ( Do not download the instaler, we are trying to create a portable local server). Also, extract it to your server directory. And install required VC++ Redistributable.
Download the prefered PHP version and extract to the Server Directory. Also, install the VC++ Components.

Now, we need to configure each of the componest to work together.
Create two Directory inside the Server Directory (Here - "E:\My Server")
  1. htdocs
  2. data

Configuring Apache:

Open the config file {Server Directory} \ {Apache Directory} \ conf \ httpd.conf. In my case "E:\My Server\https-2.4.27-x86-vc14\Apache24\conf\httpd.conf".  using the text editor Eg: Notepad++, notepad.
Now, search for "${SRVROOT}/htdocs" and replace with "{Server Directory}\htdocs" without the ouote.

Now create a file "http-php.conf" and write the following code.

LoadModule php7_module "{Server Directory}\{PHP Directory}\php7apache2-4.dll"
PHPIniDir "{Server Directory}\{PHP Directory}\php.ini"

SetEnv TMP "{Server Directory}\tmp"

Note : I'm using PHP7 so loading php7apache2-4.dll. Please replace "7" with "5", if using PHP5.

In my case:

LoadModule php7_module "E:\My Server\php-7.1.9-win32-VC14-x86\php7apache2-4.dll"
PHPIniDir "E:\My Server\php-7.1.9-Win32-VC14-x86\php.ini"
SetEnv TMP "E:\My Server\tmp"


Now, add the following line in the main config file i.e. "{Server Directory} \ {Apache Directory} \ conf \ httpd.conf" at the end of file :

# PHP conf
Include conf/httpd-php.conf.

Also in the main config file (httpd.conf), search for "Listen 80"

and replace it with "Listen 127.0.0.1:80"
You can use your any of the desired port example 8080, 4545 ...

Also in the same file search for : "TypesConfig conf/mime.types" and add following line just below it.

AddType application/x-httpd-php .phtml .pwml .php5 .php4 .php3 .php2 .php .inc

Now save this file.

Now, go to PHP directory and rename the "php.ini-development" to "php.ini".

Now, Goto MySql Directory and create a file named "my.ini" and write following line and save it.

[mysqld]
basedir="{Server Directory} \ {MySql Directory}"datadir="{Server Directory} \ data"

Starting HTTP Server:

The best way to start http server is to create a service.

Open Command promt and switch the directory to Apache Bin Directory by typing the following.

cd "{Server Directory} \ {Apache Directory} \ bin"  :- including quote.

Now, type 
httpd -k install -n "{Service Name of your choice}"
it will install a service, ow type
httpd -k start -n "{Service Name of your choice}"
it will start the server.

the command stop,  restart and uninstall will stop or restart the server or uninstall service. Just replace the install or start wih these words in the paraeter above.

Note : You donot need to install the service each time.

Related: Create a Animated Rotating Circle Menu Using Basic CSS

Starting Database Server:

Start Command Promt with administrative right:

cd "{Server Directory} \ {MySql Directory}\bin"

Starting for the first time:

mysqld --initialize-insecure 
\\ NO root password
\\ It will create a database schema with user root and blank password.

Sarting Server:
After changing the base directory  command prompt just type "mysqld" and database server will start.

Each time you start your servers just point command promt to apache and mysql directory and type following commad in cmd:

cd "{Server Directory} \ {Apache Directory} \ bin"
httpd -k start -n "{Service Name of your choice}"
cd "{Server Directory} \ {MySql Directory}\bin"
mysqld

Now, Open your favourite browser and type localhost:YourPort or 127.0.01:YourPort.
In my case localhost:80 or 127.0.0.1:80
Please note that 80 is the default http port so you not need to include 80. If other than 80 just include it.

If you would like phpmyAdmin then just download it and extract to the htdocs directory and point to this directory in browser and you will be done.

Thanks for reading. Hope you guys wont face any issues, if in case just comment below, i will help you to sort it out.

A sample config file of Apache download link.

No comments