How to install LAMP on CentOS 6 server

You can check yum by running

 

yum update

Now if everything went well with yum, lets setup the hostname of the server – in case you didn’t already.
First check the current short hostname with

hostname

command, then you can try the same command with

-f

switch to show the FQDN (fully qualified domain name) version of the hostname.
In case you have to setup the hostname you can perform the following steps:
Edit /etc/sysconfig/network and add this

HOSTNAME=yourhostname.tld

Or run the following command:

echo "HOSTNAME=yourhostname.tld"

Now run

 hostname "yourhostname.tld"

and you are done setting your hostname.
Re check with

hostname

and

hostname -f

Now lets install the Apache web server. This is the most popular choice today among web browsers and usually it is the best solution combining easy configuration, wide compatibility, stability, maturity of the platform and performance.
Do not forget to run

yum update

unless you listened and already did that at the first stages fot this tutorial.
After the update run:

yum install httpd

Now make backup for the original Apache configuration file, usually this backup is not needed but in case you will need to restore the default configuration you will be glad you have it.
Backup the configuration by running:

mkdir /yournewconfbackupdir
cp /etc/httpd/conf/httpd.conf /yournewconfbackupdir/httpd.conf.original

It is a very recommended practice to backup configuration files before you edit and save the changes. Takes only one simple command to do it just as it will take another simple command to restore the configuration backup in case of multiple mistakes in configuration file on a production server leading to long downtime until you will find the mistakes in the configuration. It is much simpler to restore the file from backup and shortening the downtime to mere seconds instead of minutes or tens of minutes.

Now we will use this directory /etc/httpd/conf.d/ – Apache threats any files with .conf extensions in this path as its configuration files and it will load any such file on start or reload.
I’m going to to show you how to create Name based Virtual Hosts in /etc/httpd/conf.d . There are different approaches to this but personally I’ve always preferred to create Virtual Hosts as different configuration files in the conf.d directory, you can of course make one configuration file and include all your virtual hosts in this file. In this example I will show how to divide the configuration to separate files.
So for example you would like to create a virtual host for domain mydomain.com, what you are going to do is to create a new file in /etc/httpd/conf.d/:

 touch /etc/httpd/conf.d/mydomain.com.conf

Now you are going to edit this file and add the configuration itself:

     ServerAdmin admin@mydomain.com
     ServerName mydomain.com
     ServerAlias www.mydomain.com
     DocumentRoot /http/www/mydomain.com/httpdocs/
     ErrorLog /http/www/mydomain.com/logs/mydomain.com.error.log
     CustomLog /http/www/mydomain.com/logs/mydomain.com.access.log combined

Now you have to create the directories we specified in the configuration:

mkdir -p /http/www/mydomain.com/httpdocs
mkdir /httpd/www/mydomain.com/logs

Before you can start apache and test the configuration you have to edit

/etc/httpd/conf/httpd.conf

the same file you backed up at the beginning of this tutorial. Find the part with name-based Vritual Host and uncomment NameVirtualHost directive:

# Use name-based virtual hosting.
#
NameVirtualHost *:80
#
# NOTE: NameVirtualHost cannot be used without a port specifier
# (e.g. :80) if mod_ssl is being used, due to the nature of the
# SSL protocol.

Now you can try to start Apache for the first time:

service httpd start

or

/etc/init.d/httpd start

If the service is already running just use restart instead of start.
After you tested everything and you are sure about the configuration you can add the Apache (httpd) service to the system start up so that next time you will reboot the server you won’t have to worry about Apache service.
Run this command to make sure that the Apache service will auto start next time on server reboot:

chkconfig --levels 235 httpd on

or

/sbin/chkconfig --levels 235 httpd on

Do not forget to reload Apache service on each configuration edit, both httpd.conf or vhost configuration files:

service httpd reload

or

/etc/init.d/httpd reload

Now that you are finished with Apache, its time to continue with the process and install MySQL. MySQL is a database server and today most web applications, cms or blog system use mysql databases. So probably you will need MySQL anyway.

Install mysql-server packages from the repository:

yum install mysql-server

Now add the MySQL server to the startup so you won’t have to worry about it either when you reboot the server:

chkconfig --levels 235 mysqld on

or

/sbin/chkconfig --levels 235 mysqld on

and its time to test the installation and start the mysql server:

service mysqld start

or

/etc/init.d/mysqld start

After insuring mysql is running and everything is ok you should run the following command to make sure you secure your mysql installation, remove default databases and users and disable root logins except from localhost:

mysql_secure_installation

You should answer yes to all of the questions that will follow and do not forget to set the mysql root password.
Now login to mysql server:

mysql -u root -p

Enter your mysql password and you will get mysql prompt where you can run sql statements.
For example you can create a database and give permissions to user by running the following commands:

create database mydatabase;
grant all on mydatabase.* to 'myuser' identified by 'mypassword';

You can quit the mysql prompt by running the following command:

quit

In case you have to change mysql configuration, your configuration file is my.cnf and you can find it in /etc.
You are almost done and what’s left is to install PHP on the server, you probably already know what PHP is and why should you need PHP on your server, in case you don’t – PHP is a general-purpose scripting language for web development, its main purpose is to create dynamic pages, websites and web applications.
Install PHP by running the following command:

yum install php php-pear php-mysql

Now you can change defaults in /etc/php.ini if you already know what you are doing and there are specific settings you need to change. If you are not sure about this one, do not worry – you can always do it later if you encounter problems with web applications that will demand different PHP settings.
You can reload everything again or just reboot the server and make sure everything works and you are done. Congratulations! You have a working LAMP server setup.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s