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#
- System Preparation: Updating the World
- Installing Apache HTTP Server
- Installing MySQL Database Server
- Installing PHP
- Integrating PHP with Apache
- Installing and Securing PhpMyAdmin
- Final Configuration and Testing
- Conclusion
- 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 @worldThis 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-stackStep 2: Install Apache#
Now, emerge Apache.
emerge -av www-servers/apacheStep 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 startYou 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/mysqlStep 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/mysqlA 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_installationFollow 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-stackExplanation 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/phpIntegrating 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/apache2Find 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 restartStep 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.phpNow, 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.phpInstalling 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/phpmyadminStep 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 restartStep 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-userThen, 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 adminRestart 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 networkFinal Configuration and Testing#
- 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). - 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.
- 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#
- Gentoo Wiki: The ultimate resource for all things Gentoo.
- Apache Guide: https://wiki.gentoo.org/wiki/Apache
- MySQL Guide: https://wiki.gentoo.org/wiki/MySQL
- PHP Guide: https://wiki.gentoo.org/wiki/PHP
- Gentoo Handbook: https://wiki.gentoo.org/wiki/Handbook:Main_Page
- Apache HTTP Server Documentation: https://httpd.apache.org/docs/
- MySQL Documentation: https://dev.mysql.com/doc/
- PHP Manual: https://www.php.net/manual/
- phpMyAdmin Documentation: https://www.phpmyadmin.net/docs/