Ubuntu 18.04 Server LTS 安裝筆記 + nginx共用php5, php7

Ubuntu 18.04 是LTS的版本,因此特別花了點時間研究了一下,VPS應該都還沒上吧?


這次的裝機流程畫面可真是大改版,我都不認識了 XD

安裝好後,發現並沒有如以前讓你安裝 mail server , web server....等資訊,所有的東西都得自己來唷!但他會問你要不要用docker之類的程式,大概主機都要開始走虛擬化的概念了吧?

主機裝好後方便遠端操作先補上 openssh-server

檢查了一下 /etc/apt/sources.list 的來源很少
deb http://archive.ubuntu.com/ubuntu bionic main
deb http://archive.ubuntu.com/ubuntu bionic-security main
deb http://archive.ubuntu.com/ubuntu bionic-updates main
因此我無法使用 apt install drupal drush去裝齊server環境

我直接下指令 apt-instal nginx 發現有些錯誤訊息,跑太快也沒特別注意
後來由於我想測試PHP7跟PHP5共存的實驗,所以我為了加裝PHP5.6,因此加裝額外的來源
sudo apt install software-properties-common
sudo add-apt-repository ppa:ondrej/php

開始處理 LEMP
sudo apt update
sudo apt install nginx php7.2 mysql-server postfix

MySql設定root角色,預設的root角色不能做什麼,基於安全理由,他其實是要你自己創一個非root的使用者。如果你懶的創可以執行下面的指令(注意!此流程是把root角色移掉,再重創一次)

sudo mysql -u root
進入mysql

SELECT User,Host FROM mysql.user;
列出使用者

DROP USER 'root'@'localhost';
把root刪掉

CREATE USER 'root'@'localhost' IDENTIFIED BY '';
重新加入root

GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION;
給root角色權限

ALTER USER 'root'@'localhost' IDENTIFIED BY 'yourpassword';
給root角色密碼

FLUSH PRIVILEGES;
寫入

exit
退出

改完後就能下指令 mysqadmin -u root -pxxxx 去處理一些sql的東西

跑Drupal需再補齊這些
php7.2-gd php7.2-xml php7.2-mbstring php7.2-fpm php7.2-mysql
php5.6-gd php5.3-xml php5.6-mbstring php5.6-fpm php5.6-mysql (可略過)
PS.未來還是會有機會遇到某些模組有需求php-xxx依賴

把Drupal抓下來
cd /var/www/
sudo wget https://ftp.drupal.org/files/projects/drupal-7.59.tar.gz
sudo tar zxvf drupal-7.59.tar.gz
mv drupal-7.59 drupal
chown www-data:www-data drupal/sites/default/

安裝Drush
sudo apt install unzip
curl -sS https://getcomposer.org/installer | php
mv composer.phar composer
alias composer='/usr/bin/php ~/composer'
composer global require drush/drush:8.1.9
alias drush='/usr/bin/php ~/.config/composer/vendor/drush/drush/drush.php'
( or alias drush='/usr/bin/php ~/.composer/vendor/drush/drush/drush.php' )

Nginx conf檔
server {
   listen 80;
    server_name testsite.com;
    root /var/www/drupal; ## <-- only="" p="" path="" reference.="" your="">
    location = /favicon.ico {
        log_not_found off;
        access_log off;
    }

    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.
<-- only="" p="" path="" reference.="" your="">         fastcgi_pass unix:/var/run/php/php5.6-fpm.sock;
        fastcgi_read_timeout 30;
        # PHP 7 socket location.
        # fastcgi_pass unix:/var/run/php/php7.2-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;
    }
}


切換PHP5、PHP7,Drupal後台報告確認
PHP7
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;


PHP 7.2有點小錯誤要處理

PS.我有另一台主機跑 Drupal 7 + PHP7.2 沒看到這段錯誤

PHP5
fastcgi_pass unix:/var/run/php/php5.6-fpm.sock;

PHP 5.6 上面錯誤訊息消失了,底下那段HTTP request status錯誤是因為我是執行測試環境,所以才會出現

重開機後發現跑很快的那一段文字就是指在apache啟動錯誤
sudo apt remove apahce2 即可

後記,要在同一台主機實現php不同版次共存,只需透過php-fpm切換即可輕易達成
我其實是在自己的VPS主機把PHP7.0升級到7.2時,才發現實做起來不會太難

另外,需特別注意一件事情,由於我有其他的WEB HOSTING,可能是SQL版本有差距吧,我發現18.04 上的SQL無法入到WEB HOSTING ??

Ubuntu 18.04
mysql --version
mysql  Ver 14.14 Distrib 5.7.23, for Linux (x86_64) using  EditLine wrapper

WEB HOSTING
mysql --version
mysql  Ver 14.14 Distrib 5.6.39, for Linux (x86_64) using  EditLine wrapper




錯誤
SQL 查詢:


CREATE TABLE `actions` (
  `aid` varchar(255) NOT NULL DEFAULT '0' COMMENT 'Primary Key: Unique actions ID.',
  `type` varchar(32) NOT NULL DEFAULT '' COMMENT 'The object that that action acts on (node, user, comment, system or custom types.)',
  `callback` varchar(255) NOT NULL DEFAULT '' COMMENT 'The callback function that executes when the action runs.',
  `parameters` longblob NOT NULL COMMENT 'Parameters to be passed to the callback function.',
  `label` varchar(255) NOT NULL DEFAULT '0' COMMENT 'Label of the action.',
  PRIMARY KEY (`aid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC COMMENT='Stores action information.'
MySQL 回應: 說明文件

#1071 - Specified key was too long; max key length is 767 bytes

20190103重裝一次筆記

01.裝機時改用 mariadb,安裝時發現nginx也會裝apache,所以必需把apache2 disable掉
sudo apt install nginx mariadb-server mariadb-client php php-fpm php-xml php-mbstring php-mysql postfix
sudo update-rc.d apache2 disable
sudo update-rc.d nginx enable

02.mysql密碼改成這個方式較好??
sudo mysql_secure_installation
(或許不用跑這段)

第1個問句直接按enter
第2,3個問句就是root密碼
接下來的問句全部按Y

測試看看,輸入密碼能出現版本資訊就搞定囉
sudo mysqladmin -p -u root version
( 記得加sudo )

更正一下,我在執行完 mysql_secure_installation 後,雖然可以加資料庫了,但是在裝drupal時遇到遇到登不進資料庫的情況,改回老方式,改密碼的那段與MySQL不同,留意一下囉

sudo mysql -u root
DROP USER 'root'@'localhost';
CREATE USER 'root'@'localhost' IDENTIFIED BY '';
GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION;
SET PASSWORD FOR 'root'@'localhost' = PASSWORD('yourpassword');
FLUSH PRIVILEGES;


Nginx + SSL的筆記
https://www.digitalocean.com/community/tutorials/how-to-secure-nginx-with-let-s-encrypt-on-ubuntu-18-04

SSL如果無法用apt可試這個方式
https://certbot.eff.org/docs/install.html

留言

熱門文章