iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 2
0
Mobile Development

小菜逼學習IOS系列 第 2

學習NextCloud IOS APP 倒數第29篇 -NextCloud伺服器架設(上)

今天是開賽的第二天,架設完伺服器,好讓珍惜已久的照片影片存起來。

架設伺服器需要一台不重要的電腦環境還是ubuntu,若不是也可以用虛擬機再下載ubuntu,但規格建議頂規喔,(容量建議要越大越好喔,這樣才能存放越多寶貝們)
當天時地利人和且發生日全食、海嘯和不重要電腦準備好時,就開始吧。

因為Nextcloud是PHP寫的,而資料庫為Mysql環境,所以要先安裝一下LEMP環境

安裝Nginx

首先輸入指令update一下

sudo apt-get update

再開始安裝nginx

sudo apt-get install nginx

此時nginx便已經跑靜態網頁了 預設html檔案存放位置為/var/www/html

安裝MySQL Server

接著安裝mysql server伺服器 輸入指令

sudo apt-get install mysql-server

安裝到一半會跳到一個畫面,表示要設定root帳號(管理員帳號)的密碼,設定自己想要的密碼,請不要忘記

安裝完後輸入以下指令進行安全性設定

sudo mysql_secure_installation

之後會依序詢問一些問題

VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin?

Press y|Y for Yes, any other key for No:

是否需要安裝VALIDATE PASSWORD PLUGIN 其用途為當在修改密碼時會進行密碼檢測 當密碼強度不夠會無法設定完成 依照個人需求設定 輸入y|Y 表示是 輸入其他表示否

Change the password for root ? ((Press y|Y for Yes, any other key for No) :

此為詢問是否要改變root密碼

By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No) :

此問題為Mysql會預設建立匿名使用者,表示當使用command登入mysql的時候可以不需要填寫帳號密碼(用於test使用),是否要將匿名使用者刪除 (這邊建議回答Y)

Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) :

這在詢問root帳號是否只能透過本機機器進行連線,或是可以其他IP連線,是否要禁止root可以透過遠端連線

這部份可以依據使用的情況去選擇,而如果是production環境建議回答Y來禁止別的IP連此root帳號

By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.

Remove test database and access to it? (Press y|Y for Yes, any other key for No) :

在預設時Mysql會建立一個database叫test,此資料庫所有帳號接可以存取使用它(用於測試目的),詢問是否要刪除這個test資料庫?

Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) :

問是否要重新載入privilege table,只有重新載入後剛剛的設定才會立即生效,否則要等到下次重新啟動Mysql資料庫

恭喜完成第一步囉

安裝PHP (PHP-fpm)

安裝PHP (因為還要讓php能夠連mysql所以也需要安裝php-mysql)

sudo apt-get install php-fpm php-mysql

當安裝完成後輸入以下指令修改一下PHP設定

sudo nano /etc/php/7.2/fpm/php.ini

找到這一行

;cgi.fix_pathinfo=1

改成這個

cgi.fix_pathinfo=0

設定完後儲存並輸入指令重新啟動PHP

sudo systemctl restart php7.2-fpm

然後要設定讓nginx能夠執行PHP腳本
輸入以下指令修改nginx設定

sudo nano /etc/nginx/sites-available/default

會看到如下

server {
    listen 80 default_server;
    listen [::]:80 default_server;
 
    root /var/www/html;
    index index.html index.htm index.nginx-debian.html;
 
    server_name _;
 
    location / {
        try_files $uri $uri/ =404;
    }
}

改成

server {
    listen 80 default_server;
    listen [::]:80 default_server;
 
    root /var/www/html;
    index index.php index.html index.htm index.nginx-debian.html;
 
    server_name _;
 
    location / {
        try_files $uri $uri/ =404;
    }
 
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.2-fpm.sock;
    }
 
    location ~ /\.ht {
        deny all;
    }
}

修改完後儲存 並輸入以下指令檢查設定檔格式是否沒有錯誤

sudo nginx -t

如果顯示以下的文字就表示設定檔格式沒有錯誤

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

檢查沒有錯誤就輸入指令重新載入nginx設定

sudo systemctl reload nginx

接下來測試php是否能正常執行

輸入指令產生info.php檔案

sudo nano /var/www/html/info.php

而檔案會輸出

<?php
phpinfo();

儲存後輸入http://hostname/info.php
若顯示為

就是正常喔
接著把剛剛產生的php腳本刪除 輸入指令

sudo rm /var/www/html/info.php

就完成LEMP安裝囉


上一篇
學習NextCloud IOS APP 倒數第30篇 -前言Xcode環境架設
下一篇
學習NextCloud IOS APP 倒數第28篇 - 創建第一支專案與簡單的介紹
系列文
小菜逼學習IOS30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言