About The Author

Sharing Tutorials on Using Computers, Basic, Simple and Easy Tips on Using Internet, Tricks and Techniques That I've Learnt Thus Far.

Email subscription

Pages

9.14.2010

Cài Apache2 với PHP5 và MySQL hỗ trợ trên Mandriva 2010.1 Spring (LAMP)

Trong bài hướng dẫn sau, Quản Trị Mạng sẽ  giới thiệu với các bạn cách cài đặt webserver Apache2 trên hệ thống  server Mandriva 2010.1 Spring với PHP5 (module mod_php) và MySQL hỗ trợ. Thuật ngữ LAMP ở đây là dạng viết tắt của tổ hợp Linux, Apache, MySQL, PHP.





Lưu ý sơ bộ




Trong hệ thống thử nghiệm này, chúng ta sử dụng hostname server1.example.com với địa chỉ IP: 192.168.0.100. Hãy thay thế các thông số này cho tương ứng và phù hợp với hệ thống của bạn.



Cài đặt MySQL 5



Trước tiên, chúng ta cần cập nhật cơ sở dữ liệu của các gói cài đặt:


urpmi.update -a

Cài đặt MySQL bằng lệnh sau:


urpmi MySQL MySQL-client

Ở chế độ mặc định, tính năng hỗ trợ mạng không được kích hoạt trong  gói Mandriva 2010.1 Spring MySQL. Chúng ta hoàn toàn có thể thay đổi  được điều này bằng cách chú thích tại dòng lệnh skip-networking trong file /etc/my.cnf:


vi /etc/my.cnf



[...]

# Don't listen on a TCP/IP port at all. This can be a security enhancement,

# if all processes that need to connect to mysqld run on the same host.

# All interaction with mysqld must be made via Unix sockets or named pipes.

# Note that using this option without enabling named pipes on Windows

# (via the "enable-named-pipe" option) will render mysqld useless!

#

#skip-networking

[...]


Sau đó, khởi động MySQL:


/etc/init.d/mysqld start

Kiểm tra lại xem tính năng hỗ trợ mạng đã được kích hoạt chưa bằng lệnh:


netstat -tap | grep mysql

Kết quả trả về của hệ thống sẽ trông giống như sau:


[root@server1 ~]# netstat -tap | grep mysql

tcp 0 0 *:mysql *:* LISTEN 2459/mysqld

[root@server1 ~]#


Tiếp theo, sử dụng lệnh:


mysql_secure_installation

để đặt mật khẩu cho tài khoản root (nếu không bất cứ ai cũng có thể truy cập vào cơ sở dữ liệu MySQL):


[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): <-- nhấn ENTER

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] <-- nhấn ENTER

New password: <-- mật khẩu SQL root

Re-enter new password: <-- mật khẩu SQL root

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] <-- nhấ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] <-- nhấ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] <-- nhấ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] <-- nhấ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 ~]#


Cài đặt Apache2



Apache2 đã có sẵn trong gói cài đặt của Mandriva, do vậy hãy sử dụng lệnh sau:


urpmi apache-mpm-worker

Và khởi động Apache:


/etc/init.d/httpd start

Bây giờ, mở trình duyệt và gõ địa chỉ http://192.168.0.100, nếu thành công thì bạn sẽ thấy trình duyệt hiển thị như sau:


Thư mục tài liệu mặc định của Apache là /var/www/html trên Mandriva,  còn file cấu hình thì được lưu trữ tại /etc/httpd/conf/httpd.conf. Các  file thiết lập khác được lưu tại thư mục /etc/httpd/conf.d/

Cài đặt PHP5



Sử dụng lệnh sau để cài đặt PHP5 và các module hỗ trợ Apache PHP5:


urpmi apache-mod_php

Kiểm tra PHP5 và thu thập thông tin về quá trình cài đặt:



Thư mục root mặc định của các website là /var/www/html. Tại đây chúng ta  sẽ tạo 1 file PHP nhỏ (info.php) trong thư mục đó và gọi ra trong trình  duyệt. File này sẽ hiển thị đầy đủ thông tin chi tiết về quá trình cài  đặt PHP.


vi /var/www/html/info.php



<?php

phpinfo();

?>


Gõ địa chỉ sau vào trình duyệt: http://192.168.0.100/info.php:


Như kết quả hiển thị trong hình, bạn có thể thấy rằng PHP5 đã hoạt  động thông qua Apache 2.0 Handler – được chỉ ra tại dòng Server API. Khi  kéo xuống phía dưới danh sách, bạn sẽ thấy tất cả các module đã được  kích hoạt trong PHP5. Riêng MySQL thì không có nghĩa là MySQL không được  hỗ trợ bên trong PHP5.



Cài đặt MySQL hỗ trợ trong PHP5



Để làm việc này, chúng ta cần cài thêm gói php-mysql hỗ trợ. Bên cạnh  đó, chúng ta có thể tìm và cài thêm 1 số module hỗ trợ PHP5 trong danh  sách liệt kê sau:


urpmf php

Chọn những gói phù hợp với nhu cầu sử dụng và cài đặt chúng theo cách sau:


urpmi php-mysql php-mysqli php-bz2 php-calendar php-ctype  php-curl php-devel php-dio php-dom php-eaccelerator php-enchant  php-esmtp php-event php-exif php-fam php-ffmpeg php-fileinfo php-filepro  php-ftp php-gd php-gettext php-gmp php-iconv php-id3 php-imap  php-mailparse php-mbstring php-mcache php-mcrypt php-ming php-newt  php-odbc php-oggvorbis php-pcntl php-pcre php-pear-Net_IDNA php-posix  php-pspell php-readline php-recode php-session php-shmop php-simplexml  php-snmp php-soap php-sockets php-sqlite php-ssh2 php-suhosin  php-sysvmsg php-sysvsem php-sysvshm php-tclink php-tcpwrap php-tidy  php-xml php-xmlrpc php-zip php-ini

Sau đó khởi động lại Apache2:


/etc/init.d/httpd restart

và sử dụng trình duyệt để kiểm tra lại module MySQL đã hoạt động hay chưa:


Cài đặt phpMyAdmin



Với phpMyAdmin người sử dụng sẽ dễ dàng hơn trong việc quản lý cơ sở dữ liệu MySQL. Các bạn tiến hành cài đặt phpMyAdmin như sau:


urpmi phpmyadmin

Sau đó mở file cấu hình /etc/httpd/conf/webapps.d/phpmyadmin.conf…:


vi /etc/httpd/conf/webapps.d/phpmyadmin.conf

và chỉnh lại để giống như sau để cho phép các truy cập khác kết nối vào phpMyAdmin:


Alias /phpmyadmin /var/www/phpmyadmin



<Directory /var/www/phpmyadmin>

Order deny,allow

#Deny from all

#Allow from 127.0.0.1

Allow from all

ErrorDocument 403 "Access denied per /etc/httpd/conf/webapps.d/phpmyadmin.conf"



php_flag session.auto_start 0

</Directory>



<Directory /var/www/phpmyadmin/libraries>

Order deny,allow

Deny from all

</Directory>


Sau đó khởi động lại Apache:


/etc/init.d/httpd restart

và truy cập vào phpMyAdmin qua đường dẫn http://192.168.0.100/phpmyadmin/:


Trên đây là 1 số bước cơ bản để cài đặt Apache2 với PHP5 và MySQL hỗ  trợ trong nền tảng Mandriva 2010.1 Spring. Chúc các bạn thành công!

T.Anh (theo HowToForge)