A Web Server in Fedora 12

12, Dec 2009


Setting up a Development Web Server

Since I do a lot of web development, it is important for me to have a fully functional development server that I can do all my work on before I push to production. Therefore, I need to install the necessary software to set this up. I work mostly in a PHP/MySQL environment, but I am beginning to utilize Django so I want to set that up additionally. The line below will install everything needed for a basic set-up.

sudo yum install httpd mysql mysql-server php php-mysql phpMyAdmin Django MySQL-python mod_python

Configuring MySQL

MySQL is my database of choice. It is very flexible, and is comes on a standard install of nearly every server. To configure MySQL, start the service by running:

sudo service mysqld start

Then make sure to set a root password like this:

mysqladmin -u root -p 'newpass'
Creating a MySQL User and .my.cnf File

Just like running your computer as root, you don't want to run MySQL as root whenever possible, so it is a good idea to create a MySQL user account for your user name. Once you've logged into MySQL as root, run the following command to create a new user and grant privileges to the databases on it.

GRANT ALL ON *.* TO 'username'@'localhost' IDENTIFIED BY 'password';

Of course, depending on what you plan to do with this user account you may want to adjust the privileges given to it. The above command will allow this new user full control of every database on the server - which may not be a good idea. On a production server you will definitely want to restrict the user accounts as much as possible, and if you've got multiple applications running on the same server, it would probably be worth it to create separate MySQL users with permissions to only modify the databases that its application uses.

Next, you need to create a .my.cnf file in your home directory to allow you to run MySQL from the command line as your user without having to enter any passwords. The most basic .my.cnf file looks something like this (of course, replacing mysql-username and mysql-password with the user name and password created above):

user = mysql-username
password = mysql-password
host = localhost

Writing a Function To Easily Manage the Server

Because I don't want httpd and mysql to be constantly running on my laptop, I've written a function that allows me to easily start, stop, and restart the two core parts of the web server. This is a simple modification of the original script that I wrote some time ago that turned on and off my LAMPP install of PHP and MySQL:

# Allow Access to LAMPP server through sudo command
function server() {
    if [[ $1 == '' ]]; then
        echo 'One of the following options must follow:'
        echo '  start   - start the apache server and mysql'
        echo '  status  - print the current status of the server and mysql'
        echo '  stop    - stop the apache server and mysql'
        echo '  restart - restart the apache server and mysql'
        if [ $1 = 'start' ]; then
            sudo service httpd start
            sudo service mysqld start 
        elif [ $1 = 'status' ]; then 
            sudo service  httpd status
            sudo service mysqld status 
        elif [ $1 = 'stop' ]; then
            sudo service  httpd stop
            sudo service mysqld stop 
        elif [ $1 = 'restart' ]; then
            sudo service  httpd restart
            sudo service mysqld restart 
            echo "The value provided is not one of the expected entries."
            echo '  start   - start the apache server and mysql'
            echo '  status  - print the current status of the server and mysql'
            echo '  stop    - stop the apache server and mysql'
            echo '  restart - restart the apache server and mysql'

Add that function to your .bashrc file, or where ever it may be that you store your custom bash functions. Personally, I like to put everything in a .bash_functions file that I've included in the .bashrc file. Once that's been done, the function becomes available for you at the bash prompt, and you can easialy manage your web server.

Building A Site

We're just about done here. It's now time to start creating the actual web site. The server looks in the /var/www/html directory for files to serve through a web browser, so that is where all the web sites that you develop must live. There is one slight problem though - that directory is owned by root, and your user account will not have any write permissions on it. There's a fix for that though: create a group and add your user account to it, then give that group write permissions on the html directory for development:

sudo groupadd web
sudo useradd -g web cjmaynar
sudo chown -R root:web /var/www/html/
sudo chmod -R g+w /var/www/html/

You must then restart your computer before your account will actually be added to the group that you have just created. Next, I added a symbolic link to the html directory from my home directory:

ln -s /var/www/html/ ~/web

A symbolic link acts as a pointer to the directory it references. Any programs or files within the folder linked will behave as if they are operating from the directory that the symolic link has been placed in. Should you ever need to delete the symbolic link, the files in the target directory will be untouched. For more information on symbolic links, read the article on Wikipedia.

You should now be able to cd in to the ~/web directory and create any files you like within that directory. So, now create an index.php file, and inside of it add this content:

<h1>Hello World</h1>
echo phpinfo();

Then visit http://localhost and you will see the file that you just created.

You've now successfully installed and configured an Apache PHP/MySQL server. Congratulations! Obviously this can be a lot more complex, and this server that we've set up is by no means a production server. There are countless other things that would need to be done to create a more secure environment. This set-up should only be used for testing and local development.


This space intentionally left blank.

Have Something to Say?

Questions? Comments? Concerns? Let me know what you’re thinking.

  • You can use Markdown formatting here.