Article
Build Your Own Database Driven Web Site Using PHP & MySQL, Part 1: Installation
Linux Installation
This section will show you the procedure for manually installing Apache, PHP, and MySQL under most current distributions of Linux. These instructions were tested under Ubuntu 8.10; however, they should work on other distributions such as Fedora, Debian, openSUSE, and Gentoo without much trouble. The steps involved will be very similar, almost identical.
Most Linux distributions come with a package manager of one kind or another. Ubuntu’s Synaptic Package Manager is a graphical front end to APT, the Debian package manager. Other distributions use the older RPM package manager. Regardless of which distribution you use, prepackaged versions of Apache, PHP, and MySQL should be readily available. These prepackaged versions of software are really easy to install; unfortunately, they also limit the software configuration options available to you. For this reason—and because any attempt to document the procedures for installing the packaged versions across all popular Linux distributions would be doomed to failure—I will instead show you how to install them manually.
If you already have Apache, PHP, and MySQL installed in packaged form, feel free to use those versions, and skip forward to the section called “Post-Installation Set-up Tasks”. If you encounter any problems, you can always uninstall the packaged versions and return here to install them by by hand.
Installing MySQL
Start by downloading MySQL. Simply proceed to the MySQL Downloads page and click the Download link for the free MySQL Community Server. This will take you to a page with a long list of download links for the current recommended version of MySQL (as of this writing, it’s MySQL 5.1).
Click the link near the top of the list to go to the Linux (non RPM packages). Now you need to choose the package that corresponds to your system architecture. If you’re positive you’re running a 64-bit version of Linux, go ahead and download the Linux (AMD64/Intel EM64T) package (about 120MB in size). If you’re running a 32-bit version of Linux, download the Linux (x86) package (about 115MB)—it’ll work even if it turns out you’re running a 64-bit version of Linux. It may be a little unclear, but the Pick a mirror link shown in the figure below is the one you need to click to download the file.
![]()
Once you’ve downloaded the file, open a Terminal and log in as the root user:
user@machine:~$ sudo su
You will, of course, be prompted for your password.
Change directories to /usr/local and unpack the downloaded file:
root@machine:/home/user# cd /usr/local
root@machine:/usr/local# tar xfz ~user/Desktop/mysql-version-linux-platform.tar.gz
The second command assumes you left the downloaded file on your desktop, which is the Desktop directory in your home directory. You’ll need to replace user with your username, version with the MySQL version you downloaded, and platform with the architecture and compiler version of the release you downloaded; this is so that the command exactly matches the path and filename of the file you downloaded. On my computer, for example, the exact command looks like this:
root@mythril:/usr/local# tar xfz ~kyank/Desktop/mysql-5.1.34-linux-x86_64-glibc23.tar.gz
After a minute or two, you’ll be returned to the command prompt. A quick ls will confirm that you now have a directory named mysql-version-linux-platform. This is what it looks like on my computer:
root@mythril:/usr/local# ls
bin games lib mysql-5.1.34-linux-x86_64-glibc23 share
etc include man sbin src
Next, create a symbolic link to the new directory with the name mysql to make accessing the directory easier. Then enter the directory:
root@machine:/usr/local# ln -s mysql-version-linux-platform mysql
root@machine:/usr/local# cd mysql
While you can run the server as the root user, or even as yourself (if, for example, you were to install the server in your home directory), you should normally set up on the system a special user whose sole purpose is to run the MySQL server. This will remove any possibility of an attacker using the MySQL server as a way to break into the rest of your system. To create a special MySQL user, type the following commands (still logged in as root):
root@machine:/usr/local/mysql# groupadd mysql
root@machine:/usr/local/mysql# useradd -g mysql mysql
Now give ownership of your MySQL directory to this new user:
root@machine:/usr/local/mysql# chown -R mysql .
root@machine:/usr/local/mysql# chgrp -R mysql .
MySQL is now installed, but before it can do anything useful, its database files need to be installed, too. Still in the new mysql directory, type the following command:
root@machine:/usr/local/mysql# scripts/mysql_install_db --user=mysql
Now everything’s prepared for you to launch the MySQL server for the first time. From the same directory, type the following command:
root@machine:/usr/local/mysql# bin/mysqld_safe --user=mysql &
If you see the message mysql daemon ended, then the MySQL server was prevented from starting. The error message should have been written to a file called hostname.err (where hostname is your machine’s host name) in MySQL’s data directory. You’ll usually find that this happens because another MySQL server is already running on your computer.
If the MySQL server was launched without complaint, the server will run (just like your web or FTP server) until your computer is shut down. To test that the server is running properly, type the following command:
root@machine:/usr/local/mysql# bin/mysqladmin -u root status
A little blurb with some statistics about the MySQL server should be displayed. If you receive an error message, check the hostname.err file to see if the fault lies with the MySQL server upon starting up. If you retrace your steps to make sure you followed the process described above, and this fails to solve the problem, a post to the SitePoint Forums will help you pin it down in little time.
If you want your MySQL server to run automatically whenever the system is running, you’ll have to set it up to do so. In the support-files subdirectory of the mysql directory, you’ll find a script called mysql.server that can be added to your system startup routines to do this. For most versions of Linux, you can do this by creating a link to the mysql.server script in the /etc/init.d directory, then create two links to that: /etc/rc2.d/S99mysql and /etc/rc0.d/K01mysql. Here are the commands to type:
root@machine:/usr/local/mysql# cd /etc
root@machine:/etc# ln -s /usr/local/mysql/support-files/mysql.server init.d/
root@machine:/etc# ln -s /etc/init.d/mysql.server rc2.d/S99mysql
root@machine:/etc# ln -s /etc/init.d/mysql.server rc0.d/K01mysql
That’s it! To test that this works, reboot your system, and request the status of the server with mysqladmin as you did above.
One final thing you might like to do for the sake of convenience is to place the MySQL client programs—which you’ll use to administer your MySQL server later on—in the system path. To this end, you can place symbolic links to mysql, mysqladmin, and mysqldump in your /usr/local/bin directory:
root@machine:/etc# cd /usr/local/bin
root@machine:/usr/local/bin# ln -s /usr/local/mysql/bin/mysql .
root@machine:/usr/local/bin# ln -s /usr/local/mysql/bin/mysqladmin .
root@machine:/usr/local/bin# ln -s /usr/local/mysql/bin/mysqldump .
Once you’ve done this, you can log out of the root account. From this point on, you can administer MySQL from any directory on your system:
root@machine:/usr/local/bin# exit
user@machine:~$ mysqladmin -u root status