Linux Consultant

Using Linux, Apache, MySQL, and PHP for Server Applications

Linux, Apache, MySQL, and PHP can all be used to produce software which runs on a web server, and thus eliminates the need for client software installations on a local computer. What this means is that the server contains both the database front end, the database back end, and the client front end all in one package. Upgrades to software based on Linux, Apache, MySQL, and PHP (LAMP) can be done with very little time involved at all, since all of the necessary software will only have to be installed one time, on one computer. In addition, LAMP software could very easily be made available to individuals who are outside of a main office. These workers would simply have to connect to a company's VPN to run the intended software.

This tutorial will cover LAMP installation on a stand alone server, which will be able to run software designed for a LAMP environment. This tutorial will also cover basic MySQL syntax, so that a server administrator can troubleshoot issues within a particular application. This tutorial already assumes that a working installation of Linux is available, and that networking is already setup on the working installation of Linux. For the sake of this tutorial, all of the LAMP components will be installed from source code when needed, and from binary packages when appropriate. This tutorial should be applicable across all distributions of Linux. The first step is to successfully install Apache.



Apache Installation and Configuration


Apache can be downloaded from http://apache.org. In this example, Apache version 1.3.31 is used. First, create a directory on the root of your file system called src.


mkdir /src


Next, move the Apache software to the /src directory, and extract it.


mv apache_1.3.31.tar.gz /src

cd /src

tar -xvzf apache_1.3.31.tar.gz


Once Apache is extracted to the /src/apache_1.3.31 directory (version numbering will probably differ), change to that directory, and configure the Apache HTTP server.


cd apache_1.3.31

./configure --prefix=/usr/local/apache --enable-module=so

make

make install


You will definitely need the Gnu C Compiler installed (GCC), and Gnu Make (make) in order to install Apache. This compilation of Apache will take quite some time, the configure options that were passed to Apache tell the configuration script to install Apache to the /usr/local/apache directory, and to enable module support within Apache. Module support will later be used to load PHP from. Once Apache is installed, the main configuration file should be copied to /usr/local/apache/conf/httpd.conf. You can then test the web server by issuing a start command to the Apache control script, apachectl:


/usr/local/apache/bin/apachectl start


You should now be able to view the default Apache page at the server's ip address, or domain name if applicable. Since Apache is fully up and running, MySQL will be installed next.



MySQL Installation


MySQL can be either installed by source, or by binary packages. Since there is no need for customization to MySQL, MySQL can be safely installed from binary packages for this step. If you are using Red Hat Linux, the RPMS you will need to download include mysql-version, mysql-server-version, and mysql-client-version. You will also need to satisfy any dependencies requested by these packages. If you run Debian, you simply will need to enter one command:


apt-get install mysql-server


Apt-get will then fetch the required dependencies, and install them. Once MySQL is installed, you should first create a password, and a test database. This can be accomplished by:


mysql -u root -p

mysql> SET PASSWORD FOR root@localhost=PASSWORD('desired password');

mysql> create database mydb;

mysql> quit


Once you have created a test database, and assigned a password to the root user, you are ready to install and configure PHP to use MySQL.


PHP Installation and Configuration


PHP will need to be installed from source code, which may be downloaded from www.php.net. The latest version as of this writing is 4.3.7. Move and extract PHP to the same directory used before, /src:


mv php-4.3.7.tar.gz /src

cd /src

tar -xvzf php-4.3.7.tar.gz

cd php-4.3.7

./configure --with-mysql --with-apxs=/usr/local/apache/bin/apxs

make

make install


Once PHP is installed, the configuration file for PHP will need to be copied to the /usr/local/lib directory, and PHP will need to be enabled within Apache. You can safely substitute the vi command below with whatever your favorite text editor is.


cp php.ini-dist /usr/local/lib/php.ini

vi /usr/local/apache/conf/httpd.conf


Create the following lines, in the respective sections of httpd.conf (the proper locations will be under the [main] configuration, and generally don't matter where they are located, but for simplicity, you should place them in the sections which are populated by similar directives).


AddType application/x-httpd-php .php

AddType application/x-httpd-php-source .phps

LoadModule php4_module libexec/libphp4.so


You will also want to modify the DirectoryIndex value to:


DirectoryIndex index.php index.html


Which will allow Apache to fetch the default index.php file when a directory is called which contains a file called index.php. Apache will now need to be restarted, via the apachectl script.


/usr/local/apache/bin/apachectl restart


Next, create a file under the default document directory (default is /usr/local/apache/htdocs) called index.php, and insert the following text into it.


<? print “PHP4 Test”; ?>


Next, view the root directory of your web server, and if everything is working as it should, you should see the text “PHP4 Test”. Finally, you can copy and setup your favorite LAMP software on the server! In the next section I will cover basic MySQL syntax, and how to backup/restore the database.


MySQL Maintenance


MySQL is a very powerful database server, as well as very fast. The basic syntax for SQL is relevant here, so if you already know basic SQL syntax, you may skip this section. The most important commands to run via MySQL is to backup and restore the database.


mysqldump -u root -p mydb > mydb.sql


To restore the MySQL database, you simply redirect the backup file to the database input.


mysql -u root -p mydb <mydb.sql


Basic MySQL syntax is basically standard SQL syntax, there are no real differences between standard SQL syntax, and MySQL syntax. Thus, the following command within MySQL will create a blank table:


CREATE TABLE test (a char(30), b char(30));


This table now has two rows, a and b. In addition, these two rows now have a limitation of 30 characters per value. To delete this table, we will now:


DROP TABLE test;


Or, to insert data into this table, we will use the following command:


INSERT INTO TABLE test VALUES (test,test2);


Which will populate the table test with values of row one being test for a, and test2 for b.