In today’s article we will cover the steps on how to install the LAMP (Linux Apache MySQL and PHP) stack on a CentOS 6 based VPS.
LAMP represents a full featured stack containing the most popular web server known as Apache, the most popular database server MySQL and the most popular open-source web programming language known as PHP.
This article shows how to setup LAMP on a CentOS 6 VPS, but the procedure is quite similar for Fedora Linux and Scientific Linux based virtual servers. The first thing we need to do is to make sure our system is fully up-to-date. So, executing the command below will install all available updates:
# yum update -y
After ‘yum’ finished updating your system, let’s proceed with the installation of Apache. Since it is available as a package in the official CentOS repositories the installation of Apache goes like this:
# yum install httpd -y
once the installation is completed add apache to your system’s startup:
# chkconfig --levels 235 httpd on
and finally start it up:
# service httpd start
Now, navigate to http://yourdomain.tld and you should see Apache’s default page in your browser.
The next step is to install PHP as an Apache module (mod_php) so you can run PHP scripts under your Apache web server. To do this execute:
# yum install php -y
after it is installed, create a PHP info page within Apache’s document root (/var/www/html) so you can verify if PHP support is enabled on your system.
# echo -e "<?php\n\tphpinfo();\n?>" > /var/www/html/info.php
once the info.php file is created, reload apache so the newly installed PHP module can take effect.
# service httpd restart
Now open this test.php file in your browser (http://yourdomain.tld/info.php) and you should see various information about your PHP installation.
If you want to have additional PHP modules for example mysql support in your PHP, then you need to install the corresponding PHP module for MySQL. The package is named ‘php-mysql’ and can easily be installed using ‘yum’. The following is a list of the available PHP modules:
php-bcmath => A module for PHP applications using the bcmath library php-cli => Command-line interface for PHP php-common => Common files for PHP php-dba => A database abstraction layer module for PHP applications php-devel => Files needed for building PHP extensions php-embedded => PHP library for embedding in applications php-enchant => Human Language and Character Encoding Support php-gd => A module for PHP applications using the gd graphics library php-imap => A module for PHP applications that use IMAP php-intl => Internationalization extension for PHP applications php-ldap => A module for PHP applications that use LDAP php-mbstring => A module for PHP applications which need multi-byte string handling php-mysql => A module for PHP applications that use MySQL databases php-odbc => A module for PHP applications that use ODBC databases php-pdo => A database access abstraction module for PHP applications php-pear.noarch => PHP Extension and Application Repository framework php-pecl-apc => APC cache optimizing PHP intermediate code php-pecl-memcache => Extension to work with the Memcached caching daemon php-pgsql => A PostgreSQL database module for PHP php-process => Modules for PHP scripts using system process interfaces php-pspell => A module for PHP applications using pspell interfaces php-recode => A module for PHP applications using the recode library php-snmp => A module for PHP applications that query SNMP-managed devices php-soap => A module for PHP applications that use the SOAP protocol php-tidy => Standard PHP module provides tidy library support php-xml => A module for PHP applications which use XML php-xmlrpc => A module for PHP applications which use the XML-RPC protocol php-zts => Thread-safe PHP interpreter for use with the Apache HTTP Server
so pickup the ones you need and install them using ‘yum’, for example:
# yum install php-cli php-common php-gd php-mysql -y
will install PHP command line interface, PHP’s common files and MySQL support for PHP. Do not forget to reload Apache so the new modules can take effect:
# service httpd restart
Next, we are going to install the MySQL server. This can be achieved by doing:
# yum install mysql mysql-server -y
after that add it to your system startup and start the MySQL server using the following commands:
# chkconfig --levels 235 mysqld on # service mysqld start
Once it is started, it is recommended to set the MySQL ‘root’ password, disable remote ‘root’ login, remove anonymous users and remove the ‘test’ database. This can be easily accomplished by executing MySQL’s ‘mysql_secure_installation’ script. So execute:
and just hit ‘ENTER’ when prompted.
With all that in place you have a working LAMP stack on your CentOS 6. A good idea is to install phpMyAdmin so you can easily manage your MySQL databases via a nice frontend written in PHP. So, proceed with installing phpMyAdmin using the following commands:
(at the time of writing this article the latest version of phpMyAdmin is 3.5.1. You can always go to http://sourceforge.net/projects/phpmyadmin/files/phpMyAdmin/ and install the latest version available)
# wget -P /tmp http://prdownloads.sourceforge.net/phpmyadmin/phpMyAdmin-3.5.1-english.tar.gz # tar -zxf /tmp/phpMyAdmin*.tar.gz -C /var/www/html/ # mv /var/www/html/phpMyAdmin-3.5.1-english /var/www/html/dbAdmin # cd /var/www/html/dbAdmin # cp config.sample.inc.php config.inc.php
The last thing you should do is open phpMyAdmin’s configuration file:
# vim config.inc.php
and change the following line:
$cfg['Servers'][$i]['auth_type'] = 'cookie';
$cfg['Servers'][$i]['auth_type'] = 'http';
save the file and navigate to http://yourdomain.com/dbAdmin where you can login using your MySQL ‘root’ user and it’s password.
Before we finish with the setup it is recommended to install some PHP cache engine so it can do some caching and will certainly speed things up. So, install PHP APC cache by executing:
# yum install php-pecl-apc -y
The default APC settings are fine but configuring it to your needs is recommended if you want maximum performance. For a 512MB RAM based CentOS 6 VPS the following APC configuration is optimal and is working perfectly fine. So edit/create ‘/etc/php.d/apc.ini’ containing the following options:
extension=apc.so apc.enabled = 1 apc.shm_segments = 1 apc.shm_size = 32M apc.optimization = 0 apc.num_files_hint = 512 apc.user_entries_hint = 1024 apc.ttl = 0 apc.user_ttl = 0 apc.gc_ttl = 600 apc.cache_by_default = 1 apc.filters = "apc\.php$" apc.slam_defense = 0 apc.use_request_time = 1 apc.mmap_file_mask = /tmp/apc-CentOS.XXXXXX ;OR apc.mmap_file_mask = /dev/zero apc.file_update_protection = 2 apc.enable_cli = 0 apc.max_file_size = 2M apc.stat = 1 apc.write_lock = 1 apc.report_autofilter = 0 apc.include_once_override = 0 apc.rfc1867 = 0 apc.rfc1867_prefix = "upload_" apc.rfc1867_name = "APC_UPLOAD_PROGRESS" apc.rfc1867_freq = 0 apc.localcache = 1 apc.localcache.size = 512 apc.coredump_unmap = 0 apc.stat_ctime = 0
and finally reload Apache so the changes can take effect. Do that by executing:
# service httpd restart
That’s it as far as the installation of the LAMP stack goes. However, in some of the next articles we will describe how you can use the LAMP stack to host multiple domains using Apache’s ‘Virtual Host Directives’ as we showed you how to do that if you’re using the Nginx webserver. We will also describe how to enable SSL (secure socket layer) for your website(s), so stay tuned.
PS. If you liked this post please share it with your friends on the social networks using the buttons on the left or simply leave a reply below. Thanks.