Setting up a LAMP stack on Arch Linux can be a bit challenging, but the benefits of a Linux, Apache, MySQL, and PHP environment are undeniable. This powerful combination is the backbone of many popular websites and applications. In this tutorial, we’ll walk you through the process of how to set up a LAMP stack on Arch Linux step by step.
Prerequisites
Before you begin, make sure you have Arch Linux installed on your system. You should also have sudo privileges and an internet connection to download and install the necessary packages.
How to Set Up LAMP Stack on Arch Linux
Update Your System
First, it’s important to ensure that your Arch Linux system is up to date. To do this, run the following command:
sudo pacman -Syu
Install Apache on Arch Linux
To install the Apache web server on Arch Linux, execute the following command:
sudo pacman -S apache
Once the installation is complete, enable and start the Apache service:
sudo systemctl enable httpd
sudo systemctl start httpd
To verify that Apache is running, open a web browser and navigate to http://localhost
. You should see the Apache default page.
Install MySQL (MariaDB) on Arch Linux
Arch Linux does not provide MySQL by default, but you can use MariaDB as a drop-in replacement. To install MariaDB, run:
sudo pacman -S mariadb
Next, run the following command to set up the initial database:
sudo mysql_install_db --user=mysql --basedir=/usr --datadir=/var/lib/mysql
Enable and start the MariaDB service:
sudo systemctl enable mysqld
sudo systemctl start mysqld
Secure your MariaDB installation by running the following command and following the prompts
sudo mysql_secure_installation
You can learn more about MariaDB installation in our guide on how to install MariaDB on Arch Linux.
Install PHP on Arch Linux
To install PHP on Arch Linux, execute the following command:
sudo pacman -S php php-apache
Next, you’ll need to configure Apache to use PHP. Open the /etc/httpd/conf/httpd.conf
file with your favorite text editor, such as Vim, and add the following lines at the end of the LoadModule
section:
LoadModule php7_module modules/libphp7.so
Include conf/extra/php7_module.conf
After adding these lines, restart the Apache service:
sudo systemctl restart httpd
To test PHP, create a file named info.php
in the /srv/http/
directory with the following content:
<?php
phpinfo();
?>
Now, open a web browser and navigate to http://localhost/info.php
. You should see the PHP information page.
Configure Firewall
To allow external access to your web server, you’ll need to configure your firewall. If you’re using iptables, you can follow our guide on how to install and configure fail2ban on Arch Linux for an extra layer of security.
For other firewalls, consult their respective documentation for instructions on how to open port 80 (HTTP) and 443 (HTTPS).
Install phpMyAdmin (Optional)
If you want to manage your MySQL databases through a web interface, you can install phpMyAdmin. This is an optional step, but it can make database management much easier, especially if you’re not familiar with command-line database management.
- First, install the necessary packages from the Arch Linux repository using the following command:
sudo pacman -S phpmyadmin
- Next, you need to configure Apache to serve phpMyAdmin. Create a new configuration file for phpMyAdmin by running:
sudo nano /etc/httpd/conf/extra/phpmyadmin.conf
- Add the following content to the
phpmyadmin.conf
file:
Alias /phpmyadmin "/usr/share/webapps/phpMyAdmin"
<Directory "/usr/share/webapps/phpMyAdmin">
DirectoryIndex index.php
AllowOverride All
Options FollowSymlinks
Require all granted
</Directory>
- Save and exit the file by pressing
Ctrl + X
, thenY
, and finallyEnter
. - Now, include the
phpmyadmin.conf
file in the main Apache configuration file by opening it:
sudo nano /etc/httpd/conf/httpd.conf
- Add the following line at the end of the
httpd.conf
file:
Include conf/extra/phpmyadmin.conf
- Save and exit the file.
- Restart Apache to apply the changes:
sudo systemctl restart httpd
- Finally, open your browser and visit
http://your_server_ip/phpmyadmin
. Log in with your MySQL or MariaDB credentials to start managing your databases.
Step 7: Secure Your LAMP Stack
To ensure the security of your LAMP stack, you should take some additional measures:
- Disable root login on Arch Linux.
- Use SSH public key authentication instead of passwords.
- Enable two-factor authentication for extra security.
- Install and configure Fail2Ban to protect your server from brute-force attacks.
- Set up Let’s Encrypt SSL to secure your web server with HTTPS.
By following these steps, you’ll have a fully functional and secure LAMP stack on your Arch Linux server. You can now start deploying your web applications and enjoy the flexibility and power of Arch Linux as a web server platform.