This guide covers building a LAMP server (Linux, Apache, MySQL/MariaDB, PHP) mostly from source. Most guides out there on this topic simply have you using the old versions of Apache, PHP, and database engines that ship with your Linux distribution. This is not ideal for developers that want to test against new and future versions of the various components that make up a typical LAMP stack. Some people are intimidated by the prospect of building software from source code, so this will cover each step as simply as possible. For the most part, you can just copy/paste the commands I list.
Estimated time (for entire guide): 1.25 to 8.5 hours
* differs greatly depending on how much you want/need to install
Articles In This Series
I’ve broken down each step in a separate article for brevity.
- Introduction — this article
- Setup Virtual Machine
- Install Linux
- Development Tools
- Apache HTTP Server
- VirtualBox — a free open-source hypervisor we will use to install our server as a virtual machine. You could use another hypervisor or a physical machine if you prefer; I chose VirtualBox for its simplicity and cross-platform support.
- Apache httpd — the most popular web server in the world.
- MariaDB or MySQL — we will be using one or more versions as our primary database engine.
- Various other databases as needed.
- PHP — we will be using several versions for testing our projects.
- Linux distributions covered:
- CentOS — one of the most popular Linux distributions in the world, especially for web servers.
- Fedora Server — similar to CentOS, but more cutting-edge.
- Ubuntu LTS Server — this distro is quickly catching up to CentOS in popularity.
- Debian — another popular distro, which many other distros are based on, including Ubuntu.
- openSUSE Leap — one of my oldest favorite distros and recently becoming more popular.
- Arch Linux — while I don’t normally recommend it for servers, this has a dedicated following so I figured I may as well include it.
Do You Need All This?
I’m trying to cover a variety of scenarios so you can develop whatever type of web application you need. The bare minimum you need for a LAMP stack is an operating system, Apache HTTP Server, MariaDB (or some other database), and PHP. So you can skip sections which don’t apply to you.
There Are Simpler Guides Out There
This is true, but none of them that I’ve found fully address multiple platforms or database engines. If you want a simple LAMP stack, there are plenty of websites where you can download a virtual appliance. This guide is geared towards those looking to develop web applications that support a wide range of Linux distributions, DBEs, etc. It is also a good educational experience for those new to the world of Linux.
Why Not Just Use a Package Manager?
There are a variety of reasons why using your Linux distribution’s package manager is not ideal. Pre-compiled binaries often are not as high-performing as packages you build from source. They are also often outdated and many contain bugs. Lastly, it makes side-by-side installations of multiple versions impossible (this is particularly important when you want to run multiple versions of PHP, MySQL, etc.).
Filesystem Hierarchy Standard
Most guides on compiling software under Linux ignore the Filesystem Hierarchy Standard. I won’t go into why it is important to respect this, but the main thing we want to avoid is installing anything directly to the
/usr directory. This would cause the package manager to overwrite the compiled software. Some people get confused about the difference between
/opt. Generally, any software you compile should be installed to
/opt. But software which upgrades core operating system tools (like
make) should be installed to
/usr/local. So you can switch between tools provided by your distribution and the compiled ones. You should maintain your original source files in
/usr/local/src to make it easier to reinstall or uninstall the packages you’ve compiled.
There are exceptions to these rules and you will often see people argue about which location is best to install software to. In my experience, the primary advantage of using
/opt is that you can easily remove/reinstall software by deleting the appropriate subdirectory. The advantage of
/usr/local is that it is automatically part of your path on most distributions for binaries and libraries.
Compiling Software (short version)
While the individual steps for every package may differ, the basic steps for compiling software are usually:
- Download and extract source code.
- Create an empty build directory beside the source directory.
../source_dir/configurefrom the build directory.
- Move build directory to
/usr/local/srcand cleanup other files.