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.
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.
- 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
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 184.108.40.206"
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
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
6. After the best-suited profile for you has been set, update your system and Portage tree.
$ sudo emerge --sync $ sudo emerge --update @world
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
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
9. Before starting Apache service run the following command to avoid ServerName error, then start httpd daemon.
# echo “ServerName localhost” >> /etc/apache2/httpd.conf
# service apache2 start OR # /etc/init.d/apache2 start
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
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"
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
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"
To get a list of installed modules use the following command.
# ls -al /etc/apache2/modules.d/
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
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
16. Before you start MySQL server make sure that MySQL database is installed on you system using the following command.
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
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;
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
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
21. Test PhpMyAdmin login process by opening a browser and use the following URL.
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
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.