Best Hookup Apps

LAMP HOWTO - Apache, MySQL, PHP

hookup Me went live in June of 2022, at this time I installed and setup our AMP environment on a pre-installed Centos (Linux) system.

This involved 3 primary applications, Apache which is a web server, MySQL which is a database, and PHP which is a programming language.

Over this last weekend I did the first big upgrade of the environment and thought with this new blog I would detail the steps to make it easier for someone else.

Downloading your source

Create a directory called /work/src, then download and untar your apache, mysql and php tarballs.

Below are three places to look, but ensure you get the latest source.

http://dev.mysql.com/downloads/mysql/5.0.html
http://www.php.net/downloads.php
http://httpd.apache.org/download.cgi

Build and Install MySQL

First create a group called mysql, then a user associated with that group. If you get any errors about the group or user already being there, don't worry and just continue.

groupadd mysql
useradd -g mysql -c "MySQL Server" mysql

Now go to your working directory, which should be something like /work/src/mysql-5.0.51b and we will begin with a very basic configure.

cd /work/src/mysql-5.0.51b

chown -R root:root *

make clean

./configure \
--prefix=/work/apps/mysql-5.0.51b \
--localstatedir=/work/apps/mysql-5.0.51b/data \
--disable-maintainer-mode \
--with-mysqld-user=mysql \
--with-unix-socket-path=/tmp/mysql.sock \
--without-comment \
--without-debug \
--without-bench

Now we're ready to make and install mysql, stay in the same directory.

make && make install

Configure MySQL

MySQL is now installed, but there are a few more steps to get a working database up.

Go to your installed MySQL directory and initialize the database.

cd /work/apps/mysql-5.0.51b
bin/mysql_install_db

Now set correct permissions on your new install.

chown -R root:mysql /work/apps/mysql-5.0.51b
chown -R mysql:mysql /work/apps/mysql-5.0.51b/data

Copy the default configuration for mysql to /etc. Pick whichever you like, but I usually go for at least the large config.

cp share/mysql/my-large.cnf /etc
chown root:sys /etc/my.cnf
chmod 644 /etc/my.cnf

Now we need to create startup scripts so that on each server reboot mysql will restart.

cp share/mysql/mysql.server /etc/rc.d/init.d/mysql
chmod +x /etc/rc.d/init.d/mysql
/sbin/chkconfig --level 3 mysql on

Start MySQL

/etc/rc.d/init.d/mysql start

MySQL Security

Previously I've had to manually change root's password, delete test databases and other little tasks, now you can simply run the below script. You can quite safely answer "Yes" to all questions the script asks you. Make sure your $PATH is set correctly before running.

export PATH=$PATH:/work/apps/mysql-5.0.51b
/work/apps/mysql-5.0.51b/bin/mysql_secure_installation

One final step I like to perform is to remove access to mysql from outside the server. I have two options for doing this, you should only pick one.

1. Allow MySQL to bind to localhost (127.0.0.1) by adding bind address = 127.0.0.1 to the [mysqld] section of your /etc/my.cnf file.
2. Allow socket connections ONLY by unhashing the skip-networking option in your /etc/my.cnf

There can be a lot more to the security of your MySQL server, however these are the basics and should set you up initially.

Create your first database instance and user

You now have your database installed, but still need to create a database instance and a user, otherwise what's the point of the database?

First, create a new database called "NewApp". Try not to add anything other than letters otherwise you may have problems.

mysqladmin -u root -p create NewApp
mysql -u root -p

You should now be in mysql and can create your user and assign that user privileges to the database you just created.

GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER, CREATE TEMPORARY TABLES, LOCK TABLES
ON databasename.* TO 'username'@'localhost'
IDENTIFIED BY 'password';

where
'databasename' is the name of your database
'username@localhost' is the username of your MySQL account
'password' is the password required for that username

Your database is up and ready to go.

Install Apache

Apache is a whole lot easier to install and get up and running than MySQL. The options we are going to use are fairly standard, one to notice is the deflate option. Deflate is going to be used in another post to zip content before it leaves your server which puts a little extra load on your cpu but frees up a lot of bandwidth. You can quite safely remove the deflate line from that configure command if you don't want to use it.

The SO option is so that in the future you can include dynamically loaded modules to apache, which means a smaller footprint right now with the ease of adding functionality later.

Go to your working directory and configure apache.

cd /work/src/httpd-2.2.8

./configure \
--prefix=/work/apps/apache-2.2.8 \
--enable-rewrite \
--enable-deflate \
--enable-so

make && make install

You are done.

Install PHP

This document has been written when PHP5 is what you should be using. Options are set for that version. If you like, you can run ./configure --help and take a look at all the available options to configure php with. However I've provided a fairly comprehensive set of config options, unless you need something specific they should work well for you.

These installs have been done with Drupal in mind, so if you're running drupal, these should work great. I also have a 64bit server, so you may need to take the 64 out of specific variables.

./configure \
--prefix=/EM/apps/php-5.2.6 \
--sysconfdir=/EM/apps/php-5.2.6/etc \
--with-exec-dir=/EM/apps/php-5.2.6/bin \
--with-apxs2=/EM/apps/apache-2.2.8/bin/apxs \
--disable-debug \
--enable-ftp \
--enable-inline-optimization \
--enable-magic-quotes \
--enable-mbstring \
--enable-safe-mode \
--enable-wddx=shared \
--enable-xml \
--with-libdir=lib64 \
--with-gd \
--with-gettext \
--with-freetype-dir=/usr/lib64 \
--enable-gd-native-ttf \
--with-mysql=/EM/apps/mysql-5.0.51b \
--with-mysqli=/EM/apps/mysql-5.0.51b/bin/mysql_config \
--with-regex=system \
--with-jpeg-dir=/usr/lib64 \
--with-png-dir=/usr/lib64 \
--with-zlib-dir=/usr/lib64

make && make install

Configure PHP

There are a couple of things you should do to ensure PHP is ready. Specifically put your config in place and update it.

cp /work/src/php-5.2.6/php.ini-recommended /work/apps/php/etc/php.ini
ln -s /work/apps/php/etc/php.ini /work/apps/php/lib/php.ini

PHP is installed.

Integrate Apache, MySQL and PHP

Now all your applications are installed, you just need to tie them in to work together. You've already
linked PHP to MySQL in PHP's configure command so they know how to communicate, the only thing left is to edit your Apache config to deal with PHP correctly.

Edit your httpd.conf file, located under /work/apps/apache-2.2.8/conf and ensure the following options are set.

The below entry should be in your DSO support section :
LoadModule php5_module modules/libphp5.so

Then you want to make sure that index.php is a valid index file, so ensure that at least index.php and index.html are in the DirectoryIndex setting.

DirectoryIndex index.php index.htm index.html

To ensure that php files are interpreted correctly and not just displayed as text files, you should add the following lines, you'll find the right spot with the other AddType entries.

#
# Interpret PHP
#
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps

Config complete, get ready to start apache.

Start Apache

First step, lets make sure Apache starts on reboot.

ln -s /work/apps/apache-2.2.8/bin/apachectl /etc/rc.d/init.d/apache
ln -s /etc/rc.d/init.d/apache /etc/rc.d/rc3.d/S90apache

Then start it.

/etc/rc.d/init.d/apache start

You Should now be up and running, with whatever application you're hoping to run or just ready to create your own website.

Conclusion

I hope the guide has been useful, it is obviously a little specific with directory names and versions. You should be able to substitute everything and figure out the small differences between my environment and yours.

Should you require help, I can provide a small amount of free assistance. If it becomes too much I'll either need to direct you to a website or a fee will be required.

There are many other options you can use, you can of course get further into the security and setup of each application and find what works best for you.

Thank you for taking the time to read this and I hope it all worked well for you.