Login or e-mail Password   

How to install apache, PHP and MySQL on Linux

Link to original publication: http://laffers.net/howtos/howto-install-mysql
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.
Views: 12.246 Created 03/13/2007

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.


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 MySQL source . You need the mysql-5.0.21.tar.gz tarball file.

unpack, configure, compile

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

# tar -xzf mysql-5.0.21.tar.gz
# cd mysql-5.0.21
# ./configure --prefix=/usr/local/mysql-5.0.21 --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.0.21/ /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 = '';
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:


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

apache 2 installation


Before you begin, it is highly recommended (though not inevitable) to create a system user and user group under which your Apache server will be running.

# groupadd www
# useradd -g www apache2

What is it good for? All actions performed by Apache (for instance your PHP scripts execution) will be restricted by this user's privileges. Thus you can explicitly rule which directories your PHP scripts may read or change. Also all files created by Apache (e.g. as a result of executing your PHP scripts) will be owned by this user (apache2 in my case), and affiliated with this user group (www in my case).

download source

Get the source from http://httpd.apache.org/download.cgi . At the time of writing this tutorial the best available version was 2.2.3 ( httpd-2.2.3.tar.gz ). These instructions are known to work with all 2.x.x Apache versions.

unpack, configure, compile

Go to the directory with the downloaded file and enter:

# tar -xzf httpd-2.2.3.tar.gz
# cd httpd-2.2.3
# ./configure --prefix=/usr/local/apache2 --enable-so

The configure options deserve a little bit more of detail here. The most important --prefix option specifies the location where Apache is to be installed. Another commonly used option --enable-so turns on the DSO support, i.e. available modules compiled as shared objects can be loaded or unloaded at runtime. Very handy.

To compile some modules statically (they are always loaded, faster execution times), use --enable-module option. To compile a module as a shared object, use --enable-module=shared option.

For all available configuration options and their default values check the Apache documentation or type ./configure --help.

SSL support

To support secure connections, you need to specify --enable-ssl option when you run ./configure. In addition to that, you will also have to configure your httpd.conf file later.

Note: Make sure that openssl is installed on your system before you run ./configure with --enable-ssl. If not, download the latest version from http://www.openssl.org/source/ , unpack, configure, make, make install. You will also need to generate server certificate. Place server.crt and server.key into /etc/ssl/apache2/ directory and make them readable by Apache2.

configuration example

For example, to compile the mod_rewrite module statically and mod_auth_digest as a DSO, and to enable secure connections, enter:

# ./configure --prefix=/usr/local/apache2 --enable-so --enable-rewrite --enable-auth-digest=shared --enable-ssl

Tip: If you are upgrading from older Apache version, you may want to copy config.nice from the directory to which the previous version was unpacked (if available) to where you unpacked the new Apache tarball file. Run ./config.nice instead of ./configure. This way all the previously used configure options will be applied to the new installation effortlessly.

Once you configured everything as you like, compile and install the software:

# make
# make install

edit httpd.conf

Before you start Apache server, edit the httpd.conf file according to your needs (the file is generously commented).

# vi /usr/local/apache2/conf/httpd.conf

I suggest the following changes (some of them may have already been set automatically) at the appropriate places inside httpd.conf (ignore "..."):

ServerRoot "/usr/local/apache2"
<IfModule !mpm_netware.c>
 User apache2
 Group www
DocumentRoot "/foo/path_to_your_www_documents_root"
<Directory />
 Options FollowSymLinks
 AllowOverride None
DirectoryIndex index.php index.html index.htm index.html.var

"apache2" and "www" are the user and user group I have previously created (see Prerequisites)

Apart from these, later you will probably want to specify detailed options for specific directories, load some DSO modules, setup virtual servers etc.

SSL support

If you wish to enable SSL for secure connections (assuming that you have configured Apache with --enable-ssl option - see above), add the following in the appropriate sections inside httpd.conf (ignore "..."; replace "laffers.net" with your own, and set the actual path to your server certificate and key file):

Listen 80
Listen 443
<VirtualHost *:443>
 ServerName laffers.net:443
 SSLEngine on
 SSLCertificateFile /etc/ssl/apache2/server.crt
 SSLCertificateKeyFile /etc/ssl/apache2/server.key
 ErrorLog /usr/local/apache2/logs/error_log_laffers.net
 TransferLog /usr/local/apache2/logs/access_log_laffers.net
 SetEnvIf User-Agent ".*MSIE.*" \
 nokeepalive ssl-unclean-shutdown \
 downgrade-1.0 force-response-1.0

Note: In some newer distributions, httpd.conf is dissected into many additional files located in conf/extra. In that case, you may want to do the SSL settings from above inside the conf/extra/httpd-ssl.conf file. Don't forget to uncomment "Include conf/extra/httpd-ssl.conf" in the httpd.conf file.

After you installed PHP (next part of this tutorial), few additional changes need to be done to httpd.conf (but they are usually made automatically during PHP installation).

setup access privileges

Don't forget to setup Apache access privileges to your www directories:

# chown -R apache2:www /foo/path_to_your_www_documents_root
# chmod -R 750 /foo/path_to_your_www_documents_root

"apache2" and "www" are the user and user group I have previously created (see Prerequisites)

start and stop apache server

After everything is set up, start Apache:

# /usr/local/apache2/bin/apachectl start

Similarly, if you wish to stop Apache, type:

# /usr/local/apache2/bin/apachectl stop

automatic startup

It's a good idea to let your Apache server start automatically after each system reboot. To setup Apache automatic startup, do:

# cp /usr/local/apache2/bin/apachectl /etc/init.d
# chmod 755 /etc/init.d/apachectl
# chkconfig --add apachectl
# chkconfig --level 35 apachectl on

further reading

PHP 5 installation

We will set up PHP as a shared module, being loaded into Apache2 dynamically during the server startup. These instructions are known to work for PHP versions: 5.0.4, 5.0.5, 5.1.0, 5.1.4, 5.1.5 .


At this point Apache web server must be installed. If you want MySQL support in PHP, MySQL server also must have been installed prior to the next steps.

download source

Get the source from http://www.php.net/downloads.php . At the time of writing this tutorial the best available version was 5.1.5 ( php-5.1.5.tar.gz ).

unpack, configure, compile

Go to the directory whith the downloaded file and enter:

tar -xzf php-5.1.5.tar.gz
cd php-5.1.5
./configure --prefix=/usr/local/php --with-apxs2=/usr/local/apache2/bin/apxs --with-mysql=/usr/local/mysql

The configuration options ought to be self-explaining; --prefix specifies the location where PHP is to be installed, --with-apxs2 with correct path pointing to bin/apxs in the Apache installation directory is mandatory for the installator to work. Since PHP 5, you need to explicitly bundle PHP with MySQL by --with-mysql directive (make sure you specified path to where MySQL is installed on your system).

There are many other options which turn on additional features. For all available configuration options and their default values type ./configure --help.

Tip: If you are performing an upgrade, you may want to copy config.nice from the old PHP installation directory (if available) to where you unpacked the new PHP tarball file. Run ./config.nice instead of ./configure. This way all the previous configure options will be applied to the new installation effortlessly.

Once you entered ./configure with all the options you need, compile and install the software:

make install

edit httpd.conf

All necessary changes to httpd.conf (Apache configuration file) should have already been made automatically during the installation, so usually you need not do anything. Anyways, check that following lines were added to your httpd.conf file:

LoadModule php5_module modules/libphp5.so
AddType application/x-httpd-php .php

If not, add them manually.

create php.ini file

Importanly, you have to create php.ini configuration file. Choose one of the pre-made files (preferably php.ini-recommended) residing inside the php-5.0.4/ directory (it's the folder to which the downloaded archive was extracted). Copy the file to the lib/ directory in the PHP installation directory.

cp php-5.0.4/php.ini-recommended /usr/local/php/lib/php.ini

If you need to, edit the php.ini file:

vi /usr/local/php/lib/php.ini

However, the default settings should work for everyone in most cases.

restart apache server

After everything is set up, restart Apache:

/usr/local/bin/apachectl stop
/usr/local/bin/apachectl start

Alternatively, simply enter:

/usr/local/bin/apachectl restart

further reading

PHP Manual

Reprint with permission of the author

Similar articles

comments: 3 | views: 27905
comments: 4 | views: 17771
comments: 6 | views: 12843
comments: 1 | views: 6828
comments: 2 | views: 5160

No messages

Add your opinion
You must be logged in to write a comment. If you're not a registered member, please register. It takes only few seconds, and you get an access to additional functions .

Users online: 118
Registered: 107.588
Comments: 1.501
Articles: 7.251
© 2005-2018 EIOBA group.