Setting up PHP Development & Production Server Environments
As most of you already know, in order for a server to work properly, you need certain software, such as Apache, PHP, etc. Now, I am going to assume that you all know the basics about a server and how they work.
One of the most common server configurations is the LAMP stack (Linux, Apache, MySQL, PHP). This is the setup we are going to be working with, as it is very commonly used and very easy to install and set up.
There are other servers that people like to use such as nginx, etc. In fact, you can find a tutorial on how to use nginx, or if you’re curious, you can also read the comparison between nginx and Apache.
Now, with PHP there are two different types of configurations you should know; production and development. It is best practice — and for security purposes — to turn off all error messages as they can sometimes show a hacker what your server’s vulnerabilities are. And obviously, when you are still developing the site or application, you want the error messages to make debugging easier and to make sure that the application is running perfectly.
For this tutorial, I am going to go through how to setup your development and production server environments properly for PHP development.
Development environment
Windows
Without having to install a Windows server, which is quite costly, you can install one of the several third-party applications to which will get your server environment up for you.
Some of these applications are XAMPP and WAMP. And all these third-party applications do the same thing—install Apache, MySQL, and PHP inside the program.
XAMPP is my personal favorite — which you can download at www.apachefriends.org — as it is cross-platform. XAMPP stands for cross-platform, Apache, MariaDB, PHP, Perl. When installing XAMPP, it is very straight forward (and just in case you need to know, you can install Laravel with XAMP) . Just keep hitting next and agree to everything. However, for the development environment ,you need to install the development tools which just changes the php.ini
file to allow errors message display. If error messages are not displayed, go to c:xampp/php/
and open the php.ini
file, and make sure that everything under error handling and logging (line 463) has been turned on, then restart your Apache server.
Once XAMPP is finished installing, it will open a dashboard which you can use to start, stop, and configure the services.
Once you have installed XAMPP and have it up and running, the webroot is located at c:xampp/htdocs/
this is where you create your site/application
. To view your work, you just enter localhost into your browser address bar and it will immediately launch the index file in htdocs.
Mac
Installing on Mac is much like Windows, the easiest and least expensive way to set up your development environment is to install a third-party application, once again XAMPP can be installed or MAMP.
XAMPP here is also my personal favorite, which you can download at www.apachefriends.org. However, it really doesn’t matter which one you use, they all do the same thing. Also, make sure you install the development files.
The webroot on mac is slightly different as it can be found in applications/xampp/htdocs
, otherwise, it is pretty much the same as on Windows.
Linux (Debian based)
For Linux, unlike Windows and Mac, it makes more sense to not install a third-party application but rather install the software onto your system itself.
You can still install XAMPP if you would prefer to use that instead. But remember, this is a development environment and you should not try use/configure these third-party applications to run as a live server.
Now, before you start installing anything, make sure your system is up to date. You can do so by running the following commands in the terminal:
sudo apt-get update
sudo apt-get upgrade
sudo apt-get dist-upgrade
These will simply just ensure that your system is up to date with the latest version of your software. This also helps make sure you don’t have any dependency issues.
Once your system is up to date, Apache2 should already be installed on most Linux machines, however, if you aren’t sure just run sudo apt-get install apache2
(remember if something is already installed, running the install command will just check if there is an updated version of that software).
Now you will also need to install a database, you can use any you like, however, I use MySQL. To install MySQL run the follow in your terminal:
sudo apt-get install mysql-server
sudo mysql_secure_installation
Follow the prompts and enter a secure password.
Once you are sure you have Apache2 and MySQL installed, then you can install PHP by running
sudo apt-get install php
Now this will install PHP in its default settings, so you will need to locate the php.ini
file which can be located in /etc/php/7.0/apache2
and also in /etc/php/7.0/cli
. You will need to update the “error handling and logging” section (line 392) to display all error. There are comments that will help you make the correct changes, however, a simple rule is; for development all on and for production all off.
You will also want to install phpmyadmin
which will make setting up and altering your database much easier.
To do so is rather simple, just open your terminal and type:
sudo apt-get install phpmyadmin
Once you have done that, you need to configure the Apache configuration file to include phpmyadmin
, this can be done by doing the following:
sudo echo -e "\nInclude /etc/phpmyadmin/apache.conf" >> /etc/apache2/apache2.conf
Now all you need to do is to start Apache and MySQL and everything will be working. Use the following command to do so:
sudo service apache2 start && service mysql start
And there we go all set up on your Linux machine.
Production environment
Now with the production environment, you will only need to use this if you are setting up a live server that is going to be hosting websites or applications, whether you are running a Windows server or a Linux server, the changes are the same. You need to find your php.ini
files on Windows which will be located in your PHP folder and on Linux, it will also be located in you PHP folder. In Linux, the folder will be in the “etc” folder, however, you can also do a search for php.ini
.
The changes that need to be made is under the “error handling and logging” section, and once again, there are clear instructions that are commented out that will help you decide what you need to change. The change you want to make is to turn off all display errors to ensure your users won’t see the error messages and therefore, add security to your system, and also to keep everything looking good even if there is an error. Make sure that those all errors are logged in an error.log
file as this will help you with debugging and also to make a few last minute changes while you set up your site to go live.
Thank you for reading my tutorial and I hope it helps you set up your system for the exact purpose you need.
Author’s Bio
Jarryd Lisher is a web developer specializing in back-end development, specifically PHP and MySQL.
He has lots of experience with other technologies and languages such as AJAX, jQuery, Bootstrap, etc.
Jarryd is a supporter of the open source movement, he loves sharing his knowledge and helping fellow developers out where he can.