In this post we are going to complete the LAMP stack on our server. Complete, since we have already a server running Linux.
To complete the LAMP stack, we need to install Apache, MySQL, and PHP.
The commands in this post are intended for Ubuntu 14.04, but will also work on Debian servers.
It is a good habit to update the source.list on your server prior to installing software.
Updating the source.list assures you that you are using the latest version of the software. Otherwise, you might use an already cached, but older, version of the software.
To update the sources.list, type
sudo apt-get update
Now we are ready to install the necessary software.
You do not have to run apt-get update for every single command. Once a day, and only when you are going to use apt-get should be sufficient.
To install Apache, enter:
sudo apt-get install apache2
The server will check any dependencies, list what will be installed, and ask for a confirmation:
Do you want to continue? [Y/n]
The capitalized letter indicates the default choice, which you can select by hitting the enter key.
Let’s check whether we have installed Apache2 succesfully. Just enter the IP address of the server in the address bar of your browser:
Replace the variable $serveripaddress with the actual IP address of your server.
The Apache2 Ubuntu Default Page will appear:
sudo apt-get install php5 libapache2-mod-php5
This command installs the PHP5 core and an Apache library for PHP5 support.
In order to activate PHP5, we need to restart the Apache server:
sudo service apache2 restart
Besides the PHP5 core, there are about 6 dozen modules available for PHP5, each module with a specific function. You can list these modules by entering:
sudo apt-cache search php5-
The server will answer with an impressive list:
php5-cgi - server-side, HTML-embedded scripting language (CGI binary) php5-cli - command-line interpreter for the php5 scripting language php5-common - Common files for packages built from the php5 source php5-curl - CURL module for php5 php5-dbg - Debug symbols for PHP5 php5-dev - Files for PHP5 module development php5-gd - GD module for php5 php5-gmp - GMP module for php5 php5-json - JSON module for php5 php5-ldap - LDAP module for php5 php5-mysql - MySQL module for php5 php5-odbc - ODBC module for php5 php5-pgsql - PostgreSQL module for php5 php5-pspell - pspell module for php5 php5-readline - Readline module for php5 php5-recode - recode module for php5 php5-snmp - SNMP module for php5 php5-sqlite - SQLite module for php5 php5-tidy - tidy module for php5 php5-xmlrpc - XML-RPC module for php5 php5-xsl - XSL module for php5 libphp5-embed - HTML-embedded scripting language (Embedded SAPI library) php5-adodb - Extension optimising the ADOdb database abstraction library php5-apcu - APC User Cache for PHP 5 php5-enchant - Enchant module for php5 php5-exactimage - fast image manipulation library (PHP bindings) php5-fpm - server-side, HTML-embedded scripting language (FPM-CGI binary) php5-gdcm - Grassroots DICOM PHP5 bindings php5-gearman - PHP wrapper to libgearman php5-geoip - GeoIP module for php5 php5-gnupg - wrapper around the gpgme library php5-imagick - ImageMagick module for php5 php5-imap - IMAP module for php5 php5-interbase - interbase/firebird module for php5 php5-intl - internationalisation module for php5 php5-lasso - Library for Liberty Alliance and SAML protocols - PHP 5 bindings php5-librdf - PHP5 language bindings for the Redland RDF library php5-mapscript - php5-cgi module for MapServer php5-mcrypt - MCrypt module for php5 php5-memcache - memcache extension module for PHP5 php5-memcached - memcached extension module for PHP5, uses libmemcached php5-midgard2 - Midgard2 Content Repository - PHP5 language bindings and module php5-ming - Ming module for php5 php5-mongo - MongoDB database driver php5-msgpack - PHP extension for interfacing with MessagePack php5-mysqlnd - MySQL module for php5 (Native Driver) php5-mysqlnd-ms - MySQL replication and load balancing module for PHP php5-oauth - OAuth 1.0 consumer and provider extension php5-pinba - Pinba module for PHP 5 php5-ps - ps module for PHP 5 php5-radius - PECL radius module for PHP 5 php5-redis - PHP extension for interfacing with Redis php5-remctl - PECL module for Kerberos-authenticated command execution php5-rrd - PHP bindings to rrd tool system php5-sasl - Cyrus SASL Extension php5-stomp - Streaming Text Oriented Messaging Protocol (STOMP) client module for PHP 5 php5-svn - PHP Bindings for the Subversion Revision control system php5-sybase - Sybase / MS SQL Server module for php5 php5-tokyo-tyrant - PHP interface to Tokyo Cabinet's network interface, Tokyo Tyrant php5-vtkgdcm - Grassroots DICOM VTK PHP bindings php5-xcache - Fast, stable PHP opcode cacher php5-xdebug - Xdebug Module for PHP 5 php5-xhprof - Hierarchical Profiler for PHP5
Installing them all might be a safe bet, but overdone. For example, installing the php5-sybase module is only sensible when you are planning to install and deploy the Sybase database.
You can either check the requirements of the software that you are planning to use, or ignore this for the moment. You can always install a module later when needed.
Now that we have installed PHP5, we need to verify that it is working. For this purpose we are going to create a web page with a single PHP function:
sudo nano /var/www/html/info.php
The Nano editor opens a blank php file.
Paste the following code into that blank file with Shift+Ctrl+V:
<?php phpinfo(); ?>
Press Ctrl+O to safe the file, and exit with Ctrl+X.
The info.php file is saved in the default document root of the Apache2 server.
Time to test our PHP5 configuration. Enter in the address bar of your browser:
As the name of the phpinfo function implies, it presents us a comprehensive overview of our server configuration. That is also the reason why it is good practice to remove the info.php file as soon as possible. When necessary we can always recreate it.
To remove the file, type on the command line:
sudo rm /var/www/html/info.php
We are ready to continue with the database. Besides the MySQL server itself, we will also install the MySQL module for PHP5. This module makes it possible to communicate with the database via PHP scripts.
sudo apt-get install mysql-server php5-mysql
During the installation of MySQL appears a windows where you have to enter a password for the root. Not the root of you Linux server, but the root of the MySQL database!
Enter your MySQL root password and jump to <Ok> with the TAB-key. A second screen pop-ups where you have to enter the password again for verification purposes.
The MySQL server is now installed, but we need to do a little housekeeping. This requires two more steps.
We need a directory where MySQL can store its data. This step is called initializing the MySQL data directory. Enter:
Finally, we run a script to remove some defaults. This increases the security level of the MySQL database:
This security script will ask you a couple of questions:
Enter current password for root (enter for none):
Change the root password? [Y/n]
When you hit enter, the system will ask you to enter a new password. When you are happy with the MySQL root password as entered in the screen above, reply with ‘n’,
Four more questions will show up. Just enter through all four – or answer with ‘y’.
Here is the complete transcript of the interaction (stripped from a couple of blank lines):
jdoe@vps:~$ sudo mysql_secure_installation NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY! In order to log into MySQL to secure it, we'll need the current password for the root user. If you've just installed MySQL, and you haven't set the root password yet, the password will be blank, so you should just press enter here. Enter current password for root (enter for none): OK, successfully used password, moving on... Setting the root password ensures that nobody can log into the MySQL root user without the proper authorisation. You already have a root password set, so you can safely answer 'n'. Change the root password? [Y/n] n ... skipping. By default, a MySQL installation has an anonymous user, allowing anyone to log into MySQL without having to have a user account created for them. This is intended only for testing, and to make the installation go a bit smoother. You should remove them before moving into a production environment. Remove anonymous users? [Y/n] ... Success! Normally, root should only be allowed to connect from 'localhost'. This ensures that someone cannot guess at the root password from the network. Disallow root login remotely? [Y/n] ... Success! By default, MySQL comes with a database named 'test' that anyone can access. This is also intended only for testing, and should be removed before moving into a production environment. Remove test database and access to it? [Y/n] - Dropping test database... ERROR 1008 (HY000) at line 1: Can't drop database 'test'; database doesn't exist ... Failed! Not critical, keep moving... - Removing privileges on test database... ... Success! Reloading the privilege tables will ensure that all changes made so far will take effect immediately. Reload privilege tables now? [Y/n] ... Success! Cleaning up... All done! If you've completed all of the above steps, your MySQL installation should now be secure. Thanks for using MySQL!
MySQL is installed. Now we are done.
Ready to Run
Even more, we have completed our LAMP stack. The web server is ready for our (first) website.