头像

LF112

从现在开始,我所期待的未来在哪里

《树莓派搭建Nginx+PHP环境》

 1年前  •   技术, 树莓派  •   , , , , ,  •   963  •   7

前言

很多树莓派玩家都可能会遇到搭建Nginx+php网页失败的情况,这里,给大家个教程! ̄▽ ̄

全程以root用户执行,为了方便嘛~_(:з」∠)_

cmdsudo passwd root

sudo passwd --unlock root

su root

⌈搭建Nginx环境⌋

安装

首先,先安装Nginx []~( ̄▽ ̄)~*

在终端输入以下命令

cmdapt-get install nginx

如有提示“你确定要安装吗[N/Y]”输入Y即可

启动

终端输入以下命令来启动Nginx服务

txt/etc/init.d/nginx start

 

配置

啊啊啊,请务必让我在这抱怨一翻:

Nginx配置,你个**,自带的“支持php的配置”去掉#后就直接报错嘛,Fuc…发财…发财…(和谐),害老子**搞了一个星期,今天老子终于攻略你了!开心吧!Fuc…发财…发财…(和谐)(;´д`)ゞ假装有图片->[图片]鱼哭了水知道,我哭了谁知道╮(╯﹏╰)╭

打开Nginx配置

在终端输入:

cmdnano /etc/nginx/sites-available/default 

这里强烈推荐nano编辑器,新手的福利啊!φ(>ω<*) 

打开配置后

推荐你把里面的内容删掉!写成我的,要不然你要配置半天!

你将看到以下内容:

txt##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# http://wiki.nginx.org/Pitfalls
# http://wiki.nginx.org/QuickStart
# http://wiki.nginx.org/Configuration
#
# Generally, you will want to move this file somewhere, and start with a clean
# file but keep this around for reference. Or just disable in sites-enabled.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##

# Default server configuration
#
server {
	listen 80 default_server;
	listen [::]:80 default_server;

	# SSL configuration
	#
	# listen 443 ssl default_server;
	# listen [::]:443 ssl default_server;
	#
	# Self signed certs generated by the ssl-cert package
	# Don't use them in a production server!
	#
	# include snippets/snakeoil.conf;

	root /var/www/html/;

	# Add index.php to the list if you are using PHP
	index index.html index.htm;

	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;
	}

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

	# deny access to .htaccess files, if Apache's document root
	# concurs with nginx's one
	#
	#location ~ /\.ht {
	#	deny all;
	#}
}


# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
#server {
#	listen 80;
#	listen [::]:80;
#
#	server_name example.com;
#
#	root /var/www/example.com;
#	index index.html;
#
#	location / {
#		try_files $uri $uri/ =404;
#	}
#}

这里的“listen 80 default_server;”是监听的端口号,推荐80端口!( ̄3 ̄)a 

这里的“root /var/www/html/;”存放网页的地方,所有网页丢到这里就OK啦~(=゚ω゚)ノ

这里的“server_name _;”是服务器名字,不推荐修改!

配置PHP支持

现在是重要环节了呢~ヾ(•ω•`。)

在下面的这个地方添加个 “index.php”33行处,如下

txt	# Add index.php to the list if you are using PHP
	index index.html index.htm;

PS:如果找不到,可以按下键盘的 “Ctrl+w” 再输入index.htm进行寻找╮( ̄▽ ̄)╭

添加后如下

txt	# Add index.php to the list if you are using PHP
	index index.php index.html index.htm;

然后找到 “location ~ \.php$ {“ 的那个部分,如下:

txt	#location ~ \.php$ {
	#	include snippets/fastcgi-php.conf;
	#
	#	# With php5-cgi alone:
	#	fastcgi_pass 127.0.0.1:9000;
	#	# With php5-fpm:
	#	fastcgi_pass unix:/var/run/php5-fpm.sock;
	#}

PS:如果找不到,可以按下键盘的 “Ctrl+w” 再输入location ~ \.php$ {进行寻找╮( ̄▽ ̄)╭

 重点:

去掉开头结尾的 #

“}”上一行写入以下内容

txt                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                include fastcgi_params;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

完成后如下:

txt	location ~ \.php$ {
	#	include snippets/fastcgi-php.conf;
	#
	#	# With php5-cgi alone:
	#	fastcgi_pass 127.0.0.1:9000;
	#	# With php5-fpm:
	#	fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                include fastcgi_params;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
	}

恭喜,Nginx配置完成!

这里赠送本博主的配置一份(`・ω・´)

txt##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# http://wiki.nginx.org/Pitfalls
# http://wiki.nginx.org/QuickStart
# http://wiki.nginx.org/Configuration
#
# Generally, you will want to move this file somewhere, and start with a clean
# file but keep this around for reference. Or just disable in sites-enabled.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##

# Default server configuration
#
server {
	listen 80 default_server;
	listen [::]:80 default_server;

	# SSL configuration
	#
	# listen 443 ssl default_server;
	# listen [::]:443 ssl default_server;
	#
	# Self signed certs generated by the ssl-cert package
	# Don't use them in a production server!
	#
	# include snippets/snakeoil.conf;

	root /var/www/html/;

	# Add index.php to the list if you are using PHP
	index index.php index.html index.htm;

	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;
	}

	# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
	#
	location ~ \.php$ {
	#	include snippets/fastcgi-php.conf;
	#
	#	# With php5-cgi alone:
	#	fastcgi_pass 127.0.0.1:9000;
	#	# With php5-fpm:
	#	fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                include fastcgi_params;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
	}

	# deny access to .htaccess files, if Apache's document root
	# concurs with nginx's one
	#
	#location ~ /\.ht {
	#	deny all;
	#}
}


# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
#server {
#	listen 80;
#	listen [::]:80;
#
#	server_name example.com;
#
#	root /var/www/example.com;
#	index index.html;
#
#	location / {
#		try_files $uri $uri/ =404;
#	}
#}

终端输入以下命令重载Nginx配置

txt/etc/init.d/nginx reload  

⌈搭建PHP环境⌋

安装

安装PHP ヾ(o・ω・)ノ

在终端输入以下命令

txtapt-get install php5-fpm php5-cgi php5-cli php5-curl php5-gd php5-mcrypt php5-mysql php5-memcache php5-gd php5-sqlite php5-cgi php5-xmlrpc
如有提示"你确定要安装吗[N/Y]"输入Y即可

搭建php环境完成!

⌈测试⌋

“/var/www/html/”下创建“index.php” ( • ̀ω•́ )✧

PS:创建之前先删除里面的文件!

什么?你是不是想跟我讲:我用ftp/vnc删除不了里面的文件啊!
在终端输入以下命令就可以哦!

txtcd /var/www

chmod -R 777 *

chown pi *

cd /var/www/html

chmod -R 777 *

chown pi *

 

输入以下命令来创建index.php文件

txtnano /var/www/html/index.php

创建之后会直接打开文件!

在里面写入以下内容

txt<?php
        phpinfo();
?>

输入完后,按下按 Ctrl+O –> 回车 –> Ctrl+X –>回车 保存退出

打开浏览器,输入树莓派ip访问网页,查看是否有php的配置信息

入有疑问或bug可以在下面的评论栏里提出!

上一篇:
下一篇:

 评论


 已有7条评论

  1. CSS_Sheep 冒泡 Windows 10 | 谷歌浏览器 64.0.3282.119 11月前

    看不懂…(ノ°ο°)ノ

  2. 大白 潜水 Windows 7 | 火狐浏览器 58.0 1年前

    看不懂看不懂⌇●﹏●⌇

  3. LF112 伏太 未知的操作系统 | 未知浏览器 1年前

    呀咩爹over呀咩爹

  4. LFTools 伏太 未知的操作系统 | 未知浏览器 1年前

    挺不错的文章~拿走惹!

    • LF112 伏太 BLN-AL10 Build/HONORBLN-AL10 | 谷歌浏览器 62.0.3202.84 9月前

      LFTools233

    • LF112 伏太 BLN-AL10 Build/HONORBLN-AL10 | 谷歌浏览器 62.0.3202.84 9月前

      LFToolstest

  5. LF112 伏太 未知的操作系统 | 未知浏览器 1年前

    出现问题就在这评论哦~