---
# System prepended metadata

title: 主機環境架設指令
tags: [系統安裝]

---

###### tags: `系統安裝`

# 主機環境架設指令

### 主機
* ubuntu 18.04
* nginx
* php 7.1

### 專案
* laravel 5.2

## 安裝環境

==#更新==
> sudo apt-get -y update

==#設定預設選項避免中斷==
> sudo DEBIAN_FRONTEND=noninteractive apt-get -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" upgrade

==#安裝伺服器==
> sudo apt install -y nginx

==#安裝套件==
> sudo apt-get install -y libbz2-dev

> sudo apt-get install -y libfreetype6-dev

> sudo apt-get install -y libjpeg-dev

> sudo apt-get install -y libpng-dev

> sudo apt-get install -y libssl-dev

> sudo apt-get install -y openssl

> sudo apt-get install -y curl

> sudo apt-get install -y git

> sudo apt-get install -y composer

==#add-apt-repository前需先安裝==
> sudo apt-get install -y software-properties-common

==#增加php套件來源==
> sudo add-apt-repository -y ppa:ondrej/php

==#安裝mongodb3.4 前加入key==
> sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 0C49F3730359A14518585931BC711F9BA15703C6

==#安裝mongodb3.4 前加入清單==
> echo "deb [ arch=amd64,arm64 ] http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.4.list

==#更新==
> sudo apt-get -y update

==#安裝mongodb3.4==
> sudo apt-get install -y mongodb-org

==#啟動mongod==
> sudo service mongod start

==#安裝php7.1以及專案需要的相關套件==
> sudo apt-get install -y php7.1 php7.1-fpm php7.1-cli php7.1-common php7.1-mongodb php7.1-gd php7.1-zip php7.1-opcache php7.1-xdebug php7.1-mbstring php7.1-xml php7.1-curl

==#指定php版本==
> sudo update-alternatives --set php /usr/bin/php7.1

==#更改memory_limit限制==
> sudo sed -i 's/memory_limit\s*=.*/memory_limit = 2048M/g' /etc/php/7.1/cli/php.ini;

> sudo sed -i 's/memory_limit\s*=.*/memory_limit = 2048M/g' /etc/php/7.1/fpm/php.ini;

==#修改nginx文件==
> sudo vi /etc/nginx/sites-available/default

```=
server {
        listen 80 default_server;
        server_name localhost;
        root /var/www/html;
        index index.php index.html;

        charset utf-8;

        location / {
                try_files $uri $uri/ /index.php?$query_string;
        }

        location ~ \.php$ {
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;
        }

        location ~ /\.ht {
                deny all;
        }
}
```
==#重啟nginx==
> sudo service nginx restart

==#重啟php7.1-fpm==
> sudo service php7.1-fpm restart

==#產生本地ssh key 將public key加入gitlab帳號==
> sudo ssh-keygen -o -t rsa -b 4096 -N "" -f ~/.ssh/id_rsa

==#加入gitlab key 需改成自己專案位置==
> sudo ssh-keyscan -t rsa git@git.com | sudo tee -a /root/.ssh/known_hosts

==#clone專案 需改成自己專案位置==
> cd /var/www/html

> sudo git clone git@git.com:test/project.git

==#進入專案 安裝專案所需套件==
> cd project/

> sudo composer install

==#建立.env==
> sudo cp .env.example .env

==#如果要跑測試更改DB_HOST_TEST==
> sudo sed -i 's/DB_HOST_TEST\s*=.*/DB_HOST_TEST=127.0.0.1/g' /var/www/html/project/.env;

==#建立專案key==
> sudo php artisan key:generate


---


## 參考指令

==#檢查nginx狀態==
> systemctl status nginx

==#測試nginx==
> sudo service nginx configtest

==#mongodb套件擴充==
> echo "extension=mongodb.so" | sudo tee -a /etc/php/7.1/cli/php.ini

> echo "extension=mongodb.so" | sudo tee -a /etc/php/7.1/fpm/php.ini

==#刪除特定金鑰==
> ssh-keygen -f "~/.ssh/known_hosts" -R 127.0.0.1

==#將public key加入gitlab帳號 需改成自己的帳號==
> sudo ssh-copy-id -i ~/.ssh/id_rsa.pub user@git.com

==#手動選擇調整php版本==
> sudo update-alternatives --config php

==#重啟mongod==
> sudo service mongod restart

==#檢查27017port是否能連線==
> nc -zvv localhost 27017