LAMP Stack Installation on VPS Server (Linux, Apache, MySQL, PHP)
LAMP is a popular open-source software stack used for developing and publishing web applications. This stack consists of the Linux operating system, Apache web server, MySQL database management system, and PHP programming language. LAMP is an ideal solution, especially for dynamic websites and applications. In this article, you will learn how to install the LAMP stack on a VPS (Virtual Private Server) step by step.
1. Introduction: What is LAMP Stack and Why Use It?
LAMP is a widely used platform among web developers. Since each component is open-source and free, it offers a cost-effective solution. Additionally, having a large community provides a great advantage in terms of troubleshooting and support. Each component of LAMP performs a specific task:
- Linux: Serves as the operating system. It forms the basis of the server and is the platform on which all other components run.
- Apache: Is the web server. It receives HTTP requests from users and sends web pages (HTML, CSS, JavaScript) to the browser.
- MySQL/MariaDB: Is the database management system. It stores and manages data in a structured way. It is used to store user data, product information, and other dynamic content of web applications. (MariaDB is an open-source fork of MySQL, and we will use MariaDB instead of MySQL in this article.)
- PHP: Is the programming language. It is used to create dynamic web pages. It is used to retrieve data from the database, process user input, and perform other server-side operations.
The LAMP stack provides a powerful and flexible foundation for dynamic websites, blogs, e-commerce platforms, and other web applications. Thanks to its reliability, performance, and scalability, many large websites and applications run on the LAMP stack.
2. Connecting to the VPS Server and Making Updates
Before starting the LAMP installation, you need to connect to your VPS server and update your system to the latest version. This is important to close security vulnerabilities and achieve the best performance.
2.1. Connecting to the Server with SSH
You will need to use SSH (Secure Shell) to connect to your server. SSH is a protocol that allows you to securely access your server. You can use PuTTY on Windows, and the Terminal application on macOS and Linux to establish an SSH connection.
Connect to your server using the following command (replace username
and server_ip_address
with your own information):
ssh username@server_ip_address
After the connection is established, you will be prompted to enter your server's password.
2.2. Making System Updates
After connecting to the server, make system updates using the following commands:
sudo apt update
sudo apt upgrade
These commands will update the current package list and install the latest versions of the installed packages. During the update process, some packages may need to be restarted. In this case, follow the instructions on the screen.
3. Installing and Configuring the Apache Web Server
Apache will act as the web server and make your website's files accessible over the internet.
3.1. Apache Installation
Use the following command to install Apache:
sudo apt install apache2
After the installation is complete, Apache will start automatically. To check if Apache is running, visit your server's IP address in your web browser. If Apache is installed correctly, you should see Apache's default welcome page.
3.2. Configuring the Firewall
If a firewall (usually UFW) is enabled on your server, you will need to allow Apache. Allow Apache using the following commands:
sudo ufw allow 'Apache Full'
This command will allow both HTTP (80) and HTTPS (443) traffic. To check if UFW is enabled, use the following command:
sudo ufw status
3.3. Creating a Virtual Host
To host multiple websites on Apache, you will need to use virtual hosts. Virtual hosts allow you to redirect different domain names or subdomains to different directories.
First, create a directory to store your website's files (use your own domain name instead of example.com
):
sudo mkdir -p /var/www/example.com/html
Give ownership of the directory to the current user:
sudo chown -R $USER:$USER /var/www/example.com/html
Set the directory permissions:
sudo chmod -R 755 /var/www/example.com
Now, create a virtual host configuration file for Apache (use your own file name instead of example.com.conf
):
sudo nano /etc/apache2/sites-available/example.com.conf
Add the following content to the file (use your own domain name instead of example.com
and www.example.com
):
ServerAdmin webmaster@localhost
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example.com/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
Save and close the file. Then, enable the virtual host:
sudo a2ensite example.com.conf
Disable the default virtual host:
sudo a2dissite 000-default.conf
Restart Apache:
sudo systemctl restart apache2
Now, when you visit your domain name or IP address in your web browser, the files in the /var/www/example.com/html
directory should be displayed.
4. MariaDB Database Installation and Security
MariaDB is the database management system that will be used to store and manage your web applications' data.
4.1. MariaDB Installation
Use the following command to install MariaDB:
sudo apt install mariadb-server
After the installation is complete, MariaDB will start automatically.
4.2. Configuring Security Settings
To increase the security of MariaDB, run the security configuration script using the following command:
sudo mysql_secure_installation
This script will ask you to perform the following steps:
- Enter the current root password (leave blank if not yet set).
- Set a new root password.
- Remove anonymous users.
- Disable remote root access.
- Remove the test database.
- Reload privilege tables.
You can increase the security of MariaDB by carefully following these steps.
4.3. Creating a Database and User
You will need to create a database and user for your web application. Connect to MariaDB as the root user:
sudo mysql -u root -p
Enter the root password. Then, create a database and user using the following SQL commands (use your own information instead of database_name
, username
, and password
):
CREATE DATABASE database_name;
CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON database_name.* TO 'username'@'localhost';
FLUSH PRIVILEGES;
EXIT;
These commands will create a database named database_name
, create a user named username
, and grant all privileges on the database to this user.
5. PHP Installation and Configuration
PHP is the programming language that will be used to create dynamic web pages and interact with the database.
5.1. PHP Installation
Use the following command to install PHP and the necessary modules:
sudo apt install php libapache2-mod-php php-mysql php-cli php-curl php-gd php-intl php-mbstring php-soap php-xml php-zip
This command will install the basic modules of PHP and the modules that will provide integration with Apache.
5.2. Configuring PHP Settings
To configure PHP settings, you will need to edit the php.ini
file. This file contains various settings that control the behavior of PHP. To find the location of the file, use the following command:
php -i | grep "Loaded Configuration File"
Open the php.ini
file with a text editor (e.g., sudo nano /etc/php/7.4/apache2/php.ini
) and edit the following settings according to your needs:
upload_max_filesize
: Determines the maximum size of the upload file.post_max_size
: Determines the maximum size of POST data.memory_limit
: Determines the maximum amount of memory a script can use.display_errors
: Determines whether error messages are displayed (it is recommended to enable it in the development environment, but it is recommended to disable it in the production environment).
After making the changes, restart Apache:
sudo systemctl restart apache2
5.3. Testing if PHP is Working
To test if PHP is installed correctly, create an info.php
file in the root directory of your website (/var/www/example.com/html
):
sudo nano /var/www/example.com/html/info.php
Add the following content to the file:
Save and close the file. Then, visit yourdomain.com/info.php
in your web browser. If PHP is installed correctly, you should see a page showing PHP configuration information. After completing the test, it is recommended to delete this file for security reasons.
6. Conclusion and Summary
In this article, you learned step by step how to install the LAMP stack on a VPS server. By following these steps, you can have a powerful platform for developing and publishing dynamic websites and applications. Remember that security should always be a priority. Update your server regularly, use strong passwords, and disable unnecessary services. Good luck!