How to install Apache, PHP and MySQL on Linux: Part 1

This tutorial explains the installation of Apache web server, bundled with PHP and MySQL server on a Linux machine. The tutorial is primarily for SuSE 9.2, 9.3, 10.0 & 10.1 operating systems, but most of the steps ought to be valid for all Linux-like operating systems.

MySQL 5.0 installation

Since Apache and MySQL servers must be installed prior to the PHP installation, I recommend installing the triad in this order: MySQL, Apache, PHP. You may well have some MySQL server already installed – in that case you can skip directly to the Apache 2 installation. However, it’s a good idea to reinstall everything, in order to have the most recent versions of the software.

There are several options for how to install MySQL:

  • using YaST – the easiest and fastest way. However, the version of MySQL bundled with SuSE installation is usually NOT the best (i.e. the most recent) available,
  • RPM installation – supposedly also fast and simple, I’ve never tried though. The only drawback here is that MySQL is not installed into a single destination – it’s scattered across several directories. I like to keep things tidy, so I skipped this option,
  • installing binaries – downloading precompiled files from the mysql.com website, copying them into a directory of your choice, and doing some simple configuration. I tried this, but it didn’t work for me – for some reason the MySQL server wouldn’t start,
  • installing from source – I would recommend this. Yes, it takes some time and effort, but you will get the most recent MySQL installed in a single location on your system, and everything will be configured according to your needs.

The rest of this chapter deals with the 4th option – the installation of MySQL from the source.

Prerequisites

Make sure you have superuser (root) privileges and user “mysql” already exists in your system. If not, create one:

groupadd mysql
useradd -g mysql mysql

This will be the default user under which the MySQL server will be running.

Download the source

First, download the MySQL source . You need the mysql-5.1.52-linux-i686-glibc23.tar.gz tarball file (or mysql-5.1.52-linux-x86_64-glibc23.tar.gz for 64-bit systems).

Unpack, configure, compile

So you have downloaded the mysql-5.1.52-linux-i686-glibc23.tar.gz file. You know the drill: unpack, configure, make, make install:.

tar -xzf mysql-5.1.52-linux-i686-glibc23.tar.gz
cd mysql-5.1.52-linux-i686-glibc23/
./configure  --prefix=/usr/local/mysql-5.1.52 --with-charset=utf8  --with-collation=utf8_general_ci
make
make install

We used the –with-charset and –with-collation options to set the default character set and collation – otherwise it would have been the default Swedish collation.

I recommend creating a symbolic link called “mysql” pointing to the MySQL installation directory, in order to make referring to it from elsewhere easier:

ln -s /usr/local/mysql-5.1.52/ /usr/local/mysql

This way we can always refer to MySQL installation directory as /usr/local/mysql . The obvious advantage is that if you install PHP with the –with-mysql=/usr/local/mysql option (see PHP 5 Installation Guide), it won’t stop working if the name of the MySQL installation directory changes in the future (if you upgrade your MySQL for instance).

Create my.cnf file

To complete MySQL server installation, you have to create a configuration file. It offers several security and control options (here you can limit system resources to be used by MySQL server, set the default collation and character set etc.). You need not to create a brand new configuration file – there are 4 pre-made files in the support-files/ directory. Read the information in those files to determine which one to use. For small servers (e.g. testing servers, or servers of a limited performance), my-small.cnf file is the best option. Copy the file of your choice to /etc/my.cnf:

cp support-files/my-small.cnf /etc/my.cnf
chown root /etc/my.cnf
chgrp root /etc/my.cnf
chmod 644 /etc/my.cnf

We have made sure both the owner and user group of the my.cnf file are “root” and the access privileges are properly set. Finally edit the file:

vi /etc/my.cnf

Search for [mysqld] clause, and add immediately below it:

user = mysql

We have specified that MySQL service is to be run with user “mysql” privileges.

If you want to use InnoDB databases (what you probably will), uncomment (and perhaps edit) all innodb options in the my.cnf file. Save all changes (<ESC> :wq).

Additional settings

For proper functioning, MySQL needs a “mysql” database. To create this database, simply run:

/usr/local/mysql/bin/mysql_install_db --user=mysql

The script will create /usr/local/mysql/var/ directory containing the necessary databases. This directory serves as a default storage for all databases you will create. Make sure it is writable by “mysql” system user!

Start server, check it, connect

Now you are ready to start your MySQL server for the first time.

/usr/local/mysql/bin/mysqld_safe --user=mysql &

Hit enter again to get your prompt back. The MySQL server should now be running. To check that server is running and works properly enter

/usr/local/mysql/bin/mysqladmin version

You should get some response about the server software version.

Connect to MySQL server:

/usr/local/mysql/bin/mysql -u root

If you get a welcome message and the prompt changes to mysql>, the server works and everything is fine. If this failed for any reason, it may indicate some problems with your installation/configuration.

Set the root password

Now, before you do anything else, set root user’s password (!). Stay connected to MySQL and enter:

DELETE FROM mysql.user WHERE User = '';
FLUSH PRIVILEGES;
SELECT  Host, User FROM mysql.user;

Look for the record that has root in the User column and something other than localhost in the Host column. This is the host_name.

SET  PASSWORD FOR 'root'@'localhost' = PASSWORD('new_password');
SET  PASSWORD FOR 'root'@'host_name' =  PASSWORD('new_password');

Remember, this is the MySQL superuser for all databases. Therefore you should use a strong password and keep it safe. Later, when you will be writing PHP scripts, do NOT use superuser for accessing databases! The “root” user is meant only for administration purposes. After you are finished, exit MySQL:

quit

Restart MySQL server

After everything is set up, restart MySQL server:

/usr/local/mysql/bin/mysqladmin -u root -p shutdown
/usr/local/mysql/bin/mysqld_safe --user=mysql &

Voila, your MySQL server is up and running!

Automatic startup

Set up an automatic startup so you don’t need to start MySQL server manually after each system reboot. Go back to the directory where you extracted the downloaded mysql tarball file. Enter

cp support-files/mysql.server /etc/init.d/mysql
chmod 755 /etc/init.d/mysql
chkconfig --add mysql
chkconfig --level 35 mysql on

Further reading

MySQL Reference Manual

Related posts

Tags: ,

  • http://www.pcforum24.de/linux/ Suse & Debian User

    What is the best for installation yast, factory rpm, binaries or source?

    I do normal the installation from yast but many friends say the binaries ar much better.

    Is this right?

  • http://laffers.net Richard Laffers

    That depends. Typically, YaST is recommended. If you need the bleeding edge version, go with the latest source. I know of no advantage of installing binaries directly instead of using YaST.

  • http://forums.opensuse.org/english/get-help-here/applications/407054-mysql-wont-start-socket-problem-maybe-3.html#post2263997 MySql won’t start. Socket problem maybe?

    [...] just follow the steps starting from Create my.cnf file if you already installed the database How to install Apache, PHP and MySQL on Linux: Part 1 | laffers.net Seems like there are some missing steps that this website covered, but certainly no need to [...]

  • mohamed

    Thanks so much! This works fine for me.

    I started from the Create my.cnf file section since I already installed the server, and used all the steps until the last one where I used “chkconfig mysql on ”

    Regards

  • http://donthave Raphael Nodji

    Hi,
    I have downloaded mysql-5.1.52-linux-i686-glibc23.tar.gz and mysql-5.1.52-linux-x86_64-glibc23.tar.gz, unpack these but cannot configure because the file “configure” does not exist.
    I have copied configure from an other packages but the c-compiler is not compatible. Can you help and give me a download link with the correct binaries.

    Thanks Raphael

  • Tom Dings

    Raphael .. I am experiencing the same challenge by not being able to use the ‘./configure’ option with this package. So probably Richard is right, better to use the Yast feature from SuSe instead of installing it by yourself.

    I didnt find a working solution too.

    Tom Dings

  • Tom Dings

    ‘mysql-5.1.54-linux-i686-glibc23′

    This is a binary already. Just read the INSTALL-BINARY for further instruction .. Look pretty simple to get it deployed. Hopefully, Raphael, you know how it works .. Have a nice day!

  • Sam

    Error!… When I followed the above instructions (using mysql-5.1.55.tar.gz) , when I try the line:
    /usr/local/mysql/bin/mysql_install_db –user=mysql

    I get the error:
    ———————————————-
    Installing MySQL system tables…
    110302 20:00:38 [Warning] ‘–skip-locking’ is deprecated and will be removed in a future release. Please use ‘–skip-external-locking’ instead.
    110302 20:00:38 [ERROR] /usr/local/mysql-5.1.55/libexec/mysqld: unknown variable ‘innodb_data_home_dir=/usr/local/mysql-5.1.55/var’
    110302 20:00:38 [ERROR] Aborting

    110302 20:00:38 [Note] /usr/local/mysql-5.1.55/libexec/mysqld: Shutdown complete

    Installation of system tables failed! Examine the logs in
    /usr/local/mysql-5.1.55/var for more information.
    ———————————————-

    Can anyone shed some light on this problem?
    many thanks
    Sam

  • Jw

    great! best tut I seen so far

  • CreatingServers1

    what is cp or chkconfig or vi?Dont understand lols

  • Mohitkumar 3571

    cp is copy command
    chkconfig is used to start the service permanently
    vi is an editor

  • http://forums.opensuse.org/english/get-technical-help-here/applications/460171-mysql-will-not-start.html#post2342460 mysql will not start

    [...] owner should not be a member of group mysql: grep mysql /etc/group mysql:!:105: See also here: How to install Apache, PHP and MySQL on Linux: Part 1 | laffers.net Reply With Quote + Reply to Thread « Previous Thread | Next [...]

  • Darrell McAdams

    I can not find mysql-5.1.52-linux-x86_64-glibc23.tar.gz anywhere.  Can you advise.

  • http://adminramble.com/install-mysql-yast/ How to install MySQL through YaST

    [...] to install MySQL besides YaST, if you would like to install MySQL from source you can check this blog Share this:FacebookPrintShareRedditDiggEmailStumbleUponNo related posts. This entry was [...]

  • http://improvehearingnaturally.com/blog/natural-ways-to-improve-your-hearing.php hearing loss treatments

    hearing loss treatments…

    How to install Apache, PHP and MySQL on Linux: Part 1 | laffers.net…