Setting up a LAMP Server with CentOS Part 3

This is the final post on setting up a LAMP server with CentOS. If you haven’t done so already, please read¬†part 1, which shows you how to set up the three main components of a LAMP server, and part 2 which shows you how to configure your set up. In part 3, I will talk about automating part 2 by writing bash scripts to make adding new users easier and installing phpMyAdmin.

Adding a User

The first thing we will want to be able to do is add a user. If you remember the steps in part 2, it took quite a while. In fact it took a whole blog post! Here is a bash script I wrote to make adding a user easier:

#!/bin/bash

HOME_DIR="/home/"
SQLSCRIPT="adduser.sql"
MYSQL=`which mysql`

# clear db script
echo > $SQLSCRIPT

#add the user with their username as password
# -m means create the home directory if it does not exist
useradd -m $1

# set the password to be the same as the username
echo $1 | passwd $1 --stdin

# create public_html folder and set permissions
mkdir $HOME_DIR$1/public_html
chown $1 $HOME_DIR$1/public_html

# copy a hello world php file
cp index.php $HOME_DIR$1/public_html/index.php
chown $1 $HOME_DIR$1/public_html/index.php

# create sftp dir
mkdir $HOME_DIR$1/sftp

# set permissions
chmod 755 $HOME_DIR$1

# write sql script to add user and database
echo "CREATE DATABASE IF NOT EXISTS $1;" >> $SQLSCRIPT
echo "GRANT USAGE ON *.* TO [email protected] IDENTIFIED BY '$1';" >> $SQLSCRIPT
echo "GRANT ALL PRIVILEGES ON $1.* TO [email protected];" >> $SQLSCRIPT
echo "FLUSH PRIVILEGES;" >> $SQLSCRIPT

# run mysql script
echo "Enter the mysql root user password."
$MYSQL -u root -p -e "source $SQLSCRIPT"
echo "Added user $1, created web space and added database."

Save this script as ‘adduser.sh’. To use the script, open a terminal and type:

sh adduser.sh username

Replace username with the username you wish to add. I have added comments in the script so that you know what each line does. Essentially it is running the same commands we ran in part 2, the only minor difference is that the SQL is output to an SQL to a file which is then imported through the mysql command. You will need your database root password. I had to set a variable called ‘HOME_DIR’ which points to the home directory on Linux. There is probably an easier way to find the system’s home directory. The reason I did this is because the physical machine that I needed to run these scripts on had the home directory located at /export/home which is different from the standard /home.

Files that need to be created before you run the script

You may need to create the (blank) ‘adduser.sql’ file, I am not sure if the script does that automatically. You will also have to create an ‘index.php’ file. This is a good file to create to test whether the page loads (permissions set correctly) and if PHP is working properly (it should detect it’s a php file and run it). For my ‘index.php’ all I have is a simple echo statement saying ‘Hello World’. You can have something different if you want, perhaps a welcome page.

Check to see if it worked!

To check to see if the script worked, you should be able to visit http://yourhost/~username and you should see the ‘index.php’ file you created. If not, make sure the script ran properly with no errors. If there were no errors, there could be a problem with your setup, most likely to do with permissions. Like most things Linux related, this could be almost anything due to varying configurations, so I won’t go into any troubleshooting tips here. I’m afraid you’re on your own (or with Google) for that one!

Removing a user

Here is the bash script I wrote to remove a user. As you will see, it pretty much does the opposite of what adding a user does.

#!/bin/bash

HOME_DIR="/home/"
SQLSCRIPT="removeuser.sql"
MYSQL=`which mysql`

echo "Delete user $1? WARNING: You cannot undo this!"
echo "Type 'yes' to remove."
read answer

if [ "$answer" = "yes" ]; then

        # clear sql script
        echo > $SQLSCRIPT

        chown $1. $HOME_DIR$1
        chgrp -R nobody $HOME_DIR$1
        userdel -r $1

        echo "DROP DATABASE IF EXISTS $1;" >> $SQLSCRIPT
        echo "DROP USER [email protected];" >> $SQLSCRIPT

        # run sql script
        echo "Enter the mysql root user password."
        $MYSQL -u root -p -e "source $SQLSCRIPT"

        echo "User $1 deleted and database removed."

else
        echo "OK, user not deleted."
fi

Using the remove user script is as simple as adding one. Simply type:

sh removeuser.sh username

Make sure to type ‘yes’ and not just ‘y’ to confirm removing a user.

Required files

You may also need to create a blank ‘removeuser.sql’ file, in case the script does not generate it. In fact, I recommend doing so as we are dealing with deleting stuff, which is potentially more troublesome than creating stuff.

If everything goes according to plan, you should see a message stating that the user was deleted and no other error messages. Again you will need your MySQL root user password.

phpMyAdmin

Some users won’t want to deal with MySQL through the command line, but thankfully phpMyAdmin is a great solution for this. There are a few different ways to install phpMyAdmin and I recently discovered that you can install it with the package manager ‘yum’. The command to do this would be:

yum install phpmyadmin

Since I did not know this, I will briefly explain how to manually install phpMyAdmin. It is actually incredibly simple, the first thing you have to do is download the latest version from the web site.

The next thing you’ll want to do is extract the files to ‘/var/www/html’. The folder should be named something like ‘phpMyAdmin-4.1.9-all-languages’. You should rename this to ‘phpmyadmin’ (all lowercase) by typing:

mv /var/www/html/phpMyAdmin-4.1.9-all-languages phpmyadmin

You should now be able to visit ‘http://yourhost/phpmyadmin’. This will load phpMyAdmin and everything should be ready to go. You should see a login form which is linked with your MySQL users. You can try it by logging in as root, or testing it with the user you created earlier with the bash script/commands.

If you get any errors with phpMyAdmin, it may be because certain packages for PHP need to be installed or enabled/compiled with PHP. A quick Googling will help you with this, and again, since everyone’s Linux distro varies, I don’t know what packages will need to be installed and it would be out of the scope of this tutorial to cover them all. I will say however, that after you have installed a PHP package, make sure to restart Apache. Remember the command for this is:

service httpd restart

Conclusion

If you have followed all three tutorials, you should now have a LAMP server set up. You should also be able to add users easily with a bash script (and just as easily remove them) that will give them their own user directory on Apache as well as one MySQL database to work with. The setup isn’t perfect and there are some security holes as well as missing features, but this should give you a taste of what it is like to be a web server admin.

This entry was posted in Undergraduate Research and tagged , , , , , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

Post a Comment

Your email is never published nor shared. Required fields are marked *

You may use these HTML tags and attributes <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*
*

Are you human br0? * Time limit is exhausted. Please reload CAPTCHA.