dotlinux blog

Installing LAMP (Linux, Apache, MySQL, PHP, and PhpMyAdmin) on Gentoo Linux: A Comprehensive Guide

Gentoo Linux is renowned for its power, flexibility, and performance, largely thanks to its unique Portage package management system and the ability to compile everything from source code with custom USE flags. For developers and system administrators, one of the most common tasks is setting up a web server stack. The LAMP stack—comprising Linux, Apache, MySQL, and PHP—is a classic, robust foundation for hosting dynamic websites and web applications.

This guide will walk you through the process of installing and configuring a complete LAMP stack on your Gentoo system, including PhpMyAdmin for easy database management. We will leverage Gentoo's fine-grained control to build a server tailored to your needs. Be prepared for a compilation-heavy process, but rest assured, the result will be a highly optimized and secure environment.

2026-05

Prerequisites#

  • A working Gentoo Linux installation.
  • Root or sudo access.
  • A basic understanding of the Linux command line.
  • Patience, as compiling packages like MySQL and PHP can take a considerable amount of time.

Table of Contents#

  1. System Preparation: Updating the World
  2. Installing Apache HTTP Server
  3. Installing MySQL Database Server
  4. Installing PHP
  5. Integrating PHP with Apache
  6. Installing and Securing PhpMyAdmin
  7. Final Configuration and Testing
  8. Conclusion
  9. References

System Preparation: Updating the World#

Before installing new packages, it's always a good practice to ensure your system is up to date. This ensures you are building against the latest libraries and security patches.

# Sync the Portage tree
emerge --sync
 
# Update the entire system
emerge -avuDN @world

This process (-avuDN) asks for confirmation, updates packages, and ensures new USE flags are applied. It may take a while.

Installing Apache HTTP Server#

Apache (www-servers/apache) is our web server of choice for this guide.

Step 1: Set USE Flags#

We need to configure USE flags for Apache. We'll enable the threads flag for better performance. Edit the package-specific USE flags.

# You can create a file in /etc/portage/package.use or add a line directly.
# Let's create a file for our LAMP stack uses.
echo "www-servers/apache threads" >> /etc/portage/package.use/lamp-stack

Step 2: Install Apache#

Now, emerge Apache.

emerge -av www-servers/apache

Step 3: Configure and Start Apache#

After installation, we need to configure Apache to start automatically at boot and then start the service.

# Add apache2 to the default runlevel
rc-update add apache2 default
 
# Start the apache2 service now
rc-service apache2 start

You can now test if Apache is running by opening a web browser and navigating to http://localhost. You should see the default Apache test page.

Installing MySQL Database Server#

We will install MySQL (dev-db/mysql). MariaDB (dev-db/mariadb) is a popular drop-in replacement and can be used interchangeably by adjusting the package name.

Step 1: Set USE Flags and Install#

MySQL requires certain USE flags. We'll enable the server profile.

# Add MySQL USE flags
echo "dev-db/mysql server" >> /etc/portage/package.use/lamp-stack
 
# Emerge MySQL (This will take a long time to compile)
emerge -av dev-db/mysql

Step 2: Initialize the MySQL Database#

Before starting the MySQL service, you must initialize the system tables. The exact command depends on the version you installed. For modern MySQL versions, use mysqld:

# For MySQL 8.0 and above:
mysqld --initialize --user=mysql --basedir=/usr --datadir=/var/lib/mysql

A temporary root password will be displayed at the end of this process. Copy it immediately!

For older versions or if the above fails, you might use mysql_install_db.

Step 3: Secure MySQL and Start the Service#

Now, start the service and run the built-in security script.

# Add mysql to the default runlevel and start it
rc-update add mysql default
rc-service mysql start
 
# Run the security script. It will prompt you for the temporary root password.
mysql_secure_installation

Follow the prompts to change the root password, remove anonymous users, disable remote root login, and remove the test database. This is a critical step for server security.

Installing PHP#

PHP is the scripting language that will process our dynamic content. We need to install PHP with the necessary extensions to work with Apache and MySQL.

Step 1: Configure PHP USE Flags#

This is where Gentoo's flexibility shines. We need a comprehensive set of USE flags for a typical web server.

# Edit the PHP USE flags. We'll enable apache2, mysql, and many other common features.
# A good starting point:
echo "dev-lang/php apache2 cgi crypt hash json mysql mysqli pdo session simplexml sockets ssl tokenizer unicode xml xmlreader xmlwriter zip" >> /etc/portage/package.use/lamp-stack

Explanation of key flags:

  • apache2: Builds the Apache module (mod_php).
  • mysql, mysqli, pdo: Provides support for MySQL databases.
  • session, json, zip: Common functionalities needed by web applications.

Step 2: Install PHP#

Now, emerge PHP. This is another lengthy compilation process.

emerge -av dev-lang/php

Integrating PHP with Apache#

To make Apache understand PHP files, we need to enable the PHP module and configure it.

Step 1: Enable the Apache PHP Module#

Gentoo's Apache installation uses a module system. We need to activate the php module.

# Edit the Apache modules file
nano /etc/conf.d/apache2

Find the APACHE2_OPTS variable and add -D PHP to it. It should look something like this:

APACHE2_OPTS="-D DEFAULT_VHOST -D INFO -D SSL -D SSL_DEFAULT_VHOST -D LANGUAGE -D PHP"

Step 2: Configure php.ini#

The main PHP configuration file is /etc/php/apache2-phpX.Y/php.ini (where X.Y is your PHP version, e.g., 8.2). You may want to adjust settings like upload_max_filesize and memory_limit based on your application's needs. For now, the default configuration is sufficient.

Step 3: Restart Apache#

To apply the changes, restart the Apache service.

rc-service apache2 restart

Step 4: Test PHP Processing#

Create a simple PHP info file in Apache's document root (usually /var/www/localhost/htdocs/).

echo "<?php phpinfo(); ?>" > /var/www/localhost/htdocs/info.php

Now, navigate to http://localhost/info.php in your browser. You should see a detailed page about your PHP configuration. For security reasons, remember to delete this file after testing:

rm /var/www/localhost/htdocs/info.php

Installing and Securing PhpMyAdmin#

PhpMyAdmin provides a web-based interface for managing MySQL databases.

Step 1: Install PhpMyAdmin#

First, we need to unmask the package as it might be ~arch (testing).

# Accept the ~amd64 or ~x86 keyword for phpmyadmin
echo "app-admin/phpmyadmin ~amd64" >> /etc/portage/package.accept_keywords
 
# Emerge phpMyAdmin
emerge -av app-admin/phpmyadmin

Step 2: Configure Apache for PhpMyAdmin#

The installation provides an Apache configuration snippet. We can create a symbolic link to enable it.

# Create a symlink in Apache's conf.d directory
ln -s /etc/phpmyadmin/apache.conf /etc/apache2/vhosts.d/phpmyadmin.conf
 
# Restart Apache to load the new configuration
rc-service apache2 restart

Step 3: Secure PhpMyAdmin#

It is vital to secure PhpMyAdmin, as it is a common target for attacks.

Method 1: HTTP Authentication (Recommended for single user) Edit the /etc/phpmyadmin/apache.conf file. Within the <Directory "/usr/share/phpmyadmin"> section, add the following lines:

    AllowOverride None
    AuthType Basic
    AuthName "Restricted Access - PhpMyAdmin"
    AuthUserFile /etc/phpmyadmin/htpasswd
    Require valid-user

Then, create a password file and add a user (e.g., admin):

# Create the password file and add a user. You will be prompted to set a password.
htpasswd -c /etc/phpmyadmin/htpasswd admin

Restart Apache: rc-service apache2 restart. Now, accessing http://localhost/phpmyadmin will require both the HTTP auth and then the MySQL login.

Method 2: Restrict by IP (More secure, for specific networks) In the same Apache configuration section, you can restrict access by IP address.

    Require ip 127.0.0.1
    Require ip 192.168.1.0/24 # Example: allow your local network

Final Configuration and Testing#

  1. Access PhpMyAdmin: Navigate to http://localhost/phpmyadmin. Log in with your MySQL root username and password (or the HTTP auth first, if you set it up).
  2. Create a Test Database/User: Use PhpMyAdmin to create a new database and a dedicated user for it. This is a better practice than using the root account for web applications.
  3. Test a PHP Script: Create a simple PHP file in your web root that connects to the database you just created to verify everything is working together.

Conclusion#

Congratulations! You have successfully installed a fully functional, source-based LAMP stack on Gentoo Linux. The journey through Portage's compilation processes has resulted in a server environment optimized specifically for your hardware. You have a powerful Apache web server, a secure MySQL database, a feature-rich PHP installation, and a managed PhpMyAdmin interface.

The true power of Gentoo lies in your ability to fine-tune this setup further. You can revisit USE flags for any package to enable or disable features, adjust CFLAGS in /etc/portage/make.conf for more aggressive optimizations, and configure each component to meet the exact demands of your applications. Remember to keep your system updated with emerge --sync && emerge -avuDN @world to ensure you have the latest security and feature updates.


References#

  1. Gentoo Wiki: The ultimate resource for all things Gentoo.
  2. Gentoo Handbook: https://wiki.gentoo.org/wiki/Handbook:Main_Page
  3. Apache HTTP Server Documentation: https://httpd.apache.org/docs/
  4. MySQL Documentation: https://dev.mysql.com/doc/
  5. PHP Manual: https://www.php.net/manual/
  6. phpMyAdmin Documentation: https://www.phpmyadmin.net/docs/