华为云之使用nginx源码制作RPM包【玩转华为云】

举报
江湖有缘 发表于 2023/06/24 15:03:24 2023/06/24
【摘要】 华为云之使用nginx源码制作RPM包【玩转华为云】

一、本次实践介绍

1.1 本次实践环境介绍

本次实践环境为华为云ECS服务器,操作系统版本为centos7.6.

1.2 本次实践目的

1.安装rpm-build软件包,编写SPEC配置文件,创建新的RPM软件包。
2.制作nginx的rpm软件包,并安装成功。

二、ECS云服务器介绍

  • 弹性云服务器 ECS

弹性云服务器(Elastic Cloud Server, ECS)是一种云上可随时自助获取、可弹性伸缩的计算服务,可帮助您打造安全、可靠、灵活、高效的应用环境。
image.png

三、环境准备工作

3.1 预置实验环境

预置实验环境约等待【2分钟】后预置成功。环境预置成功会创建好对应VPC,安全组和子网,并生成名称为ecs-name的弹性云服务器ECS。

image.png
image.png

3.2 查看预置环境信息

预置的 ECS 资源用户、密码信息可点击预置环境信息查看。

image.png

3.3 登录华为云

在实验环境中,打开浏览器可自动登录华为云,如果没有成功登录,则使用账号重新登录即可。

image.png
image.png

四、连接ECS服务器

4.1 进入ECS管理页面

  • 在服务器列表,找到ECS产品,点击进入ECS管理页面。

image.png

  • 检查ECS服务器状态,确保正常运行。

image.png

4.2 连接ECS服务器

  • 复制ECS服务器的弹性公网IP地址

image.png

  • 远程ssh连接ECS服务器,打开桌面xfce终端,按以下命令替换IP连接ECS。
ssh root@EIP

image.png

4.3 查看操作系统版本

检查服务器的操作系统版本

[root@ecs-name ~]# cat /etc/centos-release
CentOS Linux release 7.6.1810 (Core) 

五、安装rpm-build软件

5.1 安装rpm-build软件包

直接使用yum安装rpm-build软件包

yum -y install  rpm-build

image.png

5.2 生成rpmbuild目录结构

  • 使用以下命令,生成rpmbuild目录结构。

rpmbuild -ba nginx.spec
  • 生成时会有报错,没有文件或目录,通过查看目录会自动生成目录结构。
[root@ecs-name ~]# ls /root/rpmbuild
BUILD  BUILDROOT  RPMS  SOURCES  SPECS  SRPMS

5.3 将源码软件复制到SOURCES目录

将源码软件复制到SOURCES目录

wget https://repo.huaweicloud.com/nginx/nginx-1.12.2.tar.gz

image.png

cp nginx-1.12.2.tar.gz /root/rpmbuild/SOURCES/

5.4 创建并修改SPEC配置文件

SPEC文件内容解释

image.png
image.png
image.png

vim /root/rpmbuild/SPECS/nginx.spec

Name:nginx                                       
Version:1.12.2                                   
Release:    10                                        
Summary: Nginx is a web server software.           
License:GPL                                        
URL:    https://repo.huaweicloud.com/nginx/        
Source0:nginx-1.12.2.tar.gz                       
#BuildRequires:                                    
#Requires:                                        
%description
nginx [engine x] is an HTTP and reverse proxy server.   
%post
useradd nginx                              
%prep
%setup -q                               
%build
./configure
make %{?_smp_mflags}
%install
make install DESTDIR=%{buildroot}
%files
%doc
/usr/local/nginx/*                   
%changelog

六、使用配置文件创建RPM包

6.1 安装依赖软件包

安装依赖软件包

yum -y install  gcc  pcre-devel openssl-devel

image.png

6.2 rpmbuild创建RPM软件包

rpmbuild创建RPM软件包

rpmbuild -ba /root/rpmbuild/SPECS/nginx.spec

image.png

6.3 查看rpm软件包

查看制作好的rpm软件包

[root@ecs-name ~]# ls /root/rpmbuild/RPMS/x86_64/nginx-1.12.2-10.x86_64.rpm
/root/rpmbuild/RPMS/x86_64/nginx-1.12.2-10.x86_64.rpm

七、安装nginx的rpm包

7.1 安装nginx

使用yum直接安装nginx的rpm包


yum install /root/rpmbuild/RPMS/x86_64/nginx-1.12.2-10.x86_64.rpm

image.png

7.2 检查已安装软件

检查已安装软件


[root@ecs-name ~]# rpm -qa |grep nginx
nginx-1.12.2-10.x86_64

7.3 查看nginx文件

查看nginx文件

[root@ecs-name ~]# rpm -ql nginx 
/usr/local/nginx/conf
/usr/local/nginx/conf/fastcgi.conf
/usr/local/nginx/conf/fastcgi.conf.default
/usr/local/nginx/conf/fastcgi_params
/usr/local/nginx/conf/fastcgi_params.default
/usr/local/nginx/conf/koi-utf
/usr/local/nginx/conf/koi-win
/usr/local/nginx/conf/mime.types
/usr/local/nginx/conf/mime.types.default
/usr/local/nginx/conf/nginx.conf
/usr/local/nginx/conf/nginx.conf.default
/usr/local/nginx/conf/scgi_params
/usr/local/nginx/conf/scgi_params.default
/usr/local/nginx/conf/uwsgi_params
/usr/local/nginx/conf/uwsgi_params.default
/usr/local/nginx/conf/win-utf
/usr/local/nginx/html
/usr/local/nginx/html/50x.html
/usr/local/nginx/html/index.html
/usr/local/nginx/logs
/usr/local/nginx/sbin
/usr/local/nginx/sbin/nginx


7.4 查看nginx命令帮助

  • 查看nginx命令

[root@ecs-name ~]# /usr/local/nginx/sbin/nginx -h
nginx version: nginx/1.12.2
Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]

Options:
  -?,-h         : this help
  -v            : show version and exit
  -V            : show version and configure options then exit
  -t            : test configuration and exit
  -T            : test configuration, dump it and exit
  -q            : suppress non-error messages during configuration testing
  -s signal     : send signal to a master process: stop, quit, reopen, reload
  -p prefix     : set prefix path (default: /usr/local/nginx/)
  -c filename   : set configuration file (default: conf/nginx.conf)
  -g directives : set global directives out of configuration file


/usr/local/nginx/sbin/nginx     # 启动命令,启动nginx
/usr/local/nginx/sbin/nginx  -V        #显示 nginx 的版本,编译器版本和配置参数
/usr/local/nginx/sbin/nginx -s stop/quit    #停止退出服务
/usr/local/nginx/sbin/nginx  -t    #检查配置文件
 /usr/local/nginx/sbin/nginx -s reload   # 重新加载配置文件

7.5 查看nginx版本

查看nginx版本

[root@ecs-name ~]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.12.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) 
configure arguments:

八、测试nginx服务

8.1 启动nginx服务

  • 启动nginx服务
 /usr/local/nginx/sbin/nginx
  • 检查nginx服务监听端口
[root@ecs-name ~]# ss -tunlp |grep 80
tcp    LISTEN     0      511       *:80                    *:*                   users:(("nginx",pid=10640,fd=6),("nginx",pid=10639,fd=6))

8.2 编辑测试index.html文件

编辑测试index.html文件

echo "ECS-nginx-config" > /usr/local/nginx/html/index.html

8.3 设置防护墙和selinux

关闭防火墙

systemctl disable firewalld && systemctl stop firewalld

关闭selinux

setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
getenforce 

8.4 安全组放行端口

安全组放行80端口

image.png

8.5 重载nginx服务

重载nginx服务

 /usr/local/nginx/sbin/nginx -s reload

8.6 访问nginx服务

在本机访问ECS云服务器的nginx服务,访问地址为ECS的弹性公网IP
http://124.70.70.239/

image.png

【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。