Linux-源码安装及FPM打包

目录

源码安装

这里举例Nginx的源码安装,需要前往Nginx官网找到稳定版本源码安装包下载。

## 源码安装nginx
# 0.安装依赖
[root@wqh nginx-1.16.1]# yum install -y gcc gcc-c++ glibc zlib-devel pcre-devel openssl-devel

# 1.下载nginx源码包
[root@wqh ~]# wget http://nginx.org/download/nginx-1.16.1.tar.gz

# 2.解压
[root@wqh ~]# tar xf nginx-1.16.1.tar.gz 
[root@wqh ~]# cd nginx-1.16.1

# 3.生成,--prefix 即安装路径
[root@wqh nginx-1.16.1]# useradd nginx -s /sbin/nologin -M
[root@wqh nginx-1.16.1]# ./configure --prefix=/app/nginx-1.16.1 --user=nginx --group=nginx

# 4.编译
[root@wqh nginx-1.16.1]# make

# 5.安装
[root@wqh nginx-1.16.1]# make install

# 6.检测配置文件有没有语法错误
[root@wqh sbin]# /app/nginx-1.16.1/sbin/nginx -t
nginx: the configuration file /app/nginx-1.16.1/conf/nginx.conf syntax is ok
nginx: configuration file /app/nginx-1.16.1/conf/nginx.conf test is successful

# 7.启动nginx
[root@wqh sbin]# /app/nginx-1.16.1/sbin/nginx

# 8.检测80端口
[root@wqh sbin]# netstat -lntup|grep 80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      13468/nginx: master

# 9.做软连接
[root@wqh nginx-1.16.1]# ln -s /app/nginx-1.16.1 /app/nginx

制作RPM包(使用FPM工具)

FPM是一个该死的软件包管理器,主要是用来构建软件包(deb,rpm等等)。使用fpm命令需要一些依赖,

主要是rubyrubygemsruby-devel,如果我们构建的是rpm包,那还需要rpm-build这个依赖包。

FPM的工具在国内难以下载,我在博客的后台暂存了一份fpm工具包,点击下载即可(FPM工具包)

# 先了解一下fpm命令
fpm:制作rpm包命令
    -s:dir     # 打目录
    -t:rpm     # 把目录打成rpm包
    -n:nginx   # 软件名字叫nginx
    -v:1.16.1  # 软件的版本号
    -d:        # 指定nginx的依赖包
    -f:        # 指定要达成rpm包的目录路径
    --post-install # 指定rpm包安装完成之后要执行的脚本
    --pre-install  # 指定rpm包安装之前,要执行的脚本
## 制作rpm包(用fpm 工具)

[root@wqh ~]# mkdir fpm
[root@wqh ~]# mv fpm-1.3.3.x86_64.tar.gz fpm
[root@wqh fpm]# cd /root/fpm/

# 1. 解压
[root@wqh fpm]# tar xf fpm-1.3.3.x86_64.tar.gz 
# 2.安装ruby
[root@wqh fpm]# yum -y install ruby rubygems ruby-devel rpm-build       # 都是gem命令的依赖包,rpm-build是制作rpm包的依赖包
# 3.查看gem的源
[root@wqh fpm]# gem sources --list
*** CURRENT SOURCES ***

https://rubygems.org/

# 4.更换阿里云的源 先移除国外源
[root@wqh fpm]# gem sources --remove https://rubygems.org/

# 5.更换阿里云的源, 添加阿里云的源
[root@wqh fpm]# gem sources -a https://mirrors.aliyun.com/rubygems/

# 6.使用gem命令安装当前目录下所有的.gem文件
[root@wqh fpm]# gem install *.gem

# 7.写出安装rpm之后要执行的脚本,此处为简略版本,后面有详细版本
[root@wqh ~]# vi /root/nginx.sh
#!/bin/bash
useradd nginx -s /sbin/nologin -M
ln -s /app/nginx-1.16.1 /app/nginx

# 8.使用fpm打包
[root@wqh fpm]# fpm -s dir -t rpm -n nginx -v 1.16.1 -d 'zlib-devel,gcc,glibc,pcre-devel,glibc,openssl-devel,gcc-c++' --post-install /root/nginx.sh -f /app/nginx-1.16.1/

安装rpm后要执行的脚本(优化版)

[root@wqh ~]# vi nginx.sh 

#!bin/bash
## 判断nginx用户是否存在,若不存在则创建,若存在则提示已经创建
id nginx &>/dev/null
if [ $? -ne 0 ];then
    useradd nginx -r -M -s /sbin/nologin
    echo "creating user nginx..."
else
    echo "user nginx has been created,nothing to do."
fi
## 创建目录 nginx-1.16.1 的软链接,方便使用
ln -s /app/nginx-1.16.1 /app/nginx &>/dev/null
## 创建system文件,以便于对nginx使用systemctl命令管理
cat > /usr/lib/systemd/system/nginx.service << eof
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/app/nginx/logs/nginx.pid
# Nginx will fail to start if /run/nginx.pid already exists but has the wrong
# SELinux context. This might happen when running `nginx -t` from the cmdline.
# https://bugzilla.redhat.com/show_bug.cgi?id=1268621
ExecStartPre=/usr/bin/rm -f /app/nginx/logs/nginx.pid
ExecStartPre=/app/nginx/sbin/nginx -t
ExecStart=/app/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true

[Install]
WantedBy=multi-user.target
eof
## 添加环境变量,以便于用nginx执行文件启动nginx,安装完需要source一下或者重新登录
cat > /etc/profile.d/nginx.sh << eof
export PATH="/app/nginx/sbin/:$PATH"
eof