LAMP on centOS7

#เตรียมเครื่องให้ นศ. แข่งทักษะ

สิ่งที่ต้องติดตั้ง

  • Apache
  • mariaDb
  • phpmyadmin
  • php 7.2
    -pdo mysql, odbc, sqlite

Set hostname :
server1.example.com

เพิ่ม EPEL repo(รวม package ของ fedora) , nano (คำสั่ง edit ไฟล์) , mlocate (ไว้ค้นหาไฟล์ในระบบ)

rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY*
yum -y install epel-release

yum -y install nano mlocate wget
updatedb

ปิด SELinux ก่อน

sudo setenforce 0

เข้าไปที่ไฟล์ config แล้วแก้เป็น disabled
nano /etc/selinux/config

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing — SELinux security policy is enforced.
# permissive — SELinux prints warnings instead of enforcing.
# disabled — No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of these two values:
# targeted — Targeted processes are protected,
# mls — Multi Level Security protection.
SELINUXTYPE=targeted

shotdown เครื่อง แล้วลองเช็คว่า SELinux ยังทำงานอยู่ไหมด้วยคำสั่ง sestatus

sudo shutdown -r now

sestatus

ถ้าแสดงสถานะ แบบนี้แปลว่าปิดแล้ว

SELinux status: disabled

ติดตั้ง MariaDB:

yum -y install mariadb-server mariadb
systemctl start mariadb.service
systemctl enable mariadb.service

เซต password ให้ mysql

mysql_secure_installation

Enter current password for root (enter for none): <--ENTER
Set root password? [Y/n]
New password:
<--yourmariadbpassword
Re-enter new password:
<--yourmariadbpassword
Password updated successfully!Remove anonymous users? [Y/n]
<--ENTER
... Success!Disallow root login remotely? [Y/n]
<--ENTER
... Success!
Reload privilege tables now? [Y/n]
<--ENTER
... Success!

ติดตั้ง apache

yum -y install httpd httpd-devel

เปิดใช้งาน service

systemctl start httpd.service
systemctl enable httpd.service

ตั้งต่า firewall กรณีไม่ได้ปิด IPtable หรือ firewall ในตอนแรก

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

ถ้าต้องการเปิดพอร์ต 3306 ที่เอาไว้ connect mysql เพื่อให้เครื่องข้างนอกเข้ามา connect ได้ ก็ให้ใส่ไปใน firewall ด้วย

firewall-cmd — permanent — zone=public — add-service=mysql
firewall-cmd — reload

ติดตั้ง PHP

rpm -Uvh http://rpms.remirepo.net/enterprise/remi-release-7.rpm
yum -y install yum-utils
yum update

เลือก ติดตั้ง PHP 7. 2
yum-config-manager — enable remi-php72
yum -y install php php-opcache

จากนั้น restart service

systemctl restart httpd.service

ทดสอบ การรัน php โดยไปสร้างไฟล์ info.php แล้วไปวางไว้ที่ /var/www/html

nano /var/www/html/info.php

<?php
phpinfo(); //info.php

แล้วทดสอบรัน หากไม่ทราบ ip เครื่องที่ติดตั้ง ให้ทดลองใช้คำสั่ง ifconfig เช่น 192.168.1.100/info.php

ส่วนต่อไปนี้เป็นส่วนสำคัญ

yum search php
yum -y install php-mysqlnd php-pdo
yum -y install php-gd php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-soap curl curl-devel
php-devel pcre-devel gcc

restart service :

systemctl restart httpd.service

ทดสอบรัน โดยการรันไฟล์ info.php เช่น 192.168.1.100/info.php

ติดตั้ง phpMyAdmin

yum -y install phpMyAdmin

หลังจากติดตั้งเสร็จ ให้แก้ไขไฟล์ phpMyAdmin.conf

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

[…]
Alias /phpMyAdmin /usr/share/phpMyAdmin
Alias /phpmyadmin /usr/share/phpMyAdmin

<Directory /usr/share/phpMyAdmin/>
AddDefaultCharset UTF-8

<IfModule mod_authz_core.c>
# Apache 2.4
# <RequireAny>
# Require ip 127.0.0.1
# Require ip ::1
# </RequireAny>
Require all granted
</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>

[…]

การมา Remark โดยใส่เครื่องหมาย # ในบรรทัดตัวอย่าง แล้วเพิ่ม คำสั่ง

Require all granted

จากนั้นแก้ไขไฟล้ config.inc.php เพื่อให้ phpmyadmin มีการ login ก่อนเข้าใช้งาน เติม ‘http’ ตามตัวอย่าง แล้ว restart service แล้วทดสอบใช้งานดู

nano /etc/phpMyAdmin/config.inc.php

[…]
$cfg[‘Servers’][$i][‘auth_type’] = ‘http’; // Authentication method (config, http or cookie based)?
[…]

รีสตาร์ท service
systemctl restart httpd.service

ทดสอบ เช่น 192.168.1.100/phpmyadmin

ติดตั้ง FTP Server

sudo yum install vsftpd

เป็นใช้งาน service vsftpd

sudo systemctl start vsftpd
sudo systemctl enable vsftpd

ตั้งค่า firewall เป็น port 21

sudo firewall-cmd --zone=public --permanent --add-port=21/tcp
sudo firewall-cmd --zone=public --permanent --add-service=ftp
sudo firewall-cmd –reload

ตั้งค่า VSFTPD โดยสำรองไฟล์ vsftpd.conf ไว้ก่อนครับ
sudo cp /etc/vsftpd/vsftpd.conf /etc/vsftpd/vsftpd.conf.default

เปิดไฟล์เพื่อ แก้คำสั่ง
sudo nano /etc/vsftpd/vsftpd.conf

anonymous_enable=NO

local_enable=YES

write_enable=YES

chroot_local_user=YES

allow_writeable_chroot=YES

userlist_enable=YES

userlist_file=/etc/vsftpd/user_list

userlist_deny=NO

รีสตาร์ท service

sudo systemctl restart vsftpd

สร้าง user FTP สมมุติว่า สร้าง 2 user

sudo adduser team1
sudo passwd team1

sudo adduser team2
sudo passwd team2

เติม user ลงไปใน user_list

echo “team1” | sudo tee –a /etc/vsftpd/user_list
echo “team2” | sudo tee –a /etc/vsftpd/user_list

สร้างโพเดอร์ ไว้เก็บเว็บของแต่ละ user

sudo mkdir –p /var/www/html/team1
sudo chmod -R /var/www/html/team1
sudo chown –R team1: /var/www/html/team1

sudo mkdir –p /var/www/html/team2
sudo chmod -R /var/www/html/team2
sudo chown –R team1: /var/www/html/team2

เสร็จแล้ว ลอง connect ด้วย โปรแกรม filezilla ดูครับ

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade