樹(shù)莓派搭建Web服務(wù)器(LMNP)

摘要:樹(shù)莓派上搭建LNMPWeb服務(wù)器。修改Ngnix配置文件(/etc/nginx/sites-available/default),使得可解析PHP文件。

圖片發(fā)自簡(jiǎn)書(shū)App

樹(shù)莓派LNMP搭建Web服務(wù)器網(wǎng)站
Linux + Nginx + MySQL + PHP

安裝軟件前,更新所有軟件

$sudo apt-get update  && sudo apt-get upgrade  

1.安裝Nginx

命令安裝

sudo apt-get install nginx
Nginx安裝過(guò)程圖

安裝完成,Nginx已經(jīng)啟動(dòng)了,打開(kāi)瀏覽器輸入本機(jī)ip:看到下圖展示網(wǎng)頁(yè),說(shuō)明Nginx成功啟動(dòng)了!


192.168.1.100

默認(rèn)開(kāi)機(jī)啟動(dòng)Nginx,如果不想開(kāi)機(jī)啟動(dòng)Nginx,修改/etc/init.d/nginx文件

如啟動(dòng)失敗,如下命令啟動(dòng)Nginx服務(wù)

pi@xxxxxx:/etc/init.d$nginx start

查看Nginx安裝目錄(whereis nginx)

pi@xxxxxx:~$ whereis  nginx
nginx: /usr/sbin/nginx /etc/nginx /usr/share/nginx /usr/share/man/man1/nginx.1.gz

要搞懂幾個(gè)目錄及文件
/etc/nginx/ ##Nginx服務(wù)器配置文件目錄

pi@xxxxxx:/etc/nginx$ ls -lF
total 56
drwxr-xr-x 2 root root 4096 Oct 28  2016 conf.d/
-rw-r--r-- 1 root root 1034 Oct 28  2016 fastcgi.conf
-rw-r--r-- 1 root root  964 Oct 28  2016 fastcgi_params
-rw-r--r-- 1 root root 2837 Oct 28  2016 koi-utf
-rw-r--r-- 1 root root 2223 Oct 28  2016 koi-win
-rw-r--r-- 1 root root 3957 Oct 28  2016 mime.types
-rwxrwxrwx 1 root root 1491 Apr 30 15:04 nginx.conf*
-rw-r--r-- 1 root root  180 Oct 28  2016 proxy_params
-rw-r--r-- 1 root root  596 Oct 28  2016 scgi_params
drwxr-xr-x 2 root root 4096 Apr 18 14:26 sites-available/
drwxr-xr-x 2 root root 4096 Apr 18 09:26 sites-enabled/
drwxr-xr-x 2 root root 4096 Apr 18 09:26 snippets/
-rw-r--r-- 1 root root  623 Oct 28  2016 uwsgi_params
-rw-r--r-- 1 root root 3071 Oct 28  2016 win-utf

/usr/share/nginx 用戶(hù)安裝軟件的目錄

查看跟nginx關(guān)聯(lián)的文件列表(dpkg -L nginx)

pi@xxxxxx:/usr/orayapp$ dpkg -L nginx
/.
/usr
/usr/share
/usr/share/doc
/usr/share/doc/nginx
/usr/share/doc/nginx/copyright
/usr/share/doc/nginx/changelog.gz
/usr/share/doc/nginx/changelog.Debian.gz

1./etc/nginx/ #nginx配置文件目錄
/etc/nginx/nginx.conf #nginx配置文件
/etc/nginx/sites-available/default #nginx網(wǎng)站配置文件
2./etc/init.d/nginx #啟動(dòng)/關(guān)閉/重啟 ngnix服務(wù)
3./usr/share/nginx/html/ #nginx默認(rèn)Web目錄
/usr/share/nginx/html/index.html
4./var/www/html/ #通過(guò)配置文件修改為ngnix的Web目錄
/var/www/html/index.nginx-debian.html

修改Nginx Web站點(diǎn)目錄路徑

Nginx的默認(rèn)Web站點(diǎn)目錄為 /usr/share/nginx/html

pi@xiaoxiao:~d$ ls /usr/share/nginx/html
index.html

打開(kāi)Nginx配置文件/etc/nginx/sites-available/default
修改為/var/www/html

server {
        listen 8080 default_server;
        root /usr/share/nginx/html;
        server_name _;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404;
        }
        location ~ \.php$ {
                root    /usr/share/nginx/html;
                try_files $uri = 404;
                include /etc/nginx/fastcgi_params;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                fastcgi_index index.php;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
               #fastcgi_pass 127.0.0.1:9000;
        }
}

查看Web站點(diǎn)目錄/var/www/html下的文件

pi@xiaoxiao:/var/www/html$ ls -l
total 172
-rw-r--r-- 1 root root 867 Apr 18 09:26 index.nginx-debian.html
-rw-r--r-- 1 root root 168373 Apr 18 11:45 live.jpg
pi@xiaoxiao:/var/www/html$

試著瀏覽live.jpg圖片看看

http://192.168.1.100/live.jpg

接下來(lái)這一步需要安裝完成PHP后驗(yàn)證
Nginx Web站點(diǎn)下創(chuàng)建PHP腳本文件:index-raspberry-pi.php

<! DOCTYPE html>
<mate charset="utf-8"/>
<tile>云創(chuàng)智能家庭</tile>
<body>
<p>歡迎回家</p>
<?php
"Hello, World!"
?>
<div></div>
</body>

瀏覽器訪問(wèn)該P(yáng)HP文件:http://192.168.1.100/index-raspberry-pi.php
結(jié)果瀏覽器并沒(méi)有解析php文件內(nèi)容,而是下載了文件。
配置Nginx
配置方式一:
教程:樹(shù)莓派LNMP開(kāi)Web服務(wù)器搭網(wǎng)站,可外網(wǎng)訪問(wèn)
中的方式配置方法:
打開(kāi)站點(diǎn)配置文件,進(jìn)行編輯:

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

然后按下i進(jìn)入編輯模式,在

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

然后按下ESC,再輸入:
:wq
按下Enter,退出了編輯器。重啟Nginx:

sudo /etc/init.d/nginx restart

重啟失敗,錯(cuò)誤提示

錯(cuò)誤信息

第一次接觸Ngnix,摸石頭過(guò)河,網(wǎng)上搜索了一個(gè)多小時(shí),無(wú)果。
在無(wú)望之時(shí),坐下來(lái)心平氣和的喝杯茶,心平靜下來(lái)后,還是得從配置的每一個(gè)選項(xiàng)開(kāi)始研究,不能知其然而不知其所以然,經(jīng)過(guò)研究,算是弄明白些,也配置成功了,瀏覽器可正確處理PHP代碼了,最終的配置文件內(nèi)容:

server {
        listen 8080 default_server;
        root /usr/share/nginx/html;
        server_name _;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404;
        }
        location ~ \.php$ {
                root    /usr/share/nginx/html;
                try_files $uri = 404;
                include /etc/nginx/fastcgi_params;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                fastcgi_index index.php;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
               #fastcgi_pass 127.0.0.1:9000;
        }
}

安裝PHP5后,再來(lái)瀏覽PHP文件。

2.安裝MySQL

MySQL是數(shù)據(jù)庫(kù)服務(wù)程序。直接安裝:

sudo apt-get install  mysql-server mysql-client

安裝過(guò)程中要求輸密碼,隨便設(shè)定,最好設(shè)個(gè)強(qiáng)密碼。這樣就裝好了。

設(shè)置密碼圖

3.安裝PHP5

PHP需要有一個(gè)與Nginx通信的模塊

pi[@xxxxxx]:~$ sudo apt-get install php5-fpm php5-cgi php5-cli php5-curl php5-gd php5-mcrypt php5-mysql

4.驗(yàn)證Web服務(wù)站點(diǎn)搭建成功

啟動(dòng)Nginx服務(wù)

sudo /etc/init.d/nginx restart

瀏覽HTML文件--index-raspberry-pi.html

<! DOCTYPE html>
<mate charset="utf-8"/>
<tile>云創(chuàng)智能家庭</tile>
<body>
<h1>歡迎訪問(wèn)云創(chuàng)智能家庭</h1>
<div></div>
</body>
HTML文件

瀏覽PHP文件--index.php

<! DOCTYPE html>
<mate charset="utf-8"/>
<tile>云創(chuàng)智能家庭</tile>
<body>
<h1>歡迎訪問(wèn)云創(chuàng)智能家庭</h1>
<?php
"Hello, World!"
?>
<div></div>
</body>
PHP文件

到此,樹(shù)莓派上搭建Web站點(diǎn)結(jié)束,有不清楚的地方歡迎留言交流!?。?/p>

樹(shù)莓派LNMP開(kāi)Web服務(wù)器搭網(wǎng)站,可外網(wǎng)訪問(wèn)
將樹(shù)莓派變成一個(gè)Web服務(wù)器

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 樹(shù)莓派 網(wǎng)絡(luò)安全 DVWA WooYun-DVWA bWAPP WackoPicko sqli-labs ZVu...
    Gibbs基閱讀 3,290評(píng)論 0 8
  • Spring Cloud為開(kāi)發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見(jiàn)模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,555評(píng)論 19 139
  • 《老男孩Linux運(yùn)維》筆記 隱藏Nginx軟件版本號(hào) 一般來(lái)說(shuō),軟件的漏洞都和版本有關(guān)。因此要盡量隱藏對(duì)訪問(wèn)用戶(hù)...
    Zhang21閱讀 3,890評(píng)論 0 28
  • 身邊總有這樣一種人。 你考好了,她會(huì)說(shuō),哎不就是跟老師關(guān)系好嗎?我巴結(jié)巴結(jié)老師我也是門(mén)門(mén)優(yōu)秀。你考差了,她會(huì)說(shuō),哎...
    布蘭妮夫人閱讀 1,001評(píng)論 0 0
  • 蒸羊羔,蒸熊掌,蒸鹿尾兒 燒花鴨,燒雛雞兒,燒子鵝 鹵煮咸鴨,醬雞,臘肉,松花,小肚兒 晾肉,香腸,什錦蘇盤(pán) 熏雞...
    他瞎了她的夏閱讀 175評(píng)論 0 0

友情鏈接更多精彩內(nèi)容