how to create RPM and YUM repository


Reference:
http://www.ibm.com/developerworks/cn/linux/management/package/rpm/part1/
http://www.ibm.com/developerworks/cn/linux/management/package/rpm/part2/

Goal:
Make a RPM package for compiled nginx 1.1.9 with pcre 8.30 and zlib 1.2.6
Crate a private repository to manage the RPM packages

1. Create a softlink for my home
$ cd /home
$ sudo ln -s dong dong.guo

2. Install some compilation tools
$ sudo yum install gcc gcc-c++ autoconf automake zlib libxml ncurses-devel libgcrypt libtool

3. Install the RPM build tools
$ sudo yum install rpm-build rpmdevtools

4. Configure the RPM build environment
$ cd /home/dong.guo
$ mkdir rpmbuild
$ cd rpmbuild
$ mkdir -pv {BUILD,BUILDROOT,RPMS,SOURCES,SPECS,SRPMS,TMP}

$ cd /home/dong.guo
$ sudo vim .rpmmacros
$ sudo cp .rpmmacros /root/

%_topdir /home/dong.guo/rpmbuild
%_tmppath /home/dong.guo/rpmbuild/TMP

5. Download the source packages for nginx-1.1.9
$ cd /home/dong.guo/rpmbuild/SOURCES
$ wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.30.tar.gz
$ wget http://sourceforge.net/projects/libpng/files/zlib/1.2.6/zlib-1.2.6.tar.gz/download
$ wget http://nginx.org/download/nginx-1.1.9.tar.gz

$ sudo vim nginx

#!/bin/sh
#
# nginx - this script starts and stops the nginx daemin
#
# chkconfig:   - 85 15
# description:  Nginx is an HTTP(S) server, HTTP(S) reverse \
#               proxy and IMAP/POP3 proxy server
# processname: nginx
# config:      /usr/local/nginx/conf/nginx.conf
# pidfile:     /usr/local/nginx/logs/nginx.pid

# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0

nginx="/opt/nginx/sbin/nginx"
prog=$(basename $nginx)

NGINX_CONF_FILE="/opt/nginx/conf/nginx.conf"

lockfile=/opt/nginx/lock/nginx.lock

start() {
    [ -x $nginx ] || exit 5
    [ -f $NGINX_CONF_FILE ] || exit 6
    echo -n $"Starting $prog: "
    daemon $nginx -c $NGINX_CONF_FILE
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}

stop() {
    echo -n $"Stopping $prog: "
    killproc $prog -QUIT
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}

restart() {
    configtest || return $?
    stop
    start
}

reload() {
    configtest || return $?
    echo -n $"Reloading $prog: "
    killproc $nginx -HUP
    RETVAL=$?
    echo
}

force_reload() {
    restart
}

configtest() {
  $nginx -t -c $NGINX_CONF_FILE
}

rh_status() {
    status $prog
}

rh_status_q() {
    rh_status >/dev/null 2>&1
}

case "$1" in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart|configtest)
        $1
        ;;
    reload)
        rh_status_q || exit 7
        $1
        ;;
    force-reload)
        force_reload
        ;;
    status)
        rh_status
        ;;
    condrestart|try-restart)
        rh_status_q || exit 0
            ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
        exit 2
esac

$ sudo chmod +x nginx

$ tree

├── BUILD
├── BUILDROOT
├── RPMS
├── SOURCES
│   ├── nginx
│   ├── nginx-1.1.9.tar.gz
│   ├── pcre-8.30.tar.gz
│   └── zlib-1.2.6.tar.gz
├── SPECS
├── SRPMS
└── TMP

6. Configure the SPEC file
$ cd /home/dong.guo/rpmbuild/SPECS
$ vim heylinux-nginx-1.1.9.spec

Name:           heylinux-nginx
Version:        1.1.9
Release:        el6
Summary:        Compiled nginx 1.1.9 with pcre 8.30 and zlib 1.2.6

Group:          System Environment/Daemons
License:        GPL
URL:            http://www.drawbrid.ge
Source0:        nginx-1.1.9.tar.gz
Source1:        pcre-8.30.tar.gz
Source2:        zlib-1.2.6.tar.gz

Vendor:         guosuiyu@gmail.com

BuildRequires:  gcc gcc-c++ autoconf automake openssl-devel

%description
This RPM package is made for compiled nginx 1.1.9, pcre 8.30 and zlib 1.2.6 are not installed,
they just provide the packages for nginx build process.

%prep
tar xzvf $RPM_SOURCE_DIR/pcre-8.30.tar.gz
tar xzvf $RPM_SOURCE_DIR/zlib-1.2.6.tar.gz
tar xzvf $RPM_SOURCE_DIR/nginx-1.1.9.tar.gz

%build
cd nginx-1.1.9
./configure --prefix=/opt/nginx \
--user=nginx \
--group=nginx \
--pid-path=/opt/nginx/run/nginx.pid \
--lock-path=/opt/nginx/lock/nginx.lock \
--with-http_ssl_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_realip_module \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--with-mail \
--with-mail_ssl_module \
--with-pcre=../pcre-8.30 \
--with-zlib=../zlib-1.2.6 \
--with-debug \
--http-client-body-temp-path=/opt/nginx/tmp/client \
--http-proxy-temp-path=/opt/nginx/tmp/proxy \
--http-fastcgi-temp-path=/opt/nginx/tmp/fastcgi \
--http-uwsgi-temp-path=/opt/nginx/tmp/uwsgi \
--http-scgi-temp-path=/opt/nginx/tmp/scgi

%install
rm -rf $RPM_BUILD_ROOT
cd nginx-1.1.9/
mkdir -p $RPM_BUILD_ROOT/opt/nginx/tmp
mkdir -p $RPM_BUILD_ROOT/opt/nginx/run
mkdir -p $RPM_BUILD_ROOT/opt/nginx/lock
make install DESTDIR=%{buildroot}

mkdir -p $RPM_BUILD_ROOT/etc/init.d
cp -p $RPM_SOURCE_DIR/nginx $RPM_BUILD_ROOT/etc/init.d/

%clean
rm -rf $RPM_BUILD_DIR/pcre-8.30
rm -rf $RPM_BUILD_DIR/zlib-1.2.6
rm -rf $RPM_BUILD_DIR/nginx-1.1.9

%preun

%postun
userdel nginx

%files
/opt/nginx
/etc/init.d/nginx

%post
useradd -M nginx -s /sbin/nologin

%changelog

7. Build the RPM package
$ sudo rpmbuild -bb heylinux-nginx-1.1.9.spec

8. Found the RPM package
$ cd /home/dong.guo/rpmbuild/RPMS/x86_64/

$ ls

heylinux-nginx-1.1.9-el6.x86_64.rpm

9. Copy the RPM package to another server
$ scp -i /home/dong.guo/.ssh/id_rsa heylinux-nginx-1.1.9-el6.x86_64.rpm dong.guo@172.16.1.21:/home/dong/

10. Login the server 172.16.1.21 and install the RPM package
$ sudo rpm -ivh heylinux-nginx-1.1.9-el6.x86_64.rpm

Preparing...                ########################################### [100%]
   1:heylinux-nginx       ########################################### [100%]

$ sudo /etc/init.d/nginx start

Starting nginx:                                            [  OK  ]

$ sudo netstat -lntp | grep 80

tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      13581/nginx

11. Crate a private YUM repository
$ sudo yum install httpd
$ sudo yum install createrepo
$ sudo mkdir -p /var/www/html/heylinux/RPMS/x86_64
$ cd /var/www/html/heylinux/RPMS/x86_64
$ sudo cp /home/dong.guo/rpmbuild/RPMS/x86_64/heylinux-nginx-1.1.9-el6.x86_64.rpm .
$ sudo createrepo /var/www/html/heylinux/RPMS/x86_64/
$ sudo /etc/init.d/httpd start

12. Configure the YUM source on another server
$ sudo vim heylinux.repo

[heylinux]
name=heylinux
baseurl=http://172.16.1.21/heylinux/RPMS/x86_64/
gpgcheck=0
enabled=1

$ sudo yum install heylinux-nginx

Loaded plugins: fastestmirror, security
Loading mirror speeds from cached hostfile
heylinux            |  951 B     00:00
heylinux/primary    |  978 B     00:00
heylinux                                                                                      1/1
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package heylinux-nginx.x86_64 0:1.1.9-el6 set to be updated
--> Finished Dependency Resolution

Dependencies Resolved

====================================================================================================
 Package                     Arch              Version                  Repository             Size
====================================================================================================
Installing:
 heylinux-nginx            x86_64            1.1.9-el6               heylinux            386 k

Transaction Summary
====================================================================================================
Install       1 Package(s)
Upgrade       0 Package(s)

Total download size: 386 k
Installed size: 940 k
Is this ok [y/N]: y
Downloading Packages:
heylinux-nginx-1.1.9-el6.x86_64.rpm      | 386 kB     00:00
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Installing: 
  heylinux-nginx-1.1.9-el6.x86_64    1/1

Installed:
  heylinux-nginx.x86_64 0:1.1.9-el6

Complete!

13. Done

,

  1. No comments yet.
(will not be published)
*


Fork me on GitHub