Wednesday, August 15, 2018

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. Ngoài LAMP còn có LEMP (viết tắt của Linux, Nginx, MySQL và PHP hoặc Perl hay Python cũng được). Nó tương tự như LAMP server ngoại trừ việc web server nền tảng được giám sát bằng Nginx thay vì Apache. Hôm nay, mình sẽ hướng dẫn các bạn cài đặt LAMP trên VPS chạy CentOS 6.
Related image

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 kiểm tr, đổi lại hostname và chỉnh sửa file host vì một số phần mềm yêu cầu server phải được thiết lập chính xác thông tin FQDN (Fully Qualified Domain Name) cho hostname. Đây cũng là vấn đề thường xuyên gặp phải trong khi làm việc với CentOS.

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”
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.
Chúc các bạn thành công !



0 comments: