2017年12月4日 星期一

FreeBSD 安裝與設定apache+php 作業筆記


安裝apache

cd /usr/ports/www/apache24下 make all設定並編譯,最後make install

安裝完後,開始設定:

使用 ip 和 Domain 瀏覽網站根目錄會看到不同內容


編輯/usr/local/etc/apache24/httpd.conf

ServerName 改為自己的domain name

預設有一個Directory structure,這個設為使用者用Domain方式進入的Directory

DocumentRoot "/usr/local/www/apache24/data"
<Directory "/usr/local/www2/apache24/data">
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>


多新增一個Directory structure,這個設為使用者用IP方式進入的Directory

DocumentRoot "/usr/local/www/apache24/data"
<Directory "/usr/local/www2/apache24/data">
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>

將此行的註解拿掉
Include etc/apache24/extra/httpd-vhosts.conf
接著設定 /usr/local/etc/apache24/extra/httpd-vhosts.conf

<VirtualHost *:80>
    DocumentRoot "/usr/local/www/apache24/data"
    ServerName  your.domain.name
</VirtualHost>

<VirtualHost *:80>
    DocumentRoot "/usr/local/www2/apache24/data"
    ServerName  xxx.xxx.xxx.xxx
</VirtualHost>

service apache start 啟動apache

此時在瀏覽器上分別輸入自己server的Domain name和ip瀏覽,應會看到不同資料夾下的html

啟用HTTPS

申請SSL憑證,這邊我是到SSL FOR FREE申請,點Manual Verification照他的官方說明,下載他為你網站產生的憑證檔,其中包含ca_bundle.crt, certificate.crt, private.key
將它們放在/usr/local/etc/apache24/ssl資料夾下,並編輯/usr/local/etc/apache24/httpd.conf
以下幾行取消註解
LoadModule socache_shmcb_module libexec/apache24/mod_socache_shmcb.so
LoadModule ssl_module libexec/apache24/mod_ssl.so
Include etc/apache24/extra/httpd-ssl.conf
<IfModule ssl_module>
SSLRandomSeed startup builtin
SSLRandomSeed connect builtin
</IfModule>

編輯/usr/local/etc/apache24/extra/httpd-ssl.conf
確認VirtualHost 標籤下有以下設定
<VirtualHost _default_:443>
DocumentRoot "/usr/local/www/apache24/data"
ServerName your.domain.name:443
ServerAdmin you@example.com
SSLEngine on
SSLCertificateFile "/usr/local/etc/apache24/ssl/certificate.crt"(your file path)
SSLCertificateKeyFile "/usr/local/etc/apache24/ssl/private.key"(your file path)
SSLCACertificateFile "/usr/local/etc/apache24/ssl/ca_bundle.crt"(your file path)



service apache24 restart

在瀏覽器輸入https://自己的網址/index.html 理應可以連上

HTTP rediect to HTTPS

編輯/usr/local/etc/apache24/extra/httpd-vhosts.conf
在VirtualHost結構下加入Redirect 設定如下:

<VirtualHost *:80>
    DocumentRoot "/usr/local/www/apache24/data"
    ServerName  your.domain.name
    Redirect / https://your.domain.name/
</VirtualHost>

service apache24 restart
在瀏覽器輸入http://your.domain.name/index.html ,會自動導至https://your.domain.name/index.html

啟用HSTS

編輯/usr/local/etc/apache24/extra/httpd-vhosts.conf與/usr/local/etc/apache24/extra/httpd-ssl.conf
在VirtualHost結構下加入Redirect 設定如下:

<VirtualHost *:80>
    DocumentRoot "/usr/local/www/apache24/data"
    ServerName  your.domain.name
    Redirect / https://your.domain.name/
    Header always set Strict-Transport-Security "max-age=63072000; includeSubdomains; preload"
</VirtualHost>

service apache24 restart

curl -Ik your.domain.name,應為出現strict-transport-security: max-age=63072000; includeSubdomains; preload

啟用HTTP2

編輯/usr/local/etc/apache24/httpd.conf

設定
LoadModule mpm_event_module libexec/apache24/mod_mpm_event.so

註解掉
LoadModule mpm_prefork_module libexec/apache24/mod_mpm_prefork.so

在檔案最底端加入
<IfModule http2_module>
    LogLevel http2:info
    Protocols h2 h2c http/1.1
    H2Direct on
</IfModule>

service apache24 restart

curl --http2 -I your.domain.name,應會看到改連線改為HTTP/2
也可利用此網站https://tools.keycdn.com/http2-test
測試自己的網站是否已支援HTTP/2

設定Rewrite

編輯/usr/local/etc/apache24/httpd.conf
取消註解此行
LoadModule rewrite_module libexec/apache24/mod_rewrite.so

/usr/local/etc/apache24/extra/httpd-ssl.conf
在VirtualHost結構下加入Redirect 設定如下:
RewriteEngine on
RewriteRule /home/([a-zA-Z0-9]*)/(.*)$ http://your.domain.name/~$1/$2 [L]

安裝php

cd /usr/ports/lang/php70
make all設定並編譯,最後make install
編輯/etc/rc.conf
新增php_fpm_enable="YES"

到/usr/local/etc/下將php.ini-production複製一份命名為php.ini,作為php讀取初始化設定的設定檔

編輯php.ini,將short_open_tag 改為On

short_open_tag = On

在/usr/local/etc/apache24/Includes/ 新增php-fpm.conf並編輯
<FilesMatch "\.php$">
    SetHandler application/x-httpd-php
</FilesMatch>


編輯/usr/local/etc/apache24/extra/httpd-ssl.conf
在virtualhost結構下新增
ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/usr/local/www/apache24/data/$1
(讓apache 可請求正在監聽 port 9000 的 php-fpm 執行 php 檔)


編輯/usr/local/etc/apache24/httpd.conf

將必要的proxy_xx取消註解

在www目錄下新增index.php用來測試php是否設定成功
檔案內容:

<?php
phpinfo();
?>

service php-fpm restart 
service apache24 restart

這時在瀏覽器上輸入自己的網址/index.php,就會看到php資訊頁面

安裝php module

find /usr/ports -type d -iname "php70-*" 列出所有php70 module所在資料夾,
根據想裝的module到對應的資料夾下
例如:
欲安裝php70 opernssl module,該module在/usr/ports/security/php70-openssl/下
cd /usr/ports/security/php70-openssl/
make deinstall
make clean
make all install

service php-fpm restart
在重啟時若無顯示錯誤訊息,及代表安裝成功
也可用利用phpinfo()或php -m 確認是否有安裝成功

Fake server token and hide php info

使用curl -Ik my.server.name 確認目前Server token
應為apache名稱+版本+php資訊

隱藏php資訊:
編輯 /usr/local/etc/php.ini,將expose_php改為Off
expose_php = Off

偽裝server token:
cd /usr/ports/www/mod_sercurity
make all,並make install
此時/usr/local/etc/apache24/modules.d下會多出一名為280_mod_security.conf的檔案,編輯該檔
將檔案最下方的三行設定取消註解
LoadModule unique_id_module libexec/apache24/mod_unique_id.so
LoadModule security2_module libexec/apache24/mod_security2.so
Include /usr/local/etc/modsecurity/*.conf

編輯 /usr/local/etc/apache/httpd.conf,在最下方加入:

ServerTokens Full
SecServerSignature fake_server_token (ex:Microsoft-IIS/6.0)

service apache24 restart

此時再用curl -Ik my.server.name 確認Server token
理應為fake server token,且沒有php相關資訊

安裝mysql


安裝phpMyAdmin

先安裝composer,可到官網依照提示指令安裝https://getcomposer.org/download/

若過程中出現缺少php extension錯誤訊息,須另行安裝對應php module

composer create-project phpmyadmin/phpmyadmin

將裝好的phpmyadmin 複製到/usr/loca/www/下並命名為phpMyAdmin

編輯/usr/local/etc/apache/httpd.conf

Alias /phpmyadmin/ "/usr/local/www/phpMyAdmin/"
<Directory "/usr/local/www/phpMyAdmin/">
    Options Indexes FollowSymLinks MultiViews
    DirectoryIndex index.php
    AllowOverride all
    Require all granted
</Directory>

編輯/usr/local/etc/apache24/extra/httpd-ssl.conf
在virtual host結構下新增
ProxyPassMatch ^/phpmyadmin/(.*\.php)$ fcgi://127.0.0.1:9000/usr/local/www/phpMyAdmin/$1

service php-fpm restart
service apache24 restart

在瀏覽器輸入https://yourdomain.name/phpmyadmin/
應會看到phpmyadmin登入畫面







沒有留言:

張貼留言