在 Ubuntu 16.04上安裝 Nginx+MySQL+ PHP
完全解除安裝apache2
1.
sudo apt update
2.
sudo apt upgrade
3.
sudo apt remove apache2*
4.
sudo apt autoremove
安裝Nginx
1.
sudo apt install nginx
2.
sudo service nginx start
測試Nginx,首先使用如下命令獲取本機IP地址
ifconfig
使用瀏覽器訪問本地IP。
返回如下頁面,則安裝Nginx成功。
安裝MySQL
sudo apt install mysql-server
安裝過程中需要輸入兩次密碼。
安裝MySQL安全元件
sudo mysql_secure_installation
安裝過程中首先需要輸入MYSQL的root密碼.
當詢問“Change the root password?”時,輸入“N”
剩下的問題可以敲擊回車鍵,選擇預設選項。
安裝PHP
sudo apt install php php-fpm php7.0-mysql
為使Nginx支援PHP,需要修改Nginx的配置檔案,首先備份原始配置檔案。
sudo mv /etc/nginx/sites-available/default /etc/nginx/sites-available/default.old
編輯/etc/nginx/sites-available檔案,作為Nginx的配置檔案。
sudo vim /etc/nginx/sites-available/default
向/etc/nginx/sites-available
輸入以下內容
server {
listen 80;
server_name your_site_name.com;
root /usr/share/nginx/html;
index index.php index.html;
location / {
try_files $uri $uri/ =404;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /var/www/html;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
修改完Nginx配置,需要重啟Nginx
sudo service nginx restart
在/usr/share/nginx/html/目錄建立info.php檔案,測試PHP
sudo nano /usr/share/nginx/html/info.php
輸入以下內容
<?php
phpinfo();
?>
在瀏覽器開啟http://本地IP/info.php
返回如下內容,則Nginx能夠支援PHP
最後,可以選擇刪除建立的info.php檔案,避免被攻擊者利用。
配置自己多域名、多專案
在 /etc/nginx/conf.d/
建立 .conf
檔案 在這裡配置自己的域名
server {
listen 80;
server_name you_project_name;
root /Data/; #專案的路徑
index index.php index.html;
location / {
# try_files $uri $uri/ =404; #將這個註釋掉
try_files $uri $uri/ /index.php?$query_string; #新增上這一條
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /var/www/html;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}