How to Build Your Own Server Environment for PHP Programming

By Richard Blum

It’s not necessarily recommended for a live production website, but for PHP development work you can build your own web server environment. You don’t even need to have a large server for a personal web development environment — you can build it using your existing Windows or Apple workstation or laptop.

Web servers in Linux

Linux desktops and servers are becoming more popular these days, especially for web development. You can download the Apache, MySQL, and PHP server source code packages and compile them on your Linux system, but unless you need the absolute latest version of things, that’s not the recommended way to do it.

These days, most Linux distributions include packages for easily installing all the components you need for a complete web development environment. For Debian-based Linux distributions (such as Ubuntu and Linux Mint), you use the apt-get command-line tool to install software packages. For Red Hat–based Linux distributions (such as Red Hat, CentOS, and Fedora) you use the dnf command-line tool.

For Debian-based systems, such as Ubuntu, follow these steps to do that:

  1. From a command prompt, install the Apache web server using the following command:

    sudo apt-get install apache2

  2. Install the MySQL server package using the following command:

    sudo apt-get install mysql-server

    During the MySQL server installation, you’ll be prompted for a password to use for the root user account. The root user account in MySQL has full privileges to all tables and objects. Make sure you remember what password you enter here!

  3. Install the PHP packages to install the PHP server and required extensions, the Apache modifications to run PHP, and the graphical phpMyAdmin tool:

    sudo apt-get install php libapache2-mod-phpsudo apt-get install php-mcrypt php-mysql
    sudo apt-get install phpmyadmin

    The first line installs the main PHP server, along with the Apache module to interface with the PHP server. The second line installs two PHP extensions that are required to interface with the MySQL server. The third line installs the web-based phpMyAdmin PHP program, which provides a web interface to the MySQL server.

  4. Open a browser and test your environment.

    You should be greeted by the phpMyAdmin administration window.

  5. Log in using the MySQL root user account and the password you supplied when you installed MySQL.

    This image shows the main phpMyAdmin web page, which shows what versions of the Apache, PHP, and MySQL servers are running.

phpMyAdmin
The main phpMyAdmin web page showing everything that is running.

For Red Hat–based systems, such as Fedora and CentOS, follow these steps to load LAMP:

  1. From a command prompt, install the Apache web server using the following commands:

    sudo dnf install httpd
    sudo systemctl enable httpd
    sudo systemsctl start httpd

    The httpd package includes the Apache2 web server. The executable file for Apache is named httpd (thus, the name of the package). The package doesn’t start the Apache web server by default, so the second two lines use the systemctl utility to enable the service so it starts automatically at boot time and then starts it.

  2. Install the MySQL server package using the following commands:

    sudo dnf install mariadb-server
    sudo systemctl enable mariadb
    sudo systemctl start mariadb

    Notice that the Red Hat distribution (and thus CentOS and Fedora) has gone with the MariaDB replacement package for MySQL. When you install MariaDB, the package sets the root user account password to an empty string. This is not recommended if your server is on any type of a network. Fortunately, there’s a quick utility that you can run to change the root user account’s password:

    mysql_secure_installation

    When you run this script, it’ll prompt you to answer a few questions, such as the new password for the root user account, whether to restrict the root user account to only logging in from the local host, whether to remove the anonymous users feature, and whether to remove the test database.

  3. Install the PHP packages using the following commands:

    sudo dnf install php php-mbstring php-mysql
    sudo dnf install phpmyadmin
    sudo systemctl restart httpd

    The PHP server doesn’t run as its own service — the Apache web server spawns it when needed. Because of that, you do need to use the systemctl utility to restart the Apache web server so it rereads the configuration file with the new PHP settings.

  4. Open a browser and test things out here.

    You should see the phpMyAdmin login page.

  5. Log in using the root user account in MySQL along with the password you defined when you installed MySQL.

    The image below shows phpMyAdmin running on a CentOS 7 system.

phpMyAdmin CentOS 7
Viewing the phpMyAdmin main web page on Fedora 27.

By using the distribution software packages for each server, you’re guaranteed that the server will run correctly in your Linux environment. An additional benefit is that the distribution software updates will include any security patches or bug fixes released for the servers automatically.

Web servers in Windows and Mac

Installing and running the Apache, MySQL, and PHP servers in a Windows or Mac environment is very tricky, because there are lots of factors involved in how to install and configure them. For starters, both Windows and macOS come with a web server built in, so if you install the Apache web server you’ll need to configure it to use an alternative TCP port.

Likewise, macOS includes an older version of PHP by default, so if you install an updated version of PHP, things get tricky trying to make sure which version is active.

Because of these complexities, it’s not recommended for beginners to install the Apache, MySQL, and PHP packages separately in the Windows and Mac environments.