Home > English > Tutorials > How to install LAMP and creat VirtualHost(APACHE-PHP-MYSQL) on centos 6

How to install LAMP and creat VirtualHost(APACHE-PHP-MYSQL) on centos 6


I. Preparation

Install epel and remi repositories. Get reference link Click Here

Turn off selinux, edit the file selinux

# nano /etc/sysconfig/selinux 

Edit SELINUX=disabled

# reboot to reboot the server




Step 1 : Install httpd

# yum install httpd -y

start the service httpd 

# service httpd start

Let apache start together with the system 

# chkconfig httpd on

Open the gateways 80 and 3306 iptables 

# nano /etc/sysconfig/iptables

And add 2 lines

-A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 3306 -j ACCEPT

Restart iptables

# service iptables restart

You may see this error but it is ok

[[email protected]: yum.repos.d]# service httpd start
Starting httpd: httpd: Could not reliably determine the server's fully qualified                                                                                         domain name, using for ServerName
                                                           [  OK  ]

Correct it by editing the file httpd.conf

# nano /etc/httpd/conf/httpd.conf

Search for this line

#ServerName www.example.com:80

Replace the sign #  with:

ServerName *:80

Restart the httpd service

# service httpd restart


To check whether httpd is active, visit your IP. If the screen is like thhis you are successful


Step 2 : install php

Use yum command to install php

# yum install -y php 

The defaulted document root of the website are /var/www/html. Now you creat a small file PHP <info.php> in that folder and call it in one browser. These files will display many useful details about our PHP installation such as some PHP versions installed before

# nano /var/www/html/info.php

In the file info.php there is a content


Ta reset httpd

# service httpd restart

Visit your IP address http://<ip server>/info.php if the screen is like this, you are successful 

Note: you should remove the file info.php after checking to prevent visits from other people 

# rm -rf /var/www/html/info.php

To prepare for MySQL installation, you need to install a few PHP packages such as 

# yum -y install php-mysql

# yum -y install php-gd php-imap php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-mcrypt php-mssql php-snmp php-soap php-tidy curl curl-devel

# yum -y install php-pecl-apc

Restart apache 

# service httpd restart

Step 3 : Install  MySQL

Use yum command to install 

# yum -y install mysql mysql-server

Let mysql start together with the system

# chkconfig mysqld on 

start mysql service

# service mysqld start

set a password for MySQL root account:

# mysql_secure_installation

There will be these sentences

sentence 1 : Enter the current password for root (enter for none)

sentence 2 : Set root password? [Y/n] select Y and enter the password

sentence 3 : Remove anonymous users? [Y/n] select Y

sentence 4 : Disallow root login remotely? [Y/n] select Y 

sentence 5 : Remove test database and access to it? [Y/n] select Y

sentence 6 : Reload privilege tables now? [Y/n] select Y

To log in mysql, use the command 

# mysql -u root -p

Step 4: Creat VirtualHost

Creat a seperated folder public_html to store the web's files, you should distinguish files by domain names  

# mkdir -p /home/webdata/maxserver.net/public_html

Add use apache and group apache to own this folder
# chown -R apache:apache /home/webdata/maxserver.net

Creat a file to record the web errors which is error.log

# mkdir /home/webdata/maxserver.net/log

# touch /home/webdata/maxserver.net/log/error.log

Creat a file to test whether virtualhost is active, the content is as follows
# nano /home/webdata/maxserver.net/public_html/index.html

Content will be like this

Chuc mot ngay tot lanh!</h1>


Edit the configuration file of apache

# nano /etc/httpd/conf/httpd.conf

Search for this line

#NameVirtualHost *:80 (line 990)

Remove the sign  # at the beginning to allow all domains to visit through the port 80

NameVirtualHost *:80

Creat a file with tail .conf  in the folder conf.d. such as domain.conf 
# nano /etc/httpd/conf.d/maxserver.conf

The content will be this

<VirtualHost *:80>
ServerAdmin [email protected]
DocumentRoot /home/webdata/maxserver.net/public_html
ServerName www.maxserver.net
ServerAlias maxserver.net
ErrorLog /home/webdata/maxserver.net/log/error.log

Restart httpd service
# service httpd restart

In the last step you point the domain to IP address, try visting the domain. If the screen displays your created content, you are successful  

III. Some MySQL commands

On CentOS, all database files are saved in the folder  /var/lib/mysql


1.Database actions:

Show all databases:

Creat a database:
mysql> CREATE DATABASE <database name>;

Use a database:
mysql> USE <database name>;

Remove a database:
mysql> DROP DATABASE <databse name>;

2. Account management and permission distribution

Display all users:
mysql> SELECT * FROM mysql.user;

Delete null user:
mysql> DELETE FROM mysql.user WHERE user = ' ';

Delete all users that are not root:
mysql> DELETE FROM mysql.user WHERE NOT (host="localhost" AND user="root");

Rename root account(for security):
mysql> UPDATE mysql.user SET user="mydbadmin" WHERE user="root";

Assign full permissions to a new user:
mysql> GRANT ALL PRIVILEGES ON *.* TO 'username'@'localhost' IDENTIFIED BY 'mypass' WITH GRANT OPTION;

Ditribute permissions to a new use in details:

Assign full permissions to a new user on a specific database:
mysql> GRANT ALL PRIVILEGES ON mydatabase.* TO 'username'@'localhost' IDENTIFIED BY 'mypass' WITH GRANT OPTION;

Change user password:
mysql> UPDATE mysql.user SET password=PASSWORD("newpass") WHERE User='username';

mysql> DELETE FROM mysql.user WHERE user="username";


3. Backup actions and restore

backup all database by commanding(note that there is no space between -p and the password):
mysqldump -u root -pmypass --all-databases > alldatabases.sql

backup a randomdatabase :
mysqldump -u username -pmypass databasename > database.sql

Restore all database by commanding:
mysql -u username -pmypass < alldatabases.sql (no space in between -p and mypass)

restore a random database
mysql -u username -pmypass databasename < database.sql

Backup only database structure:
mysqldump --no-data --databases databasename > structurebackup.sql

Backup multiple database structures:
mysqldump --no-data --databases databasename1 databasename2 databasename3 > structurebackup.sql

Backup some specific tables
mysqldump --add-drop-table -u username -pmypass databasename table_1 table_2 > databasebackup.sql