顯示具有 php 標籤的文章。 顯示所有文章
顯示具有 php 標籤的文章。 顯示所有文章

2017年7月7日 星期五

[CentOS 7.3] 安裝Apache, MariaDB, PHP(LAMP)

sudo yum –y update
sudo yum –y upgrade
sudo yum -y install yum-utils

一、安裝apache
sudo yum install httpd //安裝apache
sudo systemctl start httpd //開啟伺服器
sudo systemctl enable httpd //開機會自動開啟伺服器

二、安裝MariaDB
sudo yum -y install mariadb-server mariadb
sudo systemctl start mariadb.service
sudo systemctl enable mariadb.service
mysql_secure_installation


三、安裝PHP 7.1
sudo yum install epel-release
sudo wget -q http://rpms.remirepo.net/enterprise/remi-release-7.rpm
sudo rpm -Uvh remi-release-7.rpm
sudo yum-config-manager --enable remi-php71
sudo yum -y install php php-opcache


四、重新整理
sudo yum update
sudo systemctl restart httpd.service


五、測試PHP
sudo nano /var/www/html/info.php

<?php
            phpinfo();
?>

PS:
chcon -R -t httpd_sys_rw_content_t storage
sudo setsebool -P httpd_can_network_connect_db=1
yum install php php-mysql
sudo systemctl restart httpd.service

https://linuxhostsupport.com/blog/how-to-upgrade-mariadb-on-centos-7/

2017年3月12日 星期日

[Linux]快速架設Apache + MySQL + PHP

讓你快速架設LAMP Server

1. 安裝tasksel
sudo apt-get install tasksel

2. 安裝LAMP Server
sudo tasksel install lamp-server

3. 如果你想安裝phpmyadmin的話(),安裝時會要求輸入mysql密碼及phpmyadmin root 登入密碼
sudo apt-get insatll phpmyadmin

完成後可以打開瀏覽器輸入127.0.0.1 or localhost 看看有沒有出現 It works!的網頁資訊
( 有出現代表LAMP安裝成功。 )

再輸入看看127.0.0.1/phpmyadmin or localhost/phpmyadmin,應該會出現phpmyadmin登入畫面,帳號:root 密碼:安裝時所設定的。


都有成功跑出來及登入,就代表成功安裝。
sudo mysql_secure_installation

[Apache] Linux Apache隱藏伺服器版本-資訊安全

為了加強網頁安全性,不讓別人看到apache版本資訊

1. 編輯apache2.conf
sudo vi /etc/apache2/apache2.conf

2. 將此兩行指令新增到文件最下方
ServerSignature Off
ServerTokens Prod

3. 重啟apache
sudo /etc/init.d/apache2 restart


[PHP] Linux Apache PHP隱藏伺服器版本-資訊安全

為了加強網頁安全性,不讓別人看到伺服器PHP版本資訊

1. 編輯php.ini
sudo vi /etc/php5/apache2/php.ini

2. expose_php改成Off
expose_php = Off

3. 重啟apache

sudo /etc/init.d/apache2 restart

[Apache] 防止別人嵌入您的網頁

為了加強網頁安全性,不讓別人iframe你的網頁

1. 打開apache headers mods
sudo a2enmod headers

2. 重啟apache
sudo /etc/init.d/apache2 restart

3. 編輯apach2.conf
sudo nano /etc/apache2/apache2.conf

4. Directory /var/www/html/ 裡面加入
Header always append X-Frame-Options SAMEORIGIN

5. 重啟apache
sudo /etc/init.d/apache2 restart

2015年11月10日 星期二

【PHP】帳號密碼如何加密? Hashing with Salt

先定義PHP變數
define("PBKDF2_HASH_ALGORITHM", "sha256");
define("PBKDF2_ITERATIONS", 1000);
define("PBKDF2_SALT_BYTE_SIZE", 24);
define("PBKDF2_HASH_BYTE_SIZE", 24);

define("HASH_SECTIONS", 4);
define("HASH_ALGORITHM_INDEX", 0);
define("HASH_ITERATION_INDEX", 1);
define("HASH_SALT_INDEX", 2);
define("HASH_PBKDF2_INDEX", 3);

利用PHP mcrypt_create_iv function 先產生SALT

再把要密碼前面加上SALT,然後進行SHA256加密。

最好用PBKDF2 (Password-Based Key Derivation Function 2)方法加密

function create_hash($password)
{
    // format: algorithm:iterations:salt:hash
    $salt = base64_encode(mcrypt_create_iv(PBKDF2_SALT_BYTE_SIZE, MCRYPT_RAND));
    return PBKDF2_HASH_ALGORITHM . ":" . PBKDF2_ITERATIONS . ":" .  $salt . ":" . 
        base64_encode(pbkdf2(
            PBKDF2_HASH_ALGORITHM,
            $password,
            $salt,
            PBKDF2_ITERATIONS,
            PBKDF2_HASH_BYTE_SIZE,
            true
        ));
}
function validate_password($password, $correct_hash)
{
    $params = explode(":", $correct_hash);
    if(count($params) < HASH_SECTIONS)
       return false; 
    $pbkdf2 = base64_decode($params[HASH_PBKDF2_INDEX]);
    return slow_equals(
        $pbkdf2,
        pbkdf2(
            $params[HASH_ALGORITHM_INDEX],
            $password,
            $params[HASH_SALT_INDEX],
            (int)$params[HASH_ITERATION_INDEX],
            strlen($pbkdf2),
            true
        )
    );
}
function pbkdf2($algorithm, $password, $salt, $count, $key_length, $raw_output = false)
{
    $algorithm = strtolower($algorithm);
    if(!in_array($algorithm, hash_algos(), true))
        trigger_error('PBKDF2 ERROR: Invalid hash algorithm.', E_USER_ERROR);
    if($count <= 0 || $key_length <= 0)
        trigger_error('PBKDF2 ERROR: Invalid parameters.', E_USER_ERROR);

    if (function_exists("hash_pbkdf2")) {
        // The output length is in NIBBLES (4-bits) if $raw_output is false!
        if (!$raw_output) {
            $key_length = $key_length * 2;
        }
        return hash_pbkdf2($algorithm, $password, $salt, $count, $key_length, $raw_output);
    }

    $hash_length = strlen(hash($algorithm, "", true));
    $block_count = ceil($key_length / $hash_length);

    $output = "";
    for($i = 1; $i <= $block_count; $i++) {
        // $i encoded as 4 bytes, big endian.
        $last = $salt . pack("N", $i);
        // first iteration
        $last = $xorsum = hash_hmac($algorithm, $last, $password, true);
        // perform the other $count - 1 iterations
        for ($j = 1; $j < $count; $j++) {
            $xorsum ^= ($last = hash_hmac($algorithm, $last, $password, true));
        }
        $output .= $xorsum;
    }

    if($raw_output)
        return substr($output, 0, $key_length);
    else
        return bin2hex(substr($output, 0, $key_length));
}

PHP Source Code PasswordHash.php

[CentOS] httpd port 9000 to 80

<VirtualHost *:80>     ServerName domain.name     ProxyRequests Off     ProxyVia Block     ProxyPreserveHost On     <Proxy *...