Tag Archive เซิร์ฟเวอร์

php: download ไฟล์จาก server

ตัวอย่าง code php ไว้ download ไฟล์จาก server

PHP/download.php
<?php
$fileDir = '../assets/';
$token = 'HH89VOiirgXlCdEqDrFs';

if ($_REQUEST['token'] != $token) {
    exit('bad token');
}

$file = $fileDir . $_REQUEST['file'];
if (file_exists($file)) {
    header('Content-Description: File Transfer');
    header('Content-Type: ' . mime_content_type($file));
    header('Content-Disposition: attachment; filename="' . basename($file) . '"');
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));

    //readfile($file);
    echo file_get_contents($file);
} else {
    exit('file not found');
}

เพราะว่า ออกแบบให้สามารถโหลดไฟล์อะไรก็ได้จาก server เลยมีการใช้ token ช่วยในการตรวจสอบความปลอดภัยเล็กน้อย ของจริงต้องแก้ให้ตรวจ token ซับซ้อนกว่านี้เพื่อความปลอดภัย

ทำ Virtual Host ใน Apache

เพราะว่าต้องแยกส่วนของ frontend และ backend ไว้คนละ server เพื่อความสดวกและปลอดภัย โดยทั้งคู่จะติดต่อกันผ่านทาง api แต่เวลาเขียนมีคอมพิวเตอร์แค่เครื่องเดียว ตามปกติสามารถทำได้โดยแยกงานไว้คนละ folder และเพื่อให้เหมือนของจริงยิ่งขึ้นก็ใข้วิธีทำ Virtual Host แยกออกเป็น 2 เซิร์ฟเวอร์ (ถึงจะแค่เครื่องจำลองก็เถอะ)

ทำได้โดยการเปิดไฟล์ C:\xampp\apache\conf\extra\httpd-vhosts.conf และเพิ่ม


## backend
Listen 81
<VirtualHost *:81>

    CustomLog "D:\xampp\htdocs\cms_backend\logs\apacheAccess.txt" common
    ErrorLog "D:\xampp\htdocs\cms_backend\logs\apacheError.txt"

    php_flag display_errors on
    php_flag log_errors on
    php_value error_log "D:\xampp\htdocs\cms_backend\logs\phpError.txt"
    php_value error_reporting 2147483647

    DocumentRoot "D:\xampp\htdocs\cms_backend\www"

    ServerAdmin [email protected]
    ServerName backend.localhost

</VirtualHost>

## frontend
Listen 82
<VirtualHost *:82>

    CustomLog "D:\xampp\htdocs\cms_frontend\logs\apacheAccess.txt" common
    ErrorLog "D:\xampp\htdocs\cms_frontend\logs\apacheError.txt"

    php_flag display_errors on
    php_flag log_errors on
    php_value error_log "D:\xampp\htdocs\cms_frontend\logs\phpError.txt"
    php_value error_reporting 2147483647

    DocumentRoot "D:\xampp\htdocs\cms_frontend\www"

    ServerAdmin [email protected]
    ServerName frontend.localhost

</VirtualHost>

อธิบาย

Listen
คือหมายเลขพอร์ตที่จะให้รอรับ request อย่าตั้งให้ชนกับตัวอื่น
CustomLog, ErrorLog
คือไฟล์ log ที่แยกออกมาของแต่ละ virtual host โดยสามารถอ่านคู่มือได้จาก Log Files
php_flag, php_value
คือ การปรับแต่ง php เป็นพิเศษสำหรับ server ตัวนี้เท่านั้น
DocumentRoot
คือ folder ที่ไว้เก็บ php ที่เราตั้งใจแยกไว้ เป็น code ชุดเดียวกับที่เราจะเอาขึ้น server จริง
ServerAdmin, ServerName
คือ ข้อมูล server แต่ละตัว

ทดสอบโดย restart apache ใหม่และเปิดเว็บ

จัดระเบียบเซิร์ฟเวอร์

ในครั้งแรกที่เราเรียนเขียนเว็บจะถูกสอนให้เก็บไฟล์ ในโฟลเดอร์ public_html หรือ www เพื่อจะได้เรียกใช้ได้จากเว็บ พอเว็บเราโตขึ้นมันทำให้สับสน และมีปัญหาตามมา เช่น

  1. จากที่อาจจะมีแค่ joomla อยู่อย่างเดียว ก็จะใส่ phpbb เพิ่มเข้าไป ถ้าไม่ย้ายไฟล์ joomla ไปไว้ใน folder ย่อย ก็ต้องใส่ phpbb ไปใน folder ย่อยเวลาจะเรียกใช้ก็เรียก domain1.com/phpbb  ถ้าไม่ถูกใจ phpMyAdmin ตัวเก่าๆที่ติดมากับโฮสต์ จะใช้ตัวใหม่ก็สร้าง folder phpMyAdmin ขึ้นมา ไปเรื่อยๆ แล้วมันจะมีปัญหาอะไรละ
    • ถ้าจะเปลี่ยนจากจูมล่าเป็น wordpress ก็ต้องมาหาว่า folder นี้เป็นของ joomla นะ ต้องดูให้แน่ใจว่ามันไม่ถูกใช้โดยโป��แกรมอ��่น จะท���้งไว้ก็เสียพื้นที่ไปเปล่าๆ
    • การ update อัตโนมัติของบางโปรแกรมอย่าง prestashop ให้วิธีลบทั้งโฟลเดอร์ออกไปทั้งหมด แล้วแตกไฟล์เข้าไปใหม่ ถ้าใน folder นั้นๆ มีไฟล์ของโปรแกรมอื่นละ มันก็ล่มโดยที่เราไม่รู้ตัว
    • บางครั้งโปรแกรมต้องใช้การ config ที่ต่างกัน อย่าง joomla จะเตือนถ้าพบว่า magic_quotes_gpc  เปิดอยู่ แต่โปรแกรมโบราณต้องเปิดไว้ ถ้าเราก็บแยกโพลเดอร์ก็สามารถใช้ .htaccess เปิดปิดได้ตามความต้องการ
  2. จะสร้างเว็บใหม่ ใช้ joomla เหมือนกัน ไว้ที่เดียวกัน บางครั้งสับสน ว่าโพลเดอร์นี้ เป็นของโดเมนไหน

ถ้าเราเก็บไฟล์โดยใช้รูปแบบ public_html/domain1.com, public_html/domain2.com, public_html/ domain3.com ก็จะแก้ปัญหาข้อ 2 ได้

ในแต่ละ domain ก็แบ่งย่อยเป็น joomla, phpbb อีก ให้โปรแกรมแต่ละตัวแยกอิสระต่อกัน

แต่ การที่จะให้ผู้ใช้ลูกค้าของเราดูเว็บ แบบ http://domain1.com/domain1.com/Joomla คงไม่ดีแน่ ทั้งในเรื่องความสะดวกและ SEO เราสามารถใช้ ไฟล์ .htaccess  point ชี้ root diretory ให้เราใหม่

  1. เปิดไฟล์ .htaccess ในโฟลเดอร์ public_html  ถ้าไม่มี ก็สามารถสร้างขึ้นมาใหม่ได้ ด้วย โปรแกรม notepad แล้ว save เป็นชื่อ .htaccess ตรง Save As Type เลือกเป็น All Files (*.*)
  2. พิมพ์ ตามตัวอย่างเลยครับ
    # BlueHost.com
    # .htaccess main domain to subdirectory redirect
    # Do not change this line.
    RewriteEngine on
    # Change example.com to be your main domain.
    RewriteCond %{HTTP_HOST} ^(www.)?example.com$
    # Change 'subdirectory' to be the directory you will use for your main domain.
    RewriteCond %{REQUEST_URI} !^/subdirectory/
    # Don't change the following two lines.
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    # Change 'subdirectory' to be the directory you will use for your main domain.
    RewriteRule ^(.*)$ /subdirectory/$1
    # Change example.com to be your main domain again.
    # Change 'subdirectory' to be the directory you will use for your main domain
    # followed by / then the main file for your site, index.php, index.html, etc.
    RewriteCond %{HTTP_HOST} ^(www.)?example.com$
    RewriteRule ^(/)?$ subdirectory/index.html [L]
    	

    copy คู่มือของเซิร์ฟเวอร์มานั่นละ แต่กลัวลืมเลยขอ note ไว้หน่อยหนึ่ง

  3. แก้ example.com เป็น domain ของเรา แก้ subdirectory เป็นโฟลเดอร์ของเรา
  4. ทดลองเข้า โดเมน domain ของเรา จะพบว่าเราเห็นไฟล์ในโฟลเดอร์ subdirectory อีกชั้นหนึ่งไม่ใช่ ตัว public_html

ปิด magic_quotes ด้วย htaccess

Joomla 3 จะมีปัญหาภาพไม่ขี้น ไม่สามารถติดตั้งได้ เพราะว่าเปิด magic_quotes_gpc ไว้ ถ้าเราไม่สามารถปิด ใน php.ini ได้ (เช่าแชร์เซิร์ฟเวอร์เค้าใช้ต้องทำใจ) หรือมีโปรแกรมโบราณสมัยพระเจ้าหลุยส์ที่สิบสามอยู่ด้วย ทำให้ปิดไม่ได้ เราก็สามารถทำได้ด้วยไฟล์ .htaccess

  1. เปิดไฟล์ .htaccess ในโฟลเดอร์ที่เก็บไฟล์จูมล่าไว้ ถ้าไม่มี ก็สามารถสร้างขึ้นมาใหม่ได้ ด้วย โปรแกรม notepad แล้ว save เป็นชื่อ .htaccess ตรง Save As Type เลือกเป็น All Files (*.*)
  2. พิมพ์
    # disabel magic_quotes for joomla
    php_flag magic_quotes off
    php_flag magic_quotes_gpc off
    php_value magic_quotes 0
    php_value magic_quotes_gpc 0
    

Mail Server อื่น MX Record

ถ้าต้องการใช้ e-mail ที่ซื้อมาจาก google app หรือจากอีเมล์เซอร์ฟเวอร์อื่นๆ แทนตัวที่แถมมากับโฮตส์ เราต้องเข้าไปเซ็ตค่า MX record ใน server ของเราด้วย เพราะว่าถ้าไม่ทำจะเจอปัญหา สามารถส่งอีเมล์ออกไปได้ แต่ไม่สามารถรับได้
วิธีการ

  1. เข้าไป control panel เซิร์ฟเวอร์ที่เราใช้บริการ
  2. หา MX Record ส่วนใหญ่จะมีใน Domain หรือบางที่จะอยู่ในหมวดอีเมล์
  3. ลบข้่อมูล MX Record เดิมออก แทนที่ด้วยข้อมูลของที่เราใช้

ตังอย่าง ถ้าเช่า gmail เอาไว้ค่าที่ต้องใส่จะตามตัวอย่าง

Name/Host/Alias Time to Live (TTL) Record Type Value/Answer/Destination
Blank or @ 3600 MX 1 ASPMX.L.GOOGLE.COM
Blank or @ 3600 MX 5 ALT1.ASPMX.L.GOOGLE.COM
Blank or @ 3600 MX 5 ALT2.ASPMX.L.GOOGLE.COM
Blank or @ 3600 MX 10 ASPMX2.GOOGLEMAIL.COM
Blank or @ 3600 MX 10 ASPMX3.GOOGLEMAIL.COM
https://support.google.com/a/answer/174125?hl=en 

การ config visual Directory ใน apache

หลังจากลง appserv หรือ xampp เราก็จะสามารถเข้าไปเพื่อเขียนงานได้ทันที ในบางครั้งการทำเว็บก็มีความต้องการให้บางส่วนของเว็บทำงานเป็นพิเศษต่างจากปกติที่โปรแกรมจัดไว้ให้ เช่น เว็บมีขนาดใหญ่มากไม่เหมาะที่จะเก็บไว้ในไดร์ฟเดียวกับเว็บอื่นๆ ไม่ต้องการที่จะที่จะเก็บไว้ในไดร์ฟซี หรือต้องวางไว้โดยใช้ port พิเศษเพื่อความปลอดภัย
ใช้ editplus เปิด Config จะอยู่ที่ C:\xampp\apache\conf\httpd.conf ใช้ เนื่องจากต้องการจะแยก config ทีเพิ่มมาใหม่ไว้อีกที่เพื่อความเป็นระเบียบและบริหารง่าย จะใช้วิธี include ให้ไปอ่านจากอีกไฟล์หนึ่ง เพิ่มบรรทัด
Include “C:\xampp\apache\conf\extra\httpd-plus.conf” หลัง include คือ ชื่อไฟล์และพาร์ทที่จะเก็บ config ตัวใหม่
ถ้าต้องการ map directory ใหม่ใช้

Alias /www2 "d:/www2/"
<Directory "d:/www2">
AllowOverride All
Order allow,deny
Allow from all
</Directory>

อธิบายคือ map directory d:/www2 โดยจะสามารถเรียกโดย url http://localhost/www2/
ถ้าต้องการกำหนด port หรือค่าอื่นๆด้วย

Listen 8080
Alias /www2 " d:/www2/"
<Directory "d:/www2/">
Options Indexes FollowSymLinks MultiViews ExecCGI
AllowOverride All
Order allow,deny
Allow from all
</Directory>
<VirtualHost *:8080>
ServerAdmin [email protected]
DocumentRoot "d:/www2"
ServerName Plus.localhost
ServerAlias dev.localhost
ErrorLog "logs/Plus.localhost-error.log"
CustomLog "logs/Plus.localhost-access.log" combined
</VirtualHost>

จะต่างกันที่เรียกโดย url http://localhost:8080 คือต้องระบุ port ด้วยและ config บางส่วนจะต่างกัน
เช่น การกำหนดอีเมล์ผู้ดูแล domain
อ่าน ทำ dns (Domain Name System) ส่วนตัว เพิ่มเติมครับ