安装nginx 服务器和php7

1) 首先需要安装需要安装 nginx

sudo apt-get install nginx
sudo apt-get install php7.0 php7.0-fpm php7.0-mysql php7.0-common
sudo apt-get install mysql-server mysql-client

sudo systemctl restart php7.0-fpm

2) 接下来我们来配置nginx,首先打开配置文件,sudo nano /etc/nginx/nginx.conf ,按照下面的配置进行修改。

user www-data; #默认以www-data运行工作进程
worker_processes 1; #单工作进程足够了,就我自己访问
worker_connections 256; #一般支持100在线连接就达到raspberry pi的极限了

3) 配置站点属性

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

如果发现显示的是空文件,可以通过以下命令找到这个文件

whereis nginx
# 找到nginx安装位置,一般在etc下
cd /etc/nginx
ls
#找到sites-available文件夹
cd sites-available
ls
#就可以看到default

找的网站的配置文件

server {
listen 887; # 访问端口号
#listen [::]:80 default_server; # 一定要注释掉,ipv6
server_name pi.com;
root /var/www/html; #站点存储位置
#nginx 默认路径html所在路径
index index.html index.htm index.nginx-debian.html index.php;
#修改php配置
       location ~ \.php$ {
        #       include snippets/fastcgi-php.conf;
        #
        #       # With php-fpm (or other unix sockets):
                fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
        #       # With php-cgi (or other tcp sockets):
        #       fastcgi_pass 127.0.0.1:9000;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;
        }

4) 测试配置文件看有没有问题

sudo nginx -t

如果没有问题会显示

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

5) 创建目录,然后设置权限

sudo mkdir /var/www
sudo mkdir /var/www/html
sudo chown -R www-data:www-data /var/www/html
sudo chmod -R 0755 /var/www/html

cd /var/www/html
#添加pi的www文件夹权限,以后可以使用ftp软件上传文件
sudo chown -R www-data pi-dashboard

6) 启动 nginx

sudo systemctl restart nginx

7) 测试
访问 http://192.168.1.10:887 看是否有网页

8) 当然你也可以放一个图形界面,监控树莓派运行状态(可选)

sudo apt install git
cd /var/www/html
sudo git clone https://github.com/spoonysonny/pi-dashboard.git
sudo mv pi-dashboard status
用浏览器访问http://192.168.1.10/status 可看到如下内容
image.png
Last modification:October 2, 2018
如果觉得文章对你有用,请随意赞赏