Hướng dẫn cài đặt LAMP (Web Server , Apache, MySQL và PHP) trên CentOS 7


Trong bài viết này tôi sẽ hướng dẫn các bạn cách cài đặt một webserver sử dụng Apache2 trên CentOS 7 hỗ trợ PHP5 (mod_php) và MySQL . LAMP là từ viết tắt của Linux,Apache MySQL,PHP.

Tải Centos 7 tại đây: http://isoredirect.centos.org/centos/7/isos/x86_64/CentOS-7-x86_64-DVD-1804.iso

1. Preliminary Note


Trong bài này tôi sử sụng máy CentOS 7 với hostname là server.adminvietnam.org có địa chỉ IP là 172.16.1.254 . Các thiết lập này là khác nhau , nên bạn có thể chỉnh sửa cho phù hợp

Trước tiên tôi sẽ cài đặt EPEL-7

rpm -ivh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm

2. Installing MySQL 5


Để cài đặt MySQL , tôi sẽ cài đặt mariadb như sau

yum -y install mariadb-server mariadb

Sau đó tôi sẽ tạo một liên kết khởi động cho MySQL (cho phép MySQL khởi động cùng hệ thống ) và khởi động MySQL server

systemctl start mariadb.service
systemctl enable mariadb.service

Đặt password cho tài khoản root MySQL

mysql_secure_installation

[root@server /]# mysql_secure_installation
/usr/bin/mysql_secure_installation: line 379: find_mysql_client: command not found

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
 SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, 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):   <--ENTER
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] y
New password:                                    <--password mariadb của bạn
Re-enter new password: 
Password updated successfully!                    
Reloading privilege tables..
 ... Success!

By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB 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, MariaDB 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 MariaDB
installation should now be secure.

Thanks for using MariaDB!

3. Installing Apache2


Tiếp theo tôi cài đặt Apache2 , Apache2 thường sẽ có sẵn trên CentOS 7

yum -y install httpd

Bây giờ cấu hình cho Apache2 khởi động cùng hệ thống

systemctl start httpd.service
systemctl enable httpd.service

Trên CentOS 7 sử dụng Firewall-cmd nên tôi sẽ tùy chỉnh cho phép truy cập từ bên ngoài đến port 80 (HTTP) và port 443 (HTTPS)

firewall-cmd --permanent --zone=public --add-service=http
firewall-cmd --permanent --zone=public --add-service=https
firewall-cmd --reload

Tôi sẽ kiểm tra thử bằng cách truy cập tới địa chỉ http://yourip (ví dụ : http://172.16.1.254)



4. Installing PHP5


Chúng ta có thể cài dặt PHP5 và module Apache PHP5 như sau

yum -y install php

Sau khi cài đặt hoàn tất tôi khởi động lại Apache

systemctl restart httpd.service

5. Testing PHP5 / Getting Details About Your PHP5 Installation


Mặc định các file gốc của trang web sẽ nằm trong /var/www/html , bây giờ tôi sẽ tạo 1 file PHP nhỏ (info.php) , file này sẽ hiển thị một số thông tin rất hữu ích như số phiên bản PHP đã được cài đặt …v …v

nano /var/www/html/info.php

<?php

phpinfo();

?>

Tôi sẽ kiểm tra file trên bằng cách truy cập đến địa chỉ http://yourip/info.php (ví dụ : http://172.16.1.254/info.php)



6. Getting MySQL Support In PHP5


Để MySQL hỗ trợ trên PHP ta có thể cài gói php-mysql , ta cũng có thể cài một số module PHP5 khác cần thiết , có thể tìm các module PHP5 khác như sau

yum search php

Chọn 1 cái bạn cần và cài đặt nó

yum -y install php-mysql

Khởi động lại Apache2

systemctl restart httpd.service

7. phpMyAdmin installation


Với phpMyAdmin bạn có thể quản lý cơ sở dữ liệu MySQL thông qua giao diện web

Tôi có thể cài đặt phpMyAdmin như sau

yum install phpMyAdmin

Tiếp theo tôi sẽ cấu hình phpMyAdmin . Tôi sẽ chỉnh sửa Apahe để cho phép các kết nối đến phpMyAdmin không chỉ từ localhost

nano /etc/httpd/conf.d/phpMyAdmin.conf

Alias /phpMyAdmin /usr/share/phpMyAdmin
Alias /phpmyadmin /usr/share/phpMyAdmin

#<Directory /usr/share/phpMyAdmin/>
# <IfModule mod_authz_core.c>
# # Apache 2.4
# <RequireAny>
# Require ip 127.0.0.1
# Require ip ::1
# </RequireAny>
# </IfModule>
# <IfModule !mod_authz_core.c>
# # Apache 2.2
# Order Deny,Allow
# Deny from All
# Allow from 127.0.0.1
# Allow from ::1
# </IfModule>
#</Directory>


<Directory /usr/share/phpMyAdmin/>
 Options none
 AllowOverride Limit
 Require all granted
</Directory>

Tiếp theo tôi sẽ khởi động lại Apache2

systemctl restart  httpd.service

Sau khi khởi động lại bạn có thể kết nối đến phpMyAdmin bằng cách truy cập đến http://yourip/phpMyAdmin (ví dụ : http://172.16.1.254/phpMyAdmin)



Để đăng nhập các bạn dùng username là root và password đã tạo ở phần 2 .



8. Cài đặt WordPress


Bước 1: Download WordPress




Bạn đăng nhập vào phpMyAdmin để tạo một database với thông tin sau:


  • DATABASE: wordpress

  • USER: root

  • PASSWORD: 123456

Bước 5: Kết nối Source với MySQL

Mở file wp-config.php và cấu hình giống như phần bên dưới

/** The name of the database for WordPress */
define(‘DB_NAME’, ‘wordpress‘);

/** MySQL database username */
define(‘DB_USER’, ‘root‘);

/** MySQL database password */
define(‘DB_PASSWORD’, 123456‘);

Bước 6: Kiểm tra

Sau khi kết nối xong, thì bạn vào trình duyệt và nhập IP của bạn vào thì sẽ thấy phần cài đặt wordpress.





Theo