树莓派(RPi) CentOS7安装Nginx

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_36731677/article/details/78762902

前一段时间用树莓派折腾了一个非常复杂的嵌入式系统,现在树莓派已经闲下来了。那么贵的设备怎么能闲着吃灰呢?我把它放在保护盒里插好线,计划使其变成一个小型的网页服务器。
现在服务器已经基本成型,我就把我的配置过程分享一下。
##0.环境说明
设备:Raspberry Pi 3b
网关:NETGEAR R6220
接入方式:有线接入
软件版本:nginx-1.12.2
##1.烧录系统
首先要在CentOS官方下载镜像,传送门:http://mirror.centos.org/altarch/7/isos/armhfp/
因为我是树莓派3b,所以我下载:http://mirror.centos.org/altarch/7/isos/armhfp/CentOS-Userland-7-armv7hl-Minimal-1708-RaspberryPi3.img.xz
下载完成后使用Win32DiskImager.exe烧录在TF卡中(一个小小的提示,TF卡的质量不过关可能会导致在写入的时候速度越来越慢,以至于用了好几个小时都无法写入)
在系统写入之后就可以将其插入树莓派开机了

##2.扩展TF卡空间
(该步不为必要操作,可跳过)
安利我自己的文章
树莓派 CentOS7扩展内存卡剩余空间 http://blog.csdn.net/qq_36731677/article/details/78764941
##3.安装依赖
可能有过nginx安装经验的人会奇怪为什么不用yum来安装,因为nginx在树莓派上由于一些我不懂的原因无法安装,即使我替换了阿里的源也不行,所以我们来手动编译安装吧。
安装依赖包
yum install -y gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel
这几个依赖也可以分开安装

yum install -y gcc-c++ 
yum install -y pcre pcre-devel 
yum install -y zlib zlib-devel 
yum install -y openssl openssl-devel

##4.下载安装包
nginx官方网站:https://nginx.org/en/download.html
下载稳定版:wget https://nginx.org/download/nginx-1.12.2.tar.gz
解压压缩包:tar -zxvf nginx-1.12.2.tar.gz
进入文件夹:cd nginx-1.12.2
编译安装:

./configure
make && make install

如果需要http2功能,需要在./configure时添加参数--with-http_v2_module
如果需要ssl功能,需要在./configure时添加参数

--prefix=/usr/local/nginx \
--with-http_stub_status_module \
--with-http_ssl_module

##5.手动开启nginx
打开安装目录:cd /usr/local/nginx/sbin/

./nginx    			#启动nginx
./nginx -s stop		#停止nginx
./nginx -s quit		#终止nginx
./nginx -s reload	#重启nginx

##6.开启nginx.service
按照以往的经验,yum安装的nginx是自带nginx.service的,但是自己编译安装的程序则没有这个服务,所以我现在就分享一下如何手动写入这个服务。
在终端上操作:

cd /usr/lib/systemd/system
touch nginx.service
vim nginx.service

在nginx.service中写入以下内容(可能需要根据情况修改)

[Unit]
Description=nginx
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true

[Install]
WantedBy=multi-user.target

然后就可以使用nginx.service相关的命令了

systemctl start nginx.service
systemctl status nginx.service
systemctl enable nginx.service

猜你喜欢

转载自blog.csdn.net/qq_36731677/article/details/78762902