When i had been asked to install apache, mysql and php in my first job then it had been really hard for me to find a proper article which i could follow blindly and could install the stuff without wasting much time (which you usually don’t have in your first job).

So i thought of writing one full fletched article which you can follow and can install these components without much difficulty and at the same time can understand things which you are doing.

In the default installation of the linux server machine, you will find all the packages installed by the default but we are not going to use them. There are various reasons for the same:

  • Firstly those packages are installed by RPM (redhat package manager) which by default install the complete package with all functionalities, which surely we are not going to need and even if we may need it in future then we can include them later. Not installing the parts which we don’t need make the package lighter for system and good in performance.
  • By using this method we will be installing the package from source hence we can make changes in the source before compiling/installing which we can’t usually do to the already installed packages.

I will be using following versions of the packages for the installation:

There is one thing to note down here is that the php should be installed at the last only. because we are going to need apache and mysql for the installation of php so that php could work with apache and mysql.

Mysql Installation:

  • Download the mysql package from here and un-compress it:
    # tar zxvf <mysql_package_name.tar.gz>
  • After unpacking the package use the configure command. Usually you don’t need anymore parameters than this but u can check the available options with –help and put in there.

    # ./configure –prefix=/usr/local/mysql –localstatedir=/usr/local/mysql/data –with-mysqld-user=mysql –with-tcp-port=3306 –with-unix-socket-path=/tmp/mysql.sock

    –prefix                                 ::     Directory where mysql will be installed
    –localstatedir                     ::     Directory where all your databases will be kept
    –with-mysqld-user           ::     User with which mysql will operate
    –with-tcp-port                   ::     Port on which mysql will start
    –with-unix-socket-path   ::     Location at which mysql will create it’s socket

  • This will take sometime. Basically this will do the check for your system whether the package is compatible for your system and all the stuff which is needed for the working of this package are available on your system and will create some files after the test completed. If this step is done without any errors then that means you are good to go and could move to the next step.
  • Now comes the long part, where the source code is actually compiled and then installed which takes a long time (depends on your hardware too). You can go and take a small coffee break while this executes.
    # make && make install
  • The above step will first do the “make” which will create all the binaries and files for your system in the same directory and “make install” will install those files to the location specified in “./configure” command. The “&” here will make sure that “make install” will run only when “make” returns no errors. If “make” returns any errors then “make install” will not be installed.

Configuring Mysql:

So, Mysql is installed. Now we need to configure it.

  • Now copy the configuration file for mysql so that you could configure the various parameters like memory, thread etc.
    # cp support-files/my-medium.cnf /usr/local/mysql/data/my.cnf
  • Now open this file and update the various parameters needed.
  • Then go to the installation directory and install the minimum database needed for mysql to work.
    # cd /usr/local/mysql
    # bin/mysql_install_db                         # this will install the DB’s in data directory
  • Now change the ownership and permissions of the mysql installation directory and data directory
    # chown -R mysql.root /usr/local/mysql
    # chown -R mysql.mysql /usr/local/mysql/data
    # chmod 770 /usr/local/mysql/data
  • Now add the modules path of mysql to /etc/ld.so.conf. My this you could use the mysql modules in the system anywhere.
    # echo “/usr/local/mysql/lib/mysql” >> /etc/ld.so.conf
    # ldconfig
  • Everything is done. Now you can start the mysql process.
    # /usr/local/mysql/bin/mysqld_safe –user=mysql &
  • This should start the mysql process. “&” makes sure that the process runs in the background and don’t get killed as soon as you leave the shell.
  • If you want to check whether your mysql server is accepting the connections then you can follow different things. One which i uses is to check whether the 3306 port is open
  • This should return you the mysql process with the port it is using.

    # netstat -npl | grep mysql
  • After this please change the root password for mysql to prevent any unauthorized access.
    # /usr/local/mysql/bin/mysqladmin -u root password new_password

This finishes the complete mysql installation. Now let’s move to the apache installation.

Apache Installation:

  • Download the Apache package from here and un-compress it
    # tar zxvf <Apache_package_name.tar.gz>
  • Run the configure command.

    # ./configure –prefix=/usr/local/apache –enable-so –enable-rewrite –enable-ssl –enable-expires –with-ssl=/usr/lib/openssl –enable-deflate

    –prefix                   ::     installation directory for apache
    –enable-so            ::     It will provide the dynamic module support in apache.
    –enable-rewrite   ::     it will compile the rewrite module which is used to rewrite the URL’s when needed
    –enable-ssl           ::     compile the ssl support in apache, something u may need later
    –enable-expires   ::     enable the support of adding the expire headers to the apache module
    –with-ssl               ::     destination of the open-ssl installed in your system
    –enable-deflate   ::     compression module. used to compress pages when served to clients

  • When this comes fine with no errors then do the make and install the package.
    # make && make install
  • The installation for the apache is done and it should we good to go ahead and start the apache server but before doing that there are few things you would like to do in the configuration of apache.
    1. Adding the 404 and 403 redirects to your configuration file (/usr/local/apache/conf/htdocs).
    2. ErrorDocument 404 http://www.geekride.com/
      ErrorDocument 404 http://www.geekride.com/

      This will make sure that if any clients lands on a documents on your web server which doesn’t exist then he will redirected to the URL given over here.

    3. Disable the directory listing in your web server
      <Directory />
      AllowOverride None
      Options -Indexes

      You should be able to find this block in your apache configuration file. You just need to add “-” in front of Indexes directive. This will disable the directory listing. If you don’t find this block in your configuration file then feel free to add it

  • That’s all. Now go the installation directory and test the configuration and then start the web server
    # cd /usr/local/apache/bin
    # ./apachectl configtest                         # This will test the configs of your apache server
    # ./apachectl start
  • Now the same process to test whether your apache had started or not.
  • # netstat -npl | grep httpd

    This should show you the apache/httpd process acquiring the port 80.

Your apache is fully functional and working. Now we will move to the installation of PHP.

PHP Installation:

This is the last step for your system getting fully functional for LAMP operations.

  • Download the php package from here and un-compress it.
    # tar zxvf <PHP_package.tar.gz>
  • Compile the source with these arguments

    # ./configure –prefix=/usr/local/php –with-apxs2=/usr/local/apache/bin/apxs –with-mysql=/usr/local/mysql –with-zlib

    –prefix                           ::     Installation directory for php
    –with-mysql                 ::     Location where you had installed the mysql earlier
    –with-apxs2                 ::     Location of apxs binary of apache. This will create the php module for apache
    –with-zlib                     ::     This will install the php with compression support

  • After compilation do the installation
    # make && make install
  • Now copy the php.ini file to the installation directory
    # cp php.ini-recommended /usr/local/php/lib/php.ini
  • Now open this file in your favorite editor (mine is vim) and do the following steps
    # vim /usr/local/php/lib/php.ini
    find the “doc_root” section and enter the correct path for the directory
    which serves your web content, such as;
    doc_root= “/usr/local/apache/htdocs/”
    file_uploads=Off    ( for security reasons)

  • Now if you will go into the module directory of your apache server then you will find a php module over there. Something like “libphp5.so”. Now you need to include this module in apache and do small configuration so that apache could get the intelligence to parse the php files with the help of this module. Open your apache configuration file:

    # vim /usr/local/apache/conf/httpd.conf

    Add this line in the LoadModule section:
    LoadModule php5_module        modules/libphp5.so

    This will load the php module during the startup of apache

    Add this line in the AddType section:
    AddType application/x-httpd-php .php

    This will let the apache know that it is capable of parsing the php files.

  • Now test the configuration of apache and restart it.
    # /usr/local/apache/bin/apachectl configtest
    # /usr/local/apache/bin/apachectl restart|graceful

    That’s it. All the components installed and should be working.

    If you want to test whether your php is working the go to the /usr/local/apache/htdocs directory and run this command.

    # echo ‘<?phpinfo() ?>’ > x.php

    Now point your browser to the location of this file. http://localhost/x.php
    It should give you a page with all the configuration and setting for your php.

Plz feel free to drop a comment if there is something which i missed or if you are stuck at some place.

If you enjoyed this post, make sure you subscribe to my RSS feed!!!!