VPS筆記 Ubutnu 16.04 Nginx + PHP7 + MariaDB

系統升級 & 防牆設定
aptitude update
aptitude dist-upgrade
apt-get clean
ufw enable
ufw allow 80/tcp
ufw allow from xxx.xxx.xxx.xxx
vi /etc/hostname
vi /etc/hosts
reboot

安裝網頁Nginx及資料庫伺服器MariaDB
aptitude install nginx mariadb-server
ufw allow 'Nginx HTTP'
ufw allow 'Nginx HTTPS'  <= 很重要!
install php-fpm php-mysql php-gd php-xml php-mbstring php-zip postfix

vi /etc/php/7.0/fpm/php.ini
cgi.fix_pathinfo=0

systemctl restart php7.0-fpm

mysql -u root -p (第一次不用密碼)

GRANT all ON *.* TO root@'localhost' IDENTIFIED BY '密碼';
FLUSH PRIVILEGES;
exit

發現一直不斷的收到錯誤訊息的信件
/etc/cron.daily/logrotate:
mysqladmin: connect to server at 'localhost' failed
error: 'Access denied for user 'root'@'localhost' (using password: NO)'
error: error running shared postrotate script for '/var/log/mysql/mysql.log /var/log/mysql/mysql-slow.log /var/log/mysql/mariadb-slow.log /var/log/mysql/error.log '
run-parts: /etc/cron.daily/logrotate exited with return code 1

查到這一段
vi /etc/logrotate.d/mysql-server
mysqladmin --defaults-file=/etc/mysql/debian.cnf flush-logs
變怪的,應該是BUG吧,因為修了debian.cnf的內容,即便是給了密碼,還是會出現錯誤訊息

解決方式就是自訂帳密囉~
mysqladmin --defaults-file=/etc/mysql/debian.cnf flush-logs -u root -prootassword

安裝Drush
curl -sS https://getcomposer.org/installer | php
mv composer.phar composer
alias composer='/usr/bin/php ~/composer'
composer global require drush/drush
alias drush='/usr/bin/php ~/.config/composer/vendor/drush/drush/drush.php'

Nginx的設定檔寫入方式
vi /etc/nginx/sites-available/www.yoursite.com
ln -s /etc/nginx/sites-available/www.yoursite.com /etc/nginx/sites-enabled/www.yoursite.com
/etc/init.d/nginx restart


Drupal7 Nginx設定檔參考
server {
    listen 80 ;
    server_name www.yoursite.com;
    root /var/www/yoursite; ## <-- only="" p="" path="" reference.="" your="">
    location = /favicon.ico {
        log_not_found off;
        access_log off;
    }

server {
    listen              443 ssl;
    ssl_protocols TLSv1.2;
    ssl_ciphers 'ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256';
    ssl_prefer_server_ciphers On;
    ssl_certificate /etc/letsencrypt/live/yoursite.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/yoursite.com/privkey.pem;
    ssl_trusted_certificate /etc/letsencrypt/live/yoursite.com/chain.pem;
    ssl_session_cache shared:SSL:128m;
    add_header Strict-Transport-Security "max-age=31557600; includeSubDomains";
    ssl_stapling on;
    ssl_stapling_verify on;
    server_name www.yoursite.com;
    resolver 8.8.8.8;
    root /var/www/yoursite; ## <-- only="" p="" path="" reference.="" your="">}
<-- only="" p="" path="" reference.="" your="">
    location = /robots.txt {
        allow all;
        log_not_found off;
        access_log off;
    }

    # Very rarely should these ever be accessed outside of your lan
    location ~* \.(txt|log)$ {
        allow 192.168.0.0/16;
        deny all;
    }

    location ~ \..*/.*\.php$ {
        return 403;
    }

    location ~ ^/sites/.*/private/ {
        return 403;
    }

    # Allow "Well-Known URIs" as per RFC 5785
    location ~* ^/.well-known/ {
        allow all;
    }

    # Block access to "hidden" files and directories whose names begin with a
    # period. This includes directories used by version control systems such
    # as Subversion or Git to store control files.
    location ~ (^|/)\. {
        return 403;
    }

    location / {
        try_files $uri @rewrite; # For Drupal <= 6
        # try_files $uri /index.php?$query_string; # For Drupal >= 7
    }

    location @rewrite {
rewrite ^/(.*)$ /index.php?q=$1;
    }

    # Don't allow direct access to PHP files in the vendor directory.
    location ~ /vendor/.*\.php$ {
        deny all;
        return 404;
    }

    # In Drupal 8, we must also match new paths where the '.php' appears in
    # the middle, such as update.php/selection. The rule we use is strict,
    # and only allows this pattern with the update.php front controller.
    # This allows legacy path aliases in the form of
    # blog/index.php/legacy-path to continue to route to Drupal nodes. If
    # you do not have any paths like that, then you might prefer to use a
    # laxer rule, such as:
    #   location ~ \.php(/|$) {
    # The laxer rule will continue to work if Drupal uses this new URL
    # pattern with front controllers other than update.php in a future
    # release.
    location ~ '\.php$|^/update.php' {
        fastcgi_split_path_info ^(.+?\.php)(|/.*)$;
        # Security note: If you're running a version of PHP older than the
        # latest 5.3, you should have "cgi.fix_pathinfo = 0;" in php.ini.
        # See http://serverfault.com/q/627903/94922 for details.
        include fastcgi_params;
        # Block httpoxy attacks. See https://httpoxy.org/.
        fastcgi_param HTTP_PROXY "";
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        fastcgi_param QUERY_STRING $query_string;
        fastcgi_intercept_errors on;
        # PHP 5 socket location.
        #fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_read_timeout 30;
        # PHP 7 socket location.
         fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
    }

    # Fighting with Styles? This little gem is amazing.
     location ~ ^/sites/.*/files/imagecache/ { # For Drupal <= 6
    #location ~ ^/sites/.*/files/styles/ { # For Drupal >= 7
        try_files $uri @rewrite;
    }

    # Handle private files through Drupal. Private file's path can come
    # with a language prefix.
    location ~ ^(/[a-z\-]+)?/system/files/ { # For Drupal >= 7
        try_files $uri /index.php?$query_string;
    }

    location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
        expires max;
        log_not_found off;
    }
}
<-- only="" p="" path="" reference.="" your="">
Drush更新程式
alias drush='/usr/bin/php ~/.config/composer/vendor/drush/drush/drush.php'
drush updatedb;drush pm-update -y;drush l10n-update-refresh;drush l10n-update;drush cc all


Nginx進站加密
    location / {
        auth_basic "Restricted Content"; # 加密用
        auth_basic_user_file /etc/nginx/.htpasswd; # 加密用
        try_files $uri @rewrite; # For Drupal <= 6
        # try_files $uri /index.php?$query_string; # For Drupal >= 7
    }

Nginx使用https
server {
   listen       80;
   server_name www.yoursite.com;
   return 301 https://www.yoursite.com$request_uri;
}

server {
    listen              443 ssl;
    ssl_protocols TLSv1.2;
    ssl_ciphers 'ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256';
    ssl_prefer_server_ciphers On;
    ssl_certificate /etc/letsencrypt/live/yoursite.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/yoursite.com/privkey.pem;
    ssl_trusted_certificate /etc/letsencrypt/live/yoursite.com/chain.pem;
    ssl_session_cache shared:SSL:128m;
    add_header Strict-Transport-Security "max-age=31557600; includeSubDomains";
    ssl_stapling on;
    ssl_stapling_verify on;
<-- only="" p="" path="" reference.="" your="">     server_name www.yoursite.com;
    root /var/www/yoursite; ## <-- only="" p="" path="" reference.="" your="">
    location = /favicon.ico {
        log_not_found off;
      <-- only="" p="" path="" reference.="" your=""> access_log off;
}
------------------------------------------------------------------

錯誤小筆記

中間如果有遇到權限問題,記得chown -R www-data:www-data sites/default/files/


$conf['advagg_skip_404_check'] = TRUE;
https://www.drupal.org/node/2318375


Nginx使用SSL憑證的方式有點不太一樣!務必照著下面網頁的方法做,因為我原先用之前使用Apache時的憑證,遇到憑證出錯的問題,應該是說Nginx需求的憑證要求會比較多

https://letsecure.me/secure-web-deployment-with-lets-encrypt-and-nginx/

htpasswd
https://www.digitalocean.com/community/tutorials/how-to-set-up-password-authentication-with-nginx-on-ubuntu-14-04

留言

熱門文章