Getty Images
How to deploy a LAMP stack
Examine the components and deployment options of a LAMP stack, which provides web hosting, database and scripting platforms that are found in many data centers.
Each layer of a LAMP server is its own independent tool, but the pieces have evolved to ensure four components designed to work as a single system. LAMP servers dominate web deployments, so knowing the basics of each layer is beneficial.
Most websites and cloud resources reside on Linux directly or rely on it indirectly. Many of these deployments use three fundamental components on the Linux OS to provide interactive web-based services to employees and customers.
Collectively, these technologies are referred to as LAMP, which stands for Linux, Apache, MySQL and PHP. If you are a Linux administrator, you should recognize these components and have a basic understanding of how to work with them.
What is LAMP?
Each component of a LAMP stack has a different function:
- Linux. The host operating system upon which the other three applications run.
- Apache. The web server service hosting webpages that provides access to various resources.
- MySQL. The behind-the-scenes database hosting information access through the Apache-hosted website.
- PHP. The server-side programming language that allows for dynamic webpages that can query databases and use application logic.
LAMP servers can scale horizontally and vertically. Deployments can benefit from containerization. Be careful to review all security options and configuration recommendations for any LAMP deployment. Failure to do so can result in data exfiltration, exposure of personal user information, site defacement or other negative consequences.
Linux operating system
The Linux family contains thousands of distributions. These distributions usually have a purpose and contain software designed to help meet that purpose. However, virtually any distribution can serve as a basic LAMP host. Some distributions may include the web server, database or scripting language components. You must add the software to the other versions using a package manager or source files.
If you're going to set up a small LAMP server as a learning tool, consider using Fedora Linux, Ubuntu Linux or openSUSE distributions. These end-user distributions are suitable for small deployments in lab or learning environments.
Business environments will likely use a dedicated server Linux distribution, such as Red Hat Enterprise Linux (RHEL) or SUSE Linux Enterprise Server. Enterprise versions may require additional subscriptions. These distributions tend to scale better, contain high-performance configuration options and may include support.
If your organization has standardized on a particular distribution, you should work with that version. Most distributions maintain effective documentation to guide you through the installation process. You could do a bare metal install on a physical device or a virtual machine deployment using software such as Oracle VirtualBox, Parallels or Boxes.
Apache web server
The next layer in the stack is the web server service that hosts the site. This service is usually Apache, though nginx and other similar services are available. Apache is a powerful, extensible, open source web server capable of hosting one or more sites. It's preinstalled on some Linux distributions. It's available as a software package or source code.
Use your selected distribution package manager if you need to install Apache. On RHEL, Fedora, Rocky and similar distributions, type the following:
$ sudo dnf install httpd
If you're using Debian, Ubuntu, Mint or other distributions that rely on APT, type the following:
$ sudo apt install apache2
The two different Apache package names between DNF and APT are based on historical choices for service naming. They have no real effect on the service but do vary by distribution.
Use the systemctl
command to manage Apache. To start Apache and cause it to launch when the system boots on a Red Hat-based system, type these two commands:
$ sudo systemctl start httpd
$ sudo systemctl enable httpd
Restart the service after changing the configuration file with the following command:
$ sudo systemctl restart httpd
In the previous commands, substitute apache2
for httpd
for Debian-derived distributions.
The basic Apache configuration file is usually found at /etc/httpd/conf/httpd.conf or /etc/apache2/apach2.conf.
Common settings within the file include the following:
- Which IP address and port Apache listens on.
- The document root (where you can find the website files).
- Virtual host configurations and files if the server hosts multiple sites.
After you start the service, browse to http://{local-IP-address} to view the Apache test page. Configure the server's firewall to permit HTTP/HTTPS connections.
Securing web servers is beyond the scope of this article. Be certain you've hardened your LAMP server before you expose it to production environments or the internet.
MySQL database
The next LAMP server component is a database back end. The MySQL database is available as a supported enterprise edition or an open source community version. You can download the community version here.
Download the appropriate .rpm file to install MySQL. Then, type the dnf
or yum
command to install it. For example, to add MySQL to a RHEL 9 server, type the following:
$ sudo dnf install mysql84-community-release-el9-1.noarch.rpm
The related command on a Debian-based distro is as follows:
$ sudo apt install mysql-apt-config_08_30-1_all.deb
You will typically manage your MySQL database from a client device that also has MySQL installed. The MySQL documentation includes a comprehensive tutorial and lots of configuration information. Use good administrative passwords with MySQL and keep it up to date.
Database services provide the hosted sites with access to real-time inventory, customer and other information based on specific end-user queries. For example, the database may host an inventory of available products customers can order through the website.
PHP scripting language
The open source Hypertext Preprocessor (PHP) utility provides the dynamic website's programming language/scripting layer. PHP generates dynamic pages based on user requests or other criteria. PHP code is embedded in the HTML code that structures and formats the page, but all PHP processing occurs on the server. The resulting information is delivered to the client device via HTML.
Download PHP to your LAMP server as a compressed file to begin the installation process. The PHP community provides extensive documentation covering its deployment, configuration and use.
PHP's primary role is to link the content of the MySQL database with the information the user needs. Exactly what this database connection provides varies by site. In some cases, PHP is replaced or supplemented by Python or Perl.
Use current versions of PHP to avoid vulnerabilities. Code reviews should be a deliberate part of your deployment as most issues arise from insecure programming practices.
The best way to learn PHP is by doing projects. Begin with the installation but be sure to follow tutorials and online guides to learn configuration options.
Damon Garn owns Cogspinner Coaction and provides freelance IT writing and editing services. He has written multiple CompTIA study guides, including the Linux+, Cloud Essentials+ and Server+ guides, and contributes extensively to TechTarget Editorial and CompTIA Blogs.