1. 程式人生 > >Ubuntu Server 16.04.1 LTS 64位配置nginx、mysql、php7.2

Ubuntu Server 16.04.1 LTS 64位配置nginx、mysql、php7.2

配置源,更新一下

sudo apt-get install software-properties-common python-software-properties

sudo add-apt-repository ppa:ondrej/php && sudo apt-get update

跑這個命令時會出現 `123` 選擇,直接回車,之後就會報編碼錯誤,執行下面命令
sudo LC_ALL=C.UTF-8 add-apt-repository ppa:ondrej/php && sudo apt-get update
遇到`123`繼續回車

接下來再次更新配置源

sudo apt-get update

安裝Nginx

sudo apt-get install nginx

切換管理員

sudo su

安裝MySQL客戶端

apt-get install mysql-server mysql-client

中途彈出框需設定MySQL密碼,重複輸入兩遍即可
安裝php7.2

apt-get install php7.2-fpm

安裝php7.2所需擴充套件

apt-get install php7.2-cli php7.2-mysql php7.2-gd php7.2-curl php7.2-xml php7.2-mbstring

三、正式部署專案
(1). 配置Nginx支援PHP

cd /etc/nginx/sites-available
cp default php
vim php

server {
     #注意,一臺伺服器,只能有一個default_server!!!其他專案配置檔案,直接將default_server這個單詞刪掉即可。
     listen 80;
     #listen [::]:80 default_server;

     #自己的專案路徑
      root /var/www/cms;

      # Add index.php to the list if you are using PHP
      index index.html index.htm index.php index.nginx-debian.html;
      #改成自己的真實域名,如果不改表示本機(localhost)
      server_name _;

      #根據框架需求,配置url重寫
      location / {
              try_files $uri $uri/ =404;
      }

      # pass the PHP scripts to FastCGI server listening 
      location ~ \.php$ {
              include snippets/fastcgi-php.conf;
      #
      #       # With php7.0-cgi alone:
      #       fastcgi_pass 127.0.0.1:9000;
      #       # With php7.0-fpm:
              fastcgi_pass unix:/run/php/php7.2-fpm.sock;
      }
}

(2). 使Nginx配置檔案生效

cd ..
cd sites-enabled
rm default
ln -s /etc/nginx/sites-available/php /etc/nginx/sites-enabled/php

重啟Nginx服務並檢視是否有問題

nginx -t                  #這個命令會告訴你Nginx配置檔案是否配置錯誤
service nginx restart     #重啟Nginx服務

(3). 建立對應的專案資料夾

mkdir /var/www/cms

(4). 新建檔案index.php,並輸出

cd /var/www/cms
vim index.php
<?php
echo "123";

裝完後,訪問http://127.0.0.1/index.php,看看是否已經裝上了。

四、修改許可權,上傳專案

cd /var/www
chmod -R 777 cms

專案上傳完成後,可能還需要根據需求,重新修改資料夾許可權 chmod -R 777 cms

五、不同框架中的Nginx配置
ThinkPHP
如果你用的ThinkPHP,請將URL_MODEL設定為2,REWRITE模式
修改這個配置檔案

location / {
if (!-e $request_filename) {
   rewrite  ^(.*)$  /index.php?s=$1  last;
   break;
    }
 }

Laravel
如果你用的是Laravel

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