Trong thời gian vừa qua do phải giải quyết một số công việc, nên không time để đóng góp bài viết cho diễn đàn, hôm nay mình xin hướng dẫn các bạn xây dựng một Webserver trên nền CentOS 6.4. Mình sưu tầm từ nhiều nguồn khác nhau, tổng hợp lại, chia sẽ cho các bạn, cũng như mình hệ thống lại cho dễ nhớ và trên thực tế mình cũng mới xây dựng xong Webserver.
Như các bạn đã biết LAMP là từ viết tắt của Linux, Apache, MySQL, PHP. Hướng dẫn này cho thấy làm thế nào bạn có thể cài đặt một máy chủ web Apache2 trên một máy chủ Centos 6.4 với PHP5 hỗ trợ (mod_php) và hỗ trợ MySQL.
Trong bài này tôi sử dụng it365days.lab tên máy với địa chỉ IP 192.168.112.130. Các thiết lập này có thể khác với bạn, vì vậy bạn phải thay thế chúng khi thích hợp.
Tôi đang chạy tất cả các bước trong hướng dẫn này với quyền root, do đó hãy chắc chắn rằng bạn đang đăng nhập như là quyền root:
1. Cài đặt mysql 5.0
#yum -y install mysql mysql-server
Cấu hình mysql khởi động cùng hdh
# chkconfig mysqld on
Restart lại mysqld
# /etc/init.d/mysqld start or service mysqld start
Reset password root cho mysql
# mysql_secure_installation
[root@server1 ~]# mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
In order to log into MySQL to secure it, we’ll need the current
password for the root user. If you’ve just installed MySQL, and
you haven’t set the root password yet, the password will be blank,
so you should just press enter here.
Enter current password for root (enter for none):
OK, successfully used password, moving on…
Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorisation.
Set root password? [Y/n] <– ENTER
New password: <– yourrootsqlpassword
Re-enter new password: <– yourrootsqlpassword
Password updated successfully!
Reloading privilege tables..
… Success!
By default, a MySQL installation has an anonymous user, allowing anyone
to log into MySQL without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.
Remove anonymous users? [Y/n] <– ENTER
… Success!
Normally, root should only be allowed to connect from ’localhost’. This
ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n] <– ENTER
… Success!
By default, MySQL comes with a database named ’test’ that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.
Remove test database and access to it? [Y/n] <– ENTER
– Dropping test database…
… Success!
– Removing privileges on test database…
… Success!
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
Reload privilege tables now? [Y/n] <– ENTER
… Success!
Cleaning up…
All done! If you’ve completed all of the above steps, your MySQL
installation should now be secure.
Thanks for using MySQL!
[root@server1 ~]#
2. Cài đặt apache
Dùng lệnh để tải gói và cài đặt.
# yum -y install httpd
Chỉnh file cấu hình
[root@www ~]# vi /etc/httpd/conf/httpd.conf
# line 44: change
ServerTokens Prod
# line 76: change to ON
KeepAlive On
# line 262: Admin’s address
ServerAdmin root@it365days.lab
# line 276: change to your server’s name
ServerName www.it365days.lab:80
# line 331: change (enable CGI and disable Indexes)
Options FollowSymLinks ExecCGI
# line 338: change
AllowOverride All
# line 402: add file name that it can access only with directory’s name
DirectoryIndex index.html index.cgi index.php
# line 536: change
ServerSignature Off
# line 759: make it comment
#AddDefaultCharset UTF-8
# line 796: uncomment and add file-type that apache looks them CGI
AddHandler cgi-script .cgi .pl
[root@www ~]# /etc/rc.d/init.d/httpd start
Starting httpd: [ OK ]
[root@www ~]# chkconfig httpd on
Cấu hình Apache khởi động cùng với Server.
#chkconfig httpd on
Sau đó khởi động Apache:
# service httpd start
Trên CentOs, thư mục lưu trữ web mặc định của Apache được lưu ở đường dẫn /var/www/html và tệp tin cấu hình được lưu tại /etc/httpd/conf/httpd.conf. Các file cấu hình khác được lưu tại thư mục /etc/httpd/conf.d/
3. Cài đặt php
# yum -y install php
Chúng ta restart apache
# /etc/init.d/httpd restart
4. Test php 5
# vi /var/www/html/info.php
<?php
phpinfo();
?>
Truy cập vào http://192.168.112.130/info.php , nếu hiển thị như hình bên dưới là OK

Như bạn thấy, PHP5 đang làm việc, và nó làm việc thông qua Handler Apache 2.0, như thể hiện trong dòngServer API. Nếu bạn di chuyển tiếp tục xuống, bạn sẽ thấy tất cả các mô-đun đã được kích hoạt trong PHP5. MySQL không được liệt kê ở đó có nghĩa là chúng tôi không có hỗ trợ MySQL trong PHP5.
5. Geting Mysql hỗ trợ php
Để có được hỗ trợ MySQL trong PHP, chúng ta có thể cài đặt các gói php5-mysql. Đó là một ý tưởng tốt để càiđặt một số PHP5 các module khác cũng như bạn có thể cần cho các ứng dụng của bạn. Bạn có thể tìm kiếmcho PHP5 mô-đun như thế này:
# yum search php
# yum -y install php-mysql php-gd php-imap php-ldap php-odbc php-pear php-xml php-xmlrpc
– Restart lại apache
# /etc/init.d/httpd restart
– Kiểm tra mysql đã hỗ trợ php chưa bằng cach http://192.168.112.130/info.php nếu hiển thị như hình dưới thì có nghĩa là ok.

6. Install phpMyAdmin phpMyAdmin 4.0.2 CentOS 6.4
– Bạn chọn phiên bản phù hợp với bạn 32bit hoặc 64bit.
Step 1: Install Required Repositories for PhpMyAdmin 4.0.2
CentOS 6.4 – 32 Bit OS
## Epel Dependency on CentOS 6 ##
# rpm -Uvh http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm
## Remi Dependency on CentOS 6 ##
# rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm
CentOS 6.4 – 64 Bit OS
## Epel Dependency on CentOS 6 ##
# rpm -Uvh http://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
## Remi Dependency on CentOS 6 ##
# rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm
Step 2: Install PhpMyAdmin 4.0.1
Once you’ve installed above repositories, you can run the following command to install it.
# yum –enablerepo=remi install phpmyadmin
Khởi động lại httpd
/etc/init.d/httpd restart
Các bạn sẽ được PHP 5.4

Note : If your are using PHP 5.4 on CentOS systems, then you need to run the below command to install it.
# yum –enablerepo=remi,remi-test install phpmyadmin
Step 3: Configuring PhpMyAdmin 4.0.2 for Apache
– Sữa lại file config
– vi /etc/httpd/conf.d/phpMyAdmin.conf
[root@server01 ~]# vi /etc/httpd/conf.d/phpMyAdmin.conf
1 # phpMyAdmin – Web based MySQL browser written in php
2 #
3 # Allows only localhost by default
4 #
5 # But allowing phpMyAdmin to anyone other than localhost should be considered
6 # dangerous unless properly secured by SSL
7
8 Alias /phpMyAdmin /usr/share/phpMyAdmin
9 Alias /phpmyadmin /usr/share/phpMyAdmin
10
11 <Directory /usr/share/phpMyAdmin/>
12 <IfModule mod_authz_core.c>
13 # Apache 2.4
14 Require local
15 </IfModule>
16 <IfModule !mod_authz_core.c>
17 # Apache 2.2
18 Order Deny,Allow
19 # Deny from All
20 Allow from 127.0.0.1
21 Allow from ::1
22 </IfModule>
23 </Directory>
24
25 <Directory /usr/share/phpMyAdmin/setup/>
Khởi động lại httpd
/etc/init.d/httpd restart
– Cấu hình authentication trên phpMyAdmin từ cookie tới http:
vi /etc/phpMyAdmin/config.inc.php
20 * Servers configuration
21 */
22 $i = 0;
23
24 /*
25 * First server
26 */
27 $i++;
28 /* Authentication type */
29 $cfg[‘Servers’][$i][‘auth_type’] = ‘http’;
30 /* Server parameters */
31 $cfg[‘Servers’][$i][‘host’] = ‘localhost’;
32 $cfg[‘Servers’][$i][‘connect_type’] = ‘tcp’;
33 $cfg[‘Servers’][$i][‘compress’] = false;
– Restart lại apache
# /etc/init.d/httpd restart
Khi cài đặt thành công các bạn sẽ được như hình dưới.

Chúc các bạn thành công.