Raspberry Pi 树莓派笔记

791 阅读4分钟
环境:
主机:Raspberry Pi B型(第一代国产红版)
系统:2018-11-13-raspbian-stretch
     (RaspberryPi-9 9.4 armv6l)

由于试了几个新版本,发现我的 USB网卡驱动有点问题,而且我的板子老了,所以选个旧点的系统

apt-get 配置国内镜像

镜像列表: www.raspbian.org/RaspbianMir…

清华大学镜像使用说明:mirrors.tuna.tsinghua.edu.cn/help/raspbi…

阿里云镜像使用说明:developer.aliyun.com/mirror/rasp…

# 修改“/etc/apt/sources.list”内容,将原内容注释掉,加入以下内容
deb http://mirrors.tuna.tsinghua.edu.cn/raspbian/raspbian/ stretch main non-free contrib rpi
deb-src http://mirrors.tuna.tsinghua.edu.cn/raspbian/raspbian/ stretch main non-free contrib rpi

# 这里以清华镜像为例,我在清华给出的内容上,加上‘rpi’,与原内容保存一致,而且清华的镜像有‘rpi’文件夹的
# 修改“/etc/apt/sources.list.d/raspi.list”内容,将原内容注释掉,并加入以下内容
deb http://mirrors.tuna.tsinghua.edu.cn/raspberrypi/ stretch main ui

修改完/etc/apt/sources.list/etc/apt/sources.list.d/raspi.list后,执行sudo apt-get update命令,更新软件源列表

配置root密码

有时候可能需要用到root权限,进行一些操作,而新安装的系统root是默认密码的,需要配置了密码才能使用root

sudo passwd root
## 然后输入当前用户的密码,Raspberry Pi默认用户是pi,默认密码是raspberry
## 在提示语Enter new UNIX password:后输入root的密码
## Retype new UNIX password:重复输入root密码
## passwd: password updated successfully 说明修改成功

# 将用户切换到root
su root

安装nginx和PHP

参考官方文档来安装nginx和PHP

  1. 安装nginx
# 首先更新一下软件包
sudo apt update
# 然后先安装nginx
sudo apt install nginx
# 安装完成后启动 nginx
## 使用方法 Usage: nginx {start|stop|restart|reload|force-reload|status|configtest|rotate|upgrade}
sudo /etc/init.d/nginx start
# 查看设备的ip地址
hostname -I

完成安装后,本地可以通过浏览器打开http://localhost/,同一网络下的其他设备可以通过浏览器打开设备IP地址访问到Welcome to nginx的网页

nginx 默认的web文件存放在/var/www/html,可以修改或替换/var/www/html/index.nginx-debian.html,这是Welcome to nginx的网页内容

自定义配置nginx服务器,可以通过修改/etc/nginx/sites-available/defaultnginx的默认配置进行自定义

这些修改需要root权限

  1. 安装PHP

现在的系统安装PHP比以前初代的时候方便很多了

# 安装php
sudo apt install php-fpm

# 编辑nginx服务器配置,使其支持php
cd /etc/nginx
sudo nano sites-enabled/default

## 找到下面👇这行
index index.html index.htm index.nginx-debian.html;
## 修改成👇下面这样,即增加 index.php
index index.php index.html index.htm index.nginx-debian.html;

## 开启PHP支持,找到下面👇这段代码

	# pass PHP scripts to FastCGI server
	#
	# 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;
	#}
	
## 改成下面👇这样的

	# pass PHP scripts to FastCGI server
	#
	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;
	}
	
## 注意别漏了'}'

# 重启nginx服务器,使php配置生效
sudo /etc/init.d/nginx reload

# 在/var/www/html新建一个index.php
cd /var/www/html/
sudo nano index.php

## index.php 里写入下面这句👇,保存就ok了
<?php echo phpinfo(); ?>

然后通过浏览器打开http://localhost/index.php或者其他同一网络的设备通过浏览器打开http://ip_address/index.php(ip_address=树莓派的ip地址)

如果看到下图的php的版本信息就成功开启PHP服务了

注意,要检查php服务是否启动,如果php服务未启动也无法看到正常信息

# 检查php状态(版本根据自己的填写,或者用tab补全)
sudo /etc/init.d/php7.0-fpm status
  1. 开机启动nginx和php服务

虽然nginx和php都安装配置好了,但是如果设备重启了,还是需要手工将nginx和php服务启动。这时候,就需要利用systemctl来配置开机启动服务了

# 先把用户切换到root
# 开机启动nginx
sudo systemctl enable nginx

# 开机启动php
sudo systemctl enable php7.0-fpm

## 如果都没有报错就成功了,可以重启设备看看是否能开机启动nginx和php服务

安装Tomcat

因为2018-11-13-raspbian-stretch自带有java,所以系统本身就有jdk环境了,可以直接安装Tomcat,不然要先安装jdk环境。

# 查看Java版本
java -version
## java version "1.8.0_65"
## Java(TM) SE Runtime Environment (build 1.8.0_65-b17)
## Java HotSpot(TM) Client VM (build 25.65-b01, mixed mode) 
# 更新apt-get源
sudo apt-get update
# 升级软件包
sudo apt-get upgrade
# 安装Tomcat
sudo apt-get install tomcat8
# 检查Tomcat状态
sudo /etc/init.d/tomcat8 status
# 如果Tomcat未启动,通过👇这命令启动Tomcat
sudo /etc/init.d/tomcat8 start

安装好后,就可以通过http://localhost:8080或者其他同一网络的设备通过浏览器打开http://ip_address:8080(ip_address=树莓派的ip地址) 查看

看到这个👇页面就说明Tomcat成功安装并运行了

如果Tomcat的默认端口是8080,如果被占,可以关闭占用的服务或者修改Tomcat的端口

先查看一下端口情况

# 检查端口占用情况,需要用sudo才有权限显示完整信息
sudo netstat -tunlp | grep 8080
# 也可以用lsof查看端口占用情况
## 由于系统本身没有lsof,需要安装
sudo apt-get install lsof
## 用lsof查看端口
sudo lsof -i:8080

修改Tomcat的端口(由于有其他服务需要在8080上运行,所以选择修改Tomcat的端口)

# 修改Tomcat的配置
sudo nano /etc/tomcat8/server.xml
## 找到以下代码
    <Connector port="8080" protocol="HTTP/1.1"
              connectionTimeout="20000"
               redirectPort="8443" />
## 将 port="8080",改成其他端口号,例如改成 port="8888"

# 重启Tomcat使配置生效
sudo /etc/init.d/tomcat8 restart