What is a local environment?
For a developer, it is not always possible to get online and put the files on a web host while to test the web application. This requires the developer to setup the environment on the system on which the development is taking place. This setup that allows the developer to test web application on the development machine itself is called a local environment. In this article, we take you through the process of setting up a PHP local development environment using XAMPP.
PHP is a scripting language. It requires a web application server to process the files and provide output content. Hence, setting up a local environment for PHP begins with installation of a server. All the modern web applications today involve dynamic content being loaded from a storage which necessitates a database to be connected with the web application. Both these requirements along with few other features like the database client, command line tools and few others are supplied in a single package by several applications. Some of these are:
Setting up local environment
Here, we are focusing on setting up PHP web development local environment using XAMPP. Before we start, ensure that you do not have either of the following pre-installled:
- Apache Tomcat for PHP
- MySQL DB
- Perl support
These are the components that would be installed by XAMPP simultaneously. All the necessary setup will be done by XAMPP itself. If any of these setups exist, there are chances that they might conflict in the setup process.
To begin with the setup, download a version of XAMPP as per your need from this link. XAMPP is available for different versions of PHP at present. You could choose the version you plan to work with. The setup process remains almost the same for every version.
Installing in Linux builds
The setup for Linux builds is available as a .run file. This is quite a different setup compared to the .deb and .tar.gz packages you encounter in Linux builds. To install XAMPP using this file, it is necessary to provide executable permission to the file. In order to provide executable rights to the file, use the below commands.
sudo chmod +x <setupfilename>
Once the file has been made executable, it is possible to run it like a normal setup file by just opening it. The installation goes pretty straightforward.
Installing in Windows & MacOSX
The installations in Windows and MacOSx are quite similar. All that needs to be done is to download the setup, execute it like an executable. On executing the setup, it will prompt you with the list of components that need to be installed. The steps are shown below:
The XAMPP Core files include the installation of 4 major components and their dependencies:
- Apache Tomcat
These components are all you need to get started with PHP local environment. This is the major reason why it is good to use such integrated installer. In certain cases, XAMPP might show up selection of the above components to selectively install them. Such a selection is helpful when we already have MySQL server installed and do not wish to install a conflicting version.
After the installation
Once installed, all you need to do is to fire up the XAMPP control panel to test the setup. In different operating systems, it would be recognised by a different name. As always, starting up the XAMPP control panel could be tricky in a Linux installation. You could follow the instruction from here to configure the panel application to open up as a GUI tool.
For MacOSX, just use the Quick Search Bar(Ctrl+Space) and look for manager-osx as shown below.
Open up the Manager-OSX.app and you should be greeted with a welcome screen as shown below:
As it can be seen, there are four buttons visible above of which two of them are important for the task at hand. The Go to Application button is useful when the server is running and you wish to jump to a specific application. The Open Application Folder is used to open up the application folder where all the PHP application files are stored. The folder can also be found by navigating to Applications->XAMPP->htdocs. The top bar shows three tabs. Select the Manage Servers tab to go to the server dashboard.
For Windows, simply search for XAMPP Control Panel and open it up. It will open up the control panel with all the server stopped at the moment.
Starting the servers for the first time
Starting and stopping the servers in XAMPP is quite simple. Simply click the start button for the corresponding servers. In case of MacOSX, the dashboard shows only three selections as shown below while in case of Windows, it would display 5 selections as shown below.
To start the server, click Start for the respective servers. For a basic dynamic PHP website, all you need at the moment is to start Apache, Tomcat and MySQL servers. Once started, to verify that they started successfully, open the browser and hit the URL http://localhost/. A dashboard as shown below will open up if the server started fine.
Debugging errors post installation
As with any tool, there are always chances of hitting a deadlock. For instance, numerous XAMPP users complain that their server does not startup and displays an error that the port is already in use while for some of the users the MySQL server won’t start display an error that the port is occupied or another server instance is already running.
Port already in use error
This error is caused when there are other applications already running on the system and using the same port. This conflict usually occurs when the user has Skype running on the system. This is because Apache Tomcat & Skype use the same port – 80 to poll the requests. In such a case, user has got two options.
Error starting MySQL server
This error occurs when you did not install MySQL server using XAMPP or re-installed the MySQL server using XAMPP too. In this case, there is already an instance of server running in the backend when you try to start the MySQL server from XAMPP. This results in a port conflict as well as server process conflict blocking it to start.
In such a case, just hit the URL http://localhost/phpmyadmin to verify that. PhpMyAdmin is a MySQL administrator tool used to work with MySQL database. If the server is running already, PhpMyAdmin should be able to pick it up and connect to the server right away. Thus, you’ll be able to check whether the server is already running or not.
In this article, we elaborately discussed the installation of XAMPP to setup a local PHP development environment. The tutorial explains all the steps in detail along with some trouble shooting tips to help you out. The tutorial covers all the operating systems to ascertain that the operating system difference does not stop you.
For any further help if needed, feel free to comment below.