Setting Up a LAMP Server

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you'll learn how to configure and deploy a LAMP (Linux, Apache, MySQL, PHP) stack to create a dynamic web server. LAMP is a widely used open-source web server environment that combines the Linux operating system, Apache HTTP server, MySQL database, and PHP scripting language. These components form a powerful platform for building web applications. In this lab, we'll set up a LAMP server and deploy a WordPress blog to understand how this stack works.

Preview

admin
add

Tasks

In this project, you will learn to:

  • Set up a LAMP (Linux, Apache, MySQL, PHP) stack on a Linux system.
  • Install and configure Apache as a web server.
  • Start and manage MySQL database service.
  • Install PHP and integrate it with Apache and MySQL.
  • Configure Apache to handle PHP files correctly.
  • Install and configure phpMyAdmin for easy MySQL database management.
  • Install and configure WordPress, a popular content management system, on the LAMP stack.

Achievements

In this project, you will learn:

  • A comprehensive understanding of setting up a web development environment using LAMP stack.
  • Hands-on experience in configuring web servers, databases, and scripting languages for web development.
  • Proficiency in deploying and managing web applications, demonstrated by setting up a WordPress blog platform.

Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux(("`Linux`")) -.-> linux/InputandOutputRedirectionGroup(["`Input and Output Redirection`"]) linux(("`Linux`")) -.-> linux/PackagesandSoftwaresGroup(["`Packages and Softwares`"]) linux(("`Linux`")) -.-> linux/CompressionandArchivingGroup(["`Compression and Archiving`"]) linux(("`Linux`")) -.-> linux/FileandDirectoryManagementGroup(["`File and Directory Management`"]) linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux(("`Linux`")) -.-> linux/UserandGroupManagementGroup(["`User and Group Management`"]) linux(("`Linux`")) -.-> linux/SystemInformationandMonitoringGroup(["`System Information and Monitoring`"]) mysql(("`MySQL`")) -.-> mysql/BasicKeywordsandStatementsGroup(["`Basic Keywords and Statements`"]) linux/BasicSystemCommandsGroup -.-> linux/echo("`Text Display`") linux/InputandOutputRedirectionGroup -.-> linux/pipeline("`Data Piping`") linux/InputandOutputRedirectionGroup -.-> linux/redirect("`I/O Redirecting`") linux/PackagesandSoftwaresGroup -.-> linux/curl("`URL Data Transferring`") linux/PackagesandSoftwaresGroup -.-> linux/apt("`Package Handling`") linux/CompressionandArchivingGroup -.-> linux/tar("`Archiving`") linux/FileandDirectoryManagementGroup -.-> linux/cd("`Directory Changing`") linux/BasicFileOperationsGroup -.-> linux/cp("`File Copying`") linux/BasicFileOperationsGroup -.-> linux/mv("`File Moving/Renaming`") linux/InputandOutputRedirectionGroup -.-> linux/tee("`Output Multiplexing`") linux/UserandGroupManagementGroup -.-> linux/sudo("`Privilege Granting`") linux/BasicFileOperationsGroup -.-> linux/chown("`Ownership Changing`") linux/SystemInformationandMonitoringGroup -.-> linux/service("`Service Managing`") mysql/BasicKeywordsandStatementsGroup -.-> mysql/create_database("`Database Creation`") linux/PackagesandSoftwaresGroup -.-> linux/software("`Linux Software`") subgraph Lab Skills linux/echo -.-> lab-372834{{"`Setting Up a LAMP Server`"}} linux/pipeline -.-> lab-372834{{"`Setting Up a LAMP Server`"}} linux/redirect -.-> lab-372834{{"`Setting Up a LAMP Server`"}} linux/curl -.-> lab-372834{{"`Setting Up a LAMP Server`"}} linux/apt -.-> lab-372834{{"`Setting Up a LAMP Server`"}} linux/tar -.-> lab-372834{{"`Setting Up a LAMP Server`"}} linux/cd -.-> lab-372834{{"`Setting Up a LAMP Server`"}} linux/cp -.-> lab-372834{{"`Setting Up a LAMP Server`"}} linux/mv -.-> lab-372834{{"`Setting Up a LAMP Server`"}} linux/tee -.-> lab-372834{{"`Setting Up a LAMP Server`"}} linux/sudo -.-> lab-372834{{"`Setting Up a LAMP Server`"}} linux/chown -.-> lab-372834{{"`Setting Up a LAMP Server`"}} linux/service -.-> lab-372834{{"`Setting Up a LAMP Server`"}} mysql/create_database -.-> lab-372834{{"`Setting Up a LAMP Server`"}} linux/software -.-> lab-372834{{"`Setting Up a LAMP Server`"}} end

Start Apache

In this step, you'll start the Apache HTTP server. Apache is one of the most widely used web server software due to its flexibility and stability. It's responsible for handling incoming HTTP requests and delivering web pages.

Apache already exists in the environment and can be started directly. Open the terminal and start the Apache service to ensure it's running:

sudo service apache2 start
step1

The service command controls system services. In this case, we're starting Apache.

Verify the Apache installation, first click the "Applications" button and select the "Run Program" option.

step2

Then enter "firefox" to launch the web browser.

step3

Type localhost in the browser and press enter, you should see a default Apache welcome page indicating that Apache is running successfully.

step4

Start MySQL

In this step, you'll activate the MySQL database service. MySQL is a popular relational database used to store data in a structured way.

MySQL already exists in the environment and can be started directly. Start the MySQL service:

sudo service mysql start

The mysql service should be active. This command will start it if it's not already running.

Install PHP

In this step, you'll install PHP, a popular scripting language used for dynamic web applications. It will be configured to work with Apache and MySQL.

Install PHP and its integration components:

sudo apt update
sudo apt install -y php libapache2-mod-php php-mysql
  • php: The main PHP package.
  • libapache2-mod-php: Module for integrating PHP with Apache.
  • php-mysql: Integration library to connect PHP with MySQL databases.

Create a PHP info page to verify the PHP installation:

sudo sh -c "echo '<?php phpinfo(); ?>' > /var/www/html/info.php"

This command uses sh -c to execute a command as a superuser, creating a file named info.php with a basic PHP script that displays information about the PHP environment.

The default directory Apache uses to serve web content is /var/www/html/. This default root directory can be changed by editing the 000-default.conf configuration file found at /etc/apache2/sites-enabled/. To customize the document root, modify the DocumentRoot directive within this configuration file.

Switch to the browser, and access to http://localhost/info.php, this page is now available.

step5

Configure Apache for PHP

In this step, you'll make sure that Apache is configured to process PHP files effectively.

Enable the mpm_prefork module, which is optimized for PHP, and disable the default mpm_event module:

sudo a2enmod mpm_prefork
sudo a2dismod mpm_event
sudo service apache2 restart
  • a2enmod enables specified modules in Apache, and a2dismod disables them.
  • Restarting Apache makes the changes take effect.

Now reload the web page and you will see the change.

step6

Install and Configure phpMyAdmin

In this step, you'll install phpMyAdmin, a web-based tool to manage MySQL databases.

Install phpMyAdmin:

sudo apt install -y phpmyadmin

This command installs the phpMyAdmin package and its dependencies.

During the installation process, select "apache2":

step7

Select "Yes" to configure database configuration:

step8

Enter and remember your phpadmin password:

step9

Comfirm your password:

step10

Include the phpMyAdmin configuration in the Apache configuration file:

echo "Include /etc/phpmyadmin/apache.conf" | sudo tee -a /etc/apache2/apache2.conf
sudo service apache2 restart
  • The Include statement tells Apache to load the phpMyAdmin configuration file.
  • Restarting Apache ensures the new configuration is loaded.

Install and Configure WordPress

In this step, you'll install and configure WordPress, a popular CMS (Content Management System).

Download the latest version of WordPress and extract the archive:

curl -O https://wordpress.org/latest.tar.gz
tar xzvf latest.tar.gz
  • curl -O downloads a file from the specified URL.
  • tar xzvf extracts the downloaded .tar.gz archive.

Create a new MySQL database for WordPress:

mysql -u root -e "CREATE DATABASE wordpress_db;"
  • -u root connects to MySQL as the root user.
  • The CREATE DATABASE command sets up a new database named wordpress_db.

Configure WordPress to connect to the newly created database:

cd /home/labex/project/wordpress
cp wp-config-sample.php wp-config.php
  • cp copies the sample WordPress configuration file to a new file (wp-config.php).

Open the folder, and update the database settings in the new configuration file:

define('DB_NAME', 'wordpress_db');
define('DB_USER', 'root');
define('DB_PASSWORD', '');
step11

Make sure that the database credentials match your setup. Save the file after editing, move the folder to the default directory.

sudo mv /home/labex/project/wordpress /var/www/html/wordpress
sudo chown -R www-data:www-data /var/www/html/wordpress
  • mv moves the WordPress directory to the web server directory.
  • chown changes the ownership of the WordPress directory to the Apache user (www-data), so Apache can access and modify the files.

Finalize WordPress Installation and Create Your First Blog Post

In this step, you'll finalize the WordPress installation through the web interface and create your first blog post.

Open your web browser and navigate to http://localhost/wordpress. This will start the WordPress installation wizard.

Follow the on-screen instructions to select your language, set up the website title, and create a user account with an admin username, password, and email address.

step12
step13

Once the setup is complete, log in to the WordPress dashboard using the credentials you created.

step14
step15

To create a new blog post:

  • Navigate to the Dashboard, find the 'Posts' menu, and click on 'Add New'.
  • Enter a title for your post and write your content in the text editor provided.
  • Once you are satisfied with your content, click on 'Publish' to make your blog post live.
step16

Now, you can view your blog posts on http://localhost/wordpress!

Summary

In this lab, you've learned how to set up a LAMP server by installing and configuring Apache, MySQL, and PHP. You then installed and configured phpMyAdmin for database management and WordPress as a sample application. This hands-on experience provided a comprehensive understanding of how to deploy web applications using the LAMP stack.

Other Linux Tutorials you may like