Note: Tested on CentOS 6.x 64 bit
One of the basic tasks every Linux Admin will need to perform at some point or another is setting up a LAMP stack. What is a LAMP stack you say? Keep on reading…
Here’s a quick breakdown:
So let’s begin, shall we? First off, this how-to assumes you have a basic CentOS 6.x server ready (guide to come soon on this site). All of the below commands assume you are running as the root user.
# yum install mysql-server php php-mysql httpd
The above command installs the necessary packages and all dependencies for your basic LAMP stack. However, there’s still a few more steps needed before we are good to go.
The first package in the above command is mysql-server, so let’s configure that first and start by starting the mysql server:
# service mysqld start
Before we go any further, let’s secure our installation. Fortunately, there’s an easy way to do that:
# 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...
You shouldn’t have a root password set at this point, so just hit enter. Please enter a complex password when prompted below:
Setting the root password ensures that nobody can log into the MySQL root user without the proper authorisation. Set root password? [Y/n] New password: Re-enter new password: Password updated successfully! Reloading privilege tables.. ... Success!
You can safely select the defaults for the next few questions.
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... ... Success! - 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!
Let’s also enable mysqld to start on boot:
# chkconfig mysqld on
If you’d like to confirm you have MySQL configured properly, you can do the following to login to mysql and show your databases using the password you set during the prior step. You should see something like this:
[root@lampstack ~]# mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 10 Server version: 5.1.69 Source distribution Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | +--------------------+ 2 rows in set (0.00 sec)
Next up is Apache. There really isn’t too much to do here except test that php is configured properly and set apache to start on boot. By default, your document root should be /var/www/html . Place a file in there called index.php containing the following:
Next, start up Apache (you can use start, but out of habit I always do restart:
# service httpd restart
You’ll then want to open a web browser and go to http://yourserverip . You should see something similar to the below:
This tells you that mod_php in Apache is working and shows a ton of options related to PHP as well as what modules are configured. You’ll want to check the output for a section called MySQL. You can verify via the command line as well by running the following (your output might differ slightly):
# php -m | grep mysql mysql mysqli pdo_mysql
You’ll then want to enable httpd (Apache) to start on boot:
# chkconfig httpd on
You should now be ready to install whatever php/mysql applications you might need (e.g. WordPress). Don’t forget to delete the index.php file you created!