Installing LAMP (Linux, Apache, MySQL, PHP and PhpMyAdmin) in Gentoo Linux

The maximum performance achieved by compiling software from sources with Gentoo has a minimal impact, if we take as a reference today hardware’s power processing. Then what is the purpose of using Gentoo as a Web server platform you might ask? Well, the most important attribute that Gentoo has is its extreme flexibility that Portage can deliver on specific tasks and the full control that final user can achieve over the entire system, due to the fact that Gentoo it’s compiled and build from sources and doesn’t use pre-packaged binary like the majority of Linux distributions.

Install LAMP in Gentoo Linux
Install LAMP in Gentoo Linux

This guide provides a step by step installation procedure for the famous LAMP stack (Linux, Apache, MySQL, and PHP/PhpMyAdmin) using a minimal Gentoo installation environment.

Requirements

  1. Minimal Gentoo Linux environment installed as in this tutorial (Install Gentoo Linux)

Step 1: Configure Static IP Address

1. Before we proceed with installing LAMP stack the system must be configured with a static IP address, which is a “must” in case of a server. But, before we begin configuring network static settings use the ifconfig command to show Network Interface Cards names.

# ifconfig -a
Check Network Interface Names
Check Network Interface Names

As you can see NIC’s name can differ from other common names used in Linux such as ethX, ensXX or others, so note down this name for further settings.

2. If you previously used an DHCP server for your network, make sure you tear down and disable DHCP Client on your system using the following commands (replace IPs and devices with your settings).

# rc-update del dhcpcd default
# /etc/init.d/dhcpcd stop
# ifconfig eno16777736 down
# ifconfig eno16777736 del 192.168.1.13 netmask 255.255.255.0
# emerge –unmerge dhcpcd

3. Then create a symbolic link from network loopback device with the name of your NIC’s connected interface and create a static configuration file for this device in /etc/conf.d/ path.

# ln -s /etc/init.d/net.lo  /etc/init.d/net.eno16777736
# sudo nano /etc/conf.d/net.eno16777736

Edit this device file with the following configurations.

config_eno16777736="192.168.1.25 netmask 255.255.255.0 brd 192.168.1.255"
routes_eno16777736="default via 192.168.1.1"
dns_servers_eno16777736="192.168.1.1 8.8.8.8"
Add Your Network Settings
Add Your Network Settings

4. After finishing editing NIC’s static configurations, start Network Interface and verify network settings and connection using ifconfig and ping commands and if everything is successfully configured add it to start up process.

# /etc/init.d/net.eno16777736 start
# ifconfig
# ping -c2 domain.tld
# rc-update add net.eno16777736 default
Verify Network Settings
Verify Network Settings

If you want DNS name servers to be system-wide configured edit /etc/resolv.conf file and append nameserver string for every DNS IP address.

Step 2: Install LAMP

5. After you’re done with network settings proceed with installing LAMP stack, but not before you verify Gentoo profiles and update Portage tree and system.

For an Internet-facing server with security patches you will probably want to use a Hardened profile which changes package settings for your entire system (masks, USE flags, etc). Use the following commands to list and change your profile.

$ sudo eselect profile list
$ sudo eselect profile set 11
Verify Gentoo Profiles
Verify Gentoo Profiles

6. After the best-suited profile for you has been set, update your system and Portage tree.

$ sudo emerge --sync
$ sudo emerge --update @world
Update Gentoo Linux
Update Gentoo Linux

7. Now it’s time to proceed with LAMP installation. Verify Apache Web Server documentation for USE flags using emerge –pv command switch, then edit Portage make.conf file with required USE flags before attempting to install it.

# emerge -pv apache
# nano /etc/portage/make.conf
Check Apache USE Flags
Check Apache USE Flags

8. Choose your USE flags for compiling process (you can leave it as it is if your server don’t require certain modules), then install Apache using the following command.

# emerge --ask www-servers/apache
Install Apache in Gentoo
Install Apache in Gentoo

9. Before starting Apache service run the following command to avoid ServerName error, then start httpd daemon.

# echo “ServerName localhost” >> /etc/apache2/httpd.conf
Add ServerName in Apache
Add ServerName in Apache
# service apache2  start

OR

# /etc/init.d/apache2 start
Start Apache Server
Start Apache Server

10. On the next step install PHP dynamic scripting language. Due to PHP modules richness, this tutorial will present you a large modules list used as USE flags, but you should stick with the ones your server configuration requires.

First get USE specific flags for PHP using the below command.

# emerge -pv php
Check USE Flags for PHP
Check USE Flags for PHP

11. Then edit /etc/portage/make.conf file and use the following USE flags for PHP5.5 (USE flags must be on a single line).

USE="apache2 php pam berkdb bzip2 cli crypt ctype exif fileinfo filter gdbm hash iconv ipv6 json -ldap nls opcache phar posix readline session simplexml spell ssl tokenizer truetype unicode xml zlib -bcmath calendar -cdb cgi -cjk curl -debug -embed -enchant -firebird -flatfile -fpm (-frontbase) ftp gd -gmp imap -inifile -intl -iodbc -kerberos -ldap-sasl -libedit libmysqlclient -mhash -mssql mysql mysqli -oci8-instant-client -odbc -pcntl pdo -postgres -qdbm -recode (-selinux) -sharedmem -snmp -soap -sockets -sqlite (-sybase-ct) -systemd -sysvipc -threads -tidy -wddx -xmlreader -xmlrpc -xmlwriter -xpm -xslt zip jpeg png pcre session unicode"

PHP_TARGETS="php5-5"
Add USE Flags in PHP
Add USE Flags in PHP

Another method you can use is by echoing USE flags to include desired PHP modules and options into /etc/portage/package.use file.

# echo “dev-lang/php apache2 cgi ctype curl curlwrappers -doc exif fastbuild filter ftp hash inifile json mysql mysqli pdo pic posix sockets spell truetype xml zip” >> /etc/portage/package.use

12. After you have selected required USE flags using one of the two presented methods, install PHP with the following command.

# emerge --ask dev-lang/php
Install PHP in Gentoo
Install PHP in Gentoo

13. PHP emerging process can take a while depending on your system resources and after it finishes tell Apache to use PHP modules by editing /etc/conf.d/apache2 file and add PHP5 on APACHE2_OPTS directive.

# nano /etc/conf.d/apache2

Make APACHE2_OPTS line look like this.

APACHE2_OPTS="-D DEFAULT_VHOST -D INFO -D SSL -D SSL_DEFAULT_VHOST -D LANGUAGE -D PHP5"
Enable PHP Modules for Apache
Enable PHP Modules for Apache

To get a list of installed modules use the following command.

# ls -al /etc/apache2/modules.d/
List all Installed PHP Modules
List all Installed PHP Modules

14. To test server configuration so far, create a phpinfo file on localhost root directory (/var/www/localhost/htdocs/) and restart Apache service, then point your browser to http://localhost/info.php or http://system_IP/info.php.

# echo "<!--?php phpinfo(); ?-->"  /var/www/localhost/htdocs/info.php
# service apache2  restart

OR

# /etc/init.d/apache2  restart
Verify PHP Information
Verify PHP Information

If you get the same result as the image above then your server is correctly configured. So, we can move forward with MySQL database and PhpMyAdmin installation.

15. Before installing MySQL database verify package USE flags and edit Portage make.conf if required. Use the following commands to verify and install MySQL-server database.

# emerge -pv mysql
# emerge --ask dev-db/mysql
Verify USE Flags for MySQL
Verify USE Flags for MySQL
Install MySQL in Gentoo
Install MySQL in Gentoo

16. Before you start MySQL server make sure that MySQL database is installed on you system using the following command.

# /usr/bin/mysql_install_db
Verify MySQL Installation
Verify MySQL Installation

17. Now start MySQL database and secure it using mysql_secure_installation by changing root password, disable root login outside localhost, remove anonymous user and test database.

# service mysql start
# mysql_secure_installation
Start MySQL Database
Start MySQL Database
Secure MySQL Database in Gentoo
Secure MySQL Database

18. To test database functionality login to MySQL issuing the command below and exit database with quit statement.

mysql -u roo -p
mysql> select user,host from mysql.user;
mysql> quit;
Login into MySQL Database
Login into MySQL Database

19. If you need a graphical interface to manage MySQL server install PhpMyAdmin package by running this commands.

# emerge -pv phpmyadmin
# emerge  dev-db/phpmyadmin
Install PhpMyAdmin in Gentoo
Install PhpMyAdmin in Gentoo

20. After the package has been compiled and installed, create a configuration file for PhpMyAdmin by copying its template file and replace blowfish_secret passphrase using an arbitrary string.

# cp /var/www/localhost/htdocs/phpmyadmin/config.sample.inc.php  /var/www/localhost/htdocs/phpmyadmin/config.inc.php
# nano /var/www/localhost/htdocs/phpmyadmin/config.inc.php
Copy PhpMyAdmin Configuration and Replace Blowfish
Copy PhpMyAdmin Configuration and Replace Blowfish

21. Test PhpMyAdmin login process by opening a browser and use the following URL.

http://localhost/phpmyadmin
Login into PhpMyAdmin Interface
Login into PhpMyAdmin Interface

22. If everything is in place, you might want to automatically start your services after reboot by making them available system-wide using the following commands.

# rc-update -v add apache2 default
# rc-update -v add mysql default
Add Services to System Startup
Add Services to System Startup

That’s all! Now you have a dynamic web environment with Apache, PHP scripting language and relational MySQL database on a high flexible and customizable server platform provided by Gentoo.

If you read this far, tweet to the author to show them you care. Tweet a thanks
Matei Cezar
I'am a computer addicted guy, a fan of open source and linux based system software, have about 4 years experience with Linux distributions desktop, servers and bash scripting.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

2 thoughts on “Installing LAMP (Linux, Apache, MySQL, PHP and PhpMyAdmin) in Gentoo Linux”

Got something to say? Join the discussion.

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.