LAMP Hands-On Lab
From T5C
This hands-on lab will guide you through the process of preparing the LAMP stack and finally installing the Moodle CMS. Once your Virtual Server has been wiped clean to the Linux post-install state, you are ready to follow these steps to reach the point we were at in our last workshop session.
Contents |
[edit] SSH Reset NOTE
Since the virtual machines were completely erased and reset to their initial state, your SSH client (Linux / MacOS X) may give you a warning regarding the server's identity, like this:
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY! Someone could be eavesdropping on you right now (man-in-the-middle attack)! It is also possible that the RSA host key has just been changed. The fingerprint for the RSA key sent by the remote host is c0:9b:be:db:f5:b0:22:e7:0c:9c:61:a0:76:99:50:0c. Please contact your system administrator. Add correct host key in /home/rtoro/.ssh/known_hosts to get rid of this message. Offending key in /home/rtoro/.ssh/known_hosts:1 RSA host key for vs2.oss.cayey.upr.edu has changed and you have requested strict checking. Host key verification failed.
If this is the case, you can fix the problem by deleting the known_hosts file in your home directory like this:
cd rm -rf .ssh/known_hosts
[edit] MySQL Setup
[edit] Install via yum
To install the MySQL database server via yum, you simply issue the following command:
yum -y install mysql-server
This will download and install MySQL and all required dependencies. That's it! After yum has finished you can proceed to configure the service for standalone operation as we'll see next.
[edit] Disabling MySQL Networking
Since our LAMP server is hosted on a single machine, our MySQL database will never need to be accessed remotely across the network. In light of this, we will configure the MySQL daemon to run without any networking code enabled. This will ensure our database server is un-hackable over the net and will run more efficiently because of the lower executing processes.
We have to edit the MySQL initialization script with this command:
vi /etc/init.d/mysqld
To edit in vi, use these controls:
- Move around with the keyboard arrow keys (up, down, left, right).
- Press i to start inserting text at the current cursor position.
- Press Escape on the keyboard to leave insertion mode.
- Pres x on the keyboard to delete the character at the current cursor position.
- Press : on the keyboard to enter command mode.
- Press Escape on the keyboard to exit command mode.
- Press / on the keyboard to enter search mode. Then enter the text to search and press enter.
- Enter wq in command mode to save and exit.
There's much more to the vi text editor, but this will get you started with basic insertion and deletion functions.
Now find this line (may be slightly different on your install):
/usr/bin/mysqld_safe --defaults-file=/etc/my.cnf --pid-file="$mypidfile" >/dev/null 2>&1 &
And change it to this by inserting the --skip-networking parameter:
/usr/bin/mysqld_safe --defaults-file=/etc/my.cnf --pid-file="$mypidfile" --skip-networking >/dev/null 2>&1 &
Now save and quit with :wq and we are ready to start the MySQL daemon.
[edit] Starting MySQL
To start MySQL, enter the following:
service mysqld start
When you see the [ OK ] message, then MySQL is up and running.
[edit] Setting the MySQL root Password
The very first step after MySQL is running is to set its root password. As in Linux, the root account in MySQL has full control over the database server. It isn't the same account as the Linux root account though, and thus can have a different password.
To set the MySQL root account password type:
mysqladmin -u root password 'VeRyhARdPassWoRD'
where you specify the password as the string between the single quotes.
[edit] Logging In to MySQL
Finally, you can now log in to your brand new MySQL server:
mysql -u root -p
MySQL will prompt for the root password and once you enter that, you'll be greeted with a message somewhat like this:
Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 5 to server version: 4.1.20 Type 'help;' or '\h' for help. Type '\c' to clear the buffer. mysql>
Now your MySQL server is ready to go. But we still have one final step to complete so logout with:
\q
[edit] Starting MySQL Automatically at Boot
Now that we know MySQL is working fine, we have to make it startup automatically at each system reboot or startup:
chkconfig mysqld on
This will make the MySQL daemon start automatically whether its a planned system restart or an unexpected failure or crash.
[edit] Apache Setup
[edit] Install via yum
Installing the Apache web server in CentOS Linux using the yum package managemen tool is as easy as a single line at the command prompt:
yum -y install httpd
That's it! This will download and install Apache, resolving any dependencies that may arise. This will install version 2 of the Apache HTTPD daemon.
[edit] Initial Configuration
Before starting our web server, we need to make some minor changes to it's configuration file so it can be secured and tuned for optimal performance. We will open the file for editing with the vi editor like this:
vi /etc/httpd/conf/httpd.conf
Once in vi, find the line that contains the line:
ServerAdmin root@localhost
and change it to specify your e-mail address as the real server admin's address:
ServerAdmin webmaster@oss.cayey.upr.edu
Then find the line:
ServerTokens OS
and change it to:
ServerTokens Prod
This will hide dangerous details about your web server from would-be hackers trying to find vulnerabilities in your system. A second step in this direction is to find the line:
ServerSignature On
and change it to:
ServerSignature Off
So it's even more difficult for hackers to know what server you're running.
Finally, you need to comment out the following lines by placing a pound sign (#) as the very first character of each line:
LoadModule proxy_module modules/mod_proxy.so LoadModule proxy_ftp_module modules/mod_proxy_ftp.so LoadModule proxy_http_module modules/mod_proxy_http.so LoadModule proxy_connect_module modules/mod_proxy_connect.so
will become:
#LoadModule proxy_module modules/mod_proxy.so #LoadModule proxy_ftp_module modules/mod_proxy_ftp.so #LoadModule proxy_http_module modules/mod_proxy_http.so #LoadModule proxy_connect_module modules/mod_proxy_connect.so
Now your server will not function as a web proxy, which can make it a target for numerous remote attacks.
Once you've made these changes to the configuration file, you can save them and exit vi (:wq) and we are now ready to start the HTTP daemon.
service httpd start
You can now open the Firefox web browser and navigate to:
http://yourservername
to see the default Apache welcome page. Congratulations! You have successfully setup your Apache server. To publish web pages or other types of files, simply put them in the following directory:
/var/www/html
To remove the default welcome page and have your own homepage displayed when someone enters your domain name, name your homepage file:
index.html
Finally, to have the http daemon start automatically when the server boots, enter the following command:
chkconfig httpd on
and the daemon will start at each server boot.
[edit] PHP Setup
[edit] Install via yum
The CentOS yum repositories contain the PHP packages and the additional extensions that specific applications may require. Moodle is one such application, requiring a set of extensions to be installed with the core PHP package for the proper operation of the system. The following command installs all of these components at once:
yum -y install php php-gd php-imap php-mysql php-mbstring php-mcrypt
Note that the php-mcrypt package is only available via the CentOS Plus repository so make sure to enable this repo in the file /etc/yum.repos.d/CentOS-Base.repo .
That's all there is to it! Once yum finishes processing, PHP and the required extensions are now installed on your system.
[edit] Configuring PHP
PHP may be installed but unless you restart the Apache web server, it won't have any effect on the system. Before restarting Apache to activate PHP, we must make some changes to the PHP configuration file. Edit the file with vi:
vi /etc/php.ini
There are three configuration variables whose values have to be modified for the future Moodle installation. These are located in different parts of the php.ini file so you need to search using vi's "/" command.
memory_limit = 64M upload_max_filesize = 128M post_max_size = 128M
These settings will allocate the necessary resources to PHP for a proper Moodle installation. Once you specify these values, save and exit the vi editor and restart Apache with:
service httpd restart
Once Apache has restarted, PHP is now enabled and any files with the ".php" extension that are placed in your web root or beneath will be processed for PHP statements.
[edit] Moodle Setup
[edit] Download Moodle 1.8
Make sure you have wget installed. You can verify by actually trying to install it and yum will let you know if it's already present on the system.
yum -y install wget
Now, move to your home directory and download Moodle 1.8+ from the Moodle repository:
cd wget http://download.moodle.org/stable18/moodle-latest-18.tgz
[edit] Configure the Databse
Login to your MySQL server with the usual command:
mysql -u root -p
and enter the root password for MySQL. To create the Moodle database, enter the following at the MySQL prompt:
create database moodle;
To create the user and assign the necessary permission for Moodle to access the database, enter the following:
grant all on moodle.* to 'moodleuser'@'localhost' identified by 'secret';
Here, 'secret' is the password for the moodleuser account, so be sure to use a more complex password for a production server. Now we need to make sure the changes are available immediately, so we issue the command:
flush privileges;
and we can exit with:
\q
[edit] Configure the File System for Moodle
We want the Moodle data directory to be outside the Web server's document root, so we will configure it one level above. Move to this directory with:
cd /var/www
and create the directory with:
mkdir moodledata
Now, set the permissions to secure the data that will be contained in this important area:
chgrp -R apache moodledata chmod -R g+w moodledata chmod -R o-rwx moodledata
Next we move into the Web server document root and prepare it for Moodle:
cd /var/www/html
If you created an index.html file previously, remove it with:
rm index.html
and answer 'yes' if Linux asks to confirm. Now let's extract Moodle from the downloaded archive in our home directory:
tar -xzvf /root/moodle-latest-18.tgz
Once the files are extracted, you have to move them to the Apache document root so users don't have to add /moodle to the end of the URL.
mv moodle/* . rm -rf moodle
[edit] Linux Utilities for Moodle
The following command installs some Linux utilities that will enhance Moodle's performance later on by allowing the platform to use these compiled executables instead of the PHP library alternatives.
yum -y install which zip unzip du
You are now ready to continue the Moodle installation from the Web browser side.
[edit] Moodle Web Installer
Now you can open your favorite Web browser (Mozilla Firefox recommended) and point it to the URL of your Virtual Server:
Within UPR Cayey Campus: http://your-virtual-server.oss.cayey.upr.edu Outside of the UPR Cayey Campus: http://your-virtual-server.oss.cayey.upr.edu:your-port-number
NOTE: Please refer to this article to learn about internal versus external Virtual Server URLs.
You will see the first step in the Moodle Web Installer process. In this page, you select the Language for the Installation process. Subsequent steps include instructions on each page, and are practically self-explanatory. We will only mention the most important items to look out for here.
- Specify the correct path to your Moodle data directory created earlier and the URL your users will use to reach your Moodle site. The installer usually does a good job of finding these automagically. For example, in this article we would use '/var/www/moodledata' as our data directory path.
- Specify the same database name, user name, and password you entered at the MySQL prompt earlier in this process. For example, in this article we would enter 'moodle' as the database name, 'moodleuser' as the database user name, and 'secret' as the database password. We would also leave the database engine as MySQL (the default), and the table prefix 'mdl_' is just fine.
- The database host should be left as localhost.
- The Web installer will not be able to write the configuration to the Moodle directory as this directory is only writable by the root user. When you receive this message from the installer, copy the configuration it presents on the same page starting from the line starting with '<?php' to the line starting with '?>' , and perform the following tasks on your Virtual Server:
cd /var/www/html vi config.php
Press i to insert and paste the configuration lines you copied from the Web installer page. Press the 'Esc' key and then enter :wq to save and quit. Now adjust the permissions to secure this sensitive file:
chgrp apache config.php chmod g+r config.php chmod o-rwx config.php
- Return to the Web installer and continue following the on-screen instructions until you arrive at the Admin User configuration page.
- Enter the required information in the fields marked with asterisks and save the profile.
- Enter the global site information as you desire. Remember you can change these options later on, but you must specify preliminary values here.
Once you enter all necessary values, your brand new Moodle site will load into your browser screen. Congratulations! You have successfully installed and configured a Moodle 1.8+ server and the LAMP stack it's running on too!
[edit] Moodle Post-Install Tasks
This section will cover some extra post-installation tasks that need to be performed at the Linux server command line.
[edit] Install Spanish Lang Pack
To have the Spanish translation of the Moodle Site available, you can do the following:
cd wget http://download.moodle.org/lang16/es_utf8.zip cd /var/www/moddledata/lang unzip ~/es_utf8.zip
Now EspaƱol - Internacional (es) is available in the Language -> Language Packs Moodle configuration screen. You can set more language options at Language -> Language settings.
[edit] Configure Cron
There are many periodic maintenance tasks that Moodle performs. But for these to actually take place, Cron must be configured to do so. To setup the Moodle cron job, perform the following steps:
yum install -y vixie-cron crontab -e
This will open the vi editor to edit the Cron tasks table. It's actually an empty file right now, so go ahead and add this single line by pressing the letter i to insert:
*/15 * * * * /usr/bin/php -f /var/www/html/admin/cron.php > /dev/null 2>&1
and then press Esc to exit insert mode. Save and quit with :wq.
This configures cron to run the cron.php maintenance script every 15 minutes. Now restart the cron daemon with:
service crond restart
NOTE: This last command may report a failure when stopping cron (most likely because cron wasn't running). This is OK as long as the starting of cron is successful just after that.

