Do đã xuất hiện từ lâu nên LAMP (viết tắt của Linux, Apache, MySQL, và PHP) được dùng rất phổ biến trên thế giới.

Các bước cài đặt LAMP trên CentOS

Đầu tiên bạn cần chuẩn bị một server CentOS mới tinh chưa cài gì cả, ở đây mình dùng CentOS 6.4 64bit. Trước khi thao tác bạn cần phải đổi lại hostname và chỉnh sửa file host.

1. Cài đặt Apache

yum install httpd

Đường dẫn file cấu hình Apache:

/etc/httpd/conf/httpd.conf

Khởi động Apache

service httpd start

Test thử bằng cách truy cập vào http://<youraddress> bạn sẽ thấy thông báo “Apache 2 Test Page”

Lưu ý: nếu sử dụng VPS ở Vultr, mặc định port 80 sẽ bị block, khi truy cập vào IP sẽ bị báo lỗi ERR_CONNECTION_REFUSED. Bạn hãy thực hiện thêm thao tác mở port như sau:

/sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT
/etc/rc.d/init.d/iptables save

Cấu hình Apache Virtual Hosts

– Tạo một file cấu hình ở thư mục /etc/httpd/conf.d, đặt tên ví dụ example.com.conf (thay bằng domain của bạn)

Nội dung file /etc/httpd/conf.d/example.com.conf

NameVirtualHost *:80

<VirtualHost *:80> 
     ServerAdmin webmaster@example.com
     ServerName example.com
     ServerAlias www.example.com
     DocumentRoot /var/www/example.com/public_html/
     ErrorLog /var/www/example.com/logs/error.log 
     CustomLog /var/www/example.com/logs/access.log combined
</VirtualHost>

– Tạo thư mục cho website

mkdir -p /var/www/example.com/public_html
mkdir /var/www/example.com/logs

– Nếu muốn thêm website, bạn chỉ cần tạo thêm một file .conf khác và reload lại Apache với lệnh: service httpd reload

2. Cài đặt MySQL

yum install mysql-server
service mysqld start

Tiến hành cài đặt MySQL bằng lệnh

/usr/bin/mysql_secure_installation

Do mới cài đặt nên nếu bị hỏi password bạn nhấn Enter

Enter current password for root (enter for none): 
OK, successfully used password, moving on...

Sau đó cần đặt root password bằng cách chọn y

Tiếp theo bạn sẽ phải trả lời một loạt các câu hỏi, tốt nhất cứ chọn y

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] y                                            
 ... 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] y
... 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] y
 - 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] y
 ... 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!

3. Cài đặt PHP

yum install php php-mysql

PHP Modules

PHP có rất nhiều thư viện module khác nhau, bạn có thể xem bằng cách gõ lệnh sau

yum search php-
php-bcmath.x86_64 : A module for PHP applications for using the bcmath library
php-cli.x86_64 : Command-line interface for PHP
php-common.x86_64 : Common files for PHP
php-dba.x86_64 : A database abstraction layer module for PHP applications
php-devel.x86_64 : Files needed for building PHP extensions
php-embedded.x86_64 : PHP library for embedding in applications
php-enchant.x86_64 : Human Language and Character Encoding Support
php-gd.x86_64 : A module for PHP applications for using the gd graphics library
php-imap.x86_64 : A module for PHP applications that use IMAP

Để cài module nào dùng lệnh sau

yum install name of the module

4. Tự động chạy service khi reboot

chkconfig httpd on
chkconfig mysqld on

Restart Apache

service httpd restart

OK vậy là quá trình cài đặt LAMP trên CentOS vậy là xong.

Xem thêm

Comment của bạn

Lưu ý: tất cả comment đều được kiểm duyệt cẩn thận! Nếu có code bạn hãy up lên pastebin.com rồi để lại link trong comment.