基于Docker环境中源码部署容器Nginx

时间:2022-04-28
本文章向大家介绍基于Docker环境中源码部署容器Nginx,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

docker容器中源码安装nginx:

 [root@465d13a11ffa  ~]# mkdir /Nginx-1.8/
 [root@465d13a11ffa  Nginx-1.8]# ls
 nginx-1.8.0  nginx-1.8.0.tar.gz
[root@465d13a11ffa nginx-1.8.0]# ./configure --prefix=/export/server/nginx  --with-http_ssl_module --with-http_stub_status_module
 checking for OS
  + Linux 2.6.32-504.el6.x86_64 x86_64
 checking for C compiler ... found
  + using GNU C compiler
  + gcc version: 4.8.3 20140911 (Red Hat 4.8.3-9) (GCC)
 checking for gcc -pipe switch ... found
 。。。。。。。
 Configuration summary
   + using system PCRE library
   + OpenSSL library is not used
   + using builtin md5 code
   + sha1 library is not found
   + using zlib library: /Nginx-1.8/nginx-1.8.0/auto/lib/zlib
 
   nginx path prefix: "/export/server/nginx"
   nginx binary file: "/export/server/nginx/sbin/nginx"
   nginx configuration prefix: "/export/server/nginx/conf"
   nginx configuration file: "/export/server/nginx/conf/nginx.conf"
   nginx pid file: "/export/server/nginx/logs/nginx.pid"
   nginx error log file: "/export/server/nginx/logs/error.log"
   nginx http access log file: "/export/server/nginx/logs/access.log"
   nginx http client request body temporary files: "client_body_temp"
   nginx http proxy temporary files: "proxy_temp"
   nginx http fastcgi temporary files: "fastcgi_temp"
   nginx http uwsgi temporary files: "uwsgi_temp"
   nginx http scgi temporary files: "scgi_temp"

配置完成! 开始进行安装编译

 [root@465d13a11ffa nginx-1.8.0]# make && make install 
 make[1]: Leaving directory `/Nginx-1.8/nginx-1.8.0'
 make -f objs/Makefile install
 make[1]: Entering directory `/Nginx-1.8/nginx-1.8.0'
 test -d '/export/server/nginx' || mkdir -p '/export/server/nginx'
 test -d '/export/server/nginx/sbin'         || mkdir -p '/export/server/nginx/sbin'
 test ! -f '/export/server/nginx/sbin/nginx'         || mv '/export/server/nginx/sbin/nginx'             '/export/server/nginx/sbin/nginx.old'
 cp objs/nginx '/export/server/nginx/sbin/nginx'
 test -d '/export/server/nginx/conf'         || mkdir -p '/export/server/nginx/conf'
 cp conf/koi-win '/export/server/nginx/conf'
 cp conf/koi-utf '/export/server/nginx/conf'
 cp conf/win-utf '/export/server/nginx/conf'
 test -f '/export/server/nginx/conf/mime.types'         || cp conf/mime.types '/export/server/nginx/conf'
 cp conf/mime.types '/export/server/nginx/conf/mime.types.default'
 test -f '/export/server/nginx/conf/fastcgi_params'         || cp conf/fastcgi_params '/export/server/nginx/conf'
 cp conf/fastcgi_params         '/export/server/nginx/conf/fastcgi_params.default'
 test -f '/export/server/nginx/conf/fastcgi.conf'         || cp conf/fastcgi.conf '/export/server/nginx/conf'
 cp conf/fastcgi.conf '/export/server/nginx/conf/fastcgi.conf.default'
 test -f '/export/server/nginx/conf/uwsgi_params'         || cp conf/uwsgi_params '/export/server/nginx/conf'
 cp conf/uwsgi_params         '/export/server/nginx/conf/uwsgi_params.default'
 test -f '/export/server/nginx/conf/scgi_params'         || cp conf/scgi_params '/export/server/nginx/conf'
 cp conf/scgi_params         '/export/server/nginx/conf/scgi_params.default'
 test -f '/export/server/nginx/conf/nginx.conf'         || cp conf/nginx.conf '/export/server/nginx/conf/nginx.conf'
 cp conf/nginx.conf '/export/server/nginx/conf/nginx.conf.default'
 test -d '/export/server/nginx/logs'         || mkdir -p '/export/server/nginx/logs'
 test -d '/export/server/nginx/logs' ||         mkdir -p '/export/server/nginx/logs'
 test -d '/export/server/nginx/html'         || cp -R html '/export/server/nginx'
 test -d '/export/server/nginx/logs' ||         mkdir -p '/export/server/nginx/logs'
 make[1]: Leaving directory `/Nginx-1.8/nginx-1.8.0
 

Nginx源码编译成功!

 [root@465d13a11ffa nginx]# pwd
 /export/server/nginx

查看nginx的文件目录!

[root@465d13a11ffa nginx]# ls *
 conf:
 fastcgi.conf          fastcgi_params          koi-utf  mime.types          nginx.conf          scgi_params          uwsgi_params          win-utf
 fastcgi.conf.default  fastcgi_params.default  koi-win  mime.types.default  nginx.conf.default  scgi_params.default  uwsgi_params.default
 
 html:
 50x.html  index.html
 
 logs:
 
 sbin:
 nginx

查看容器cpu信息!

 [root@465d13a11ffa conf]# cat /proc/cpuinfo 
 processor    : 0
 vendor_id    : GenuineIntel
 cpu family    : 6
 model        : 58
 model name    : Intel(R) Core(TM) i5-3230M CPU @ 2.60GHz
 stepping    : 9
 microcode    : 25
 cpu MHz        : 2600.219
 cache size    : 6144 KB
 physical id    : 0
 siblings    : 1
 core id        : 0
 cpu cores    : 1
 apicid        : 0
 initial apicid    : 0
 fpu        : yes
 fpu_exception    : yes
 cpuid level    : 5
 wp        : yes
 flags        : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 syscall nx rdtscp lm constant_tsc up rep_good pni monitor ssse3 lahf_lm
 bogomips    : 5200.43
 clflush size    : 64
 cache_alignment    : 64
 address sizes    : 36 bits physical, 48 bits virtual

简单配置Nginx访问:

 [root@465d13a11ffa nginx-1.8.0]# cd /export/server/nginx
 [root@465d13a11ffa nginx]# ls
 client_body_temp  conf  fastcgi_temp  html  logs  proxy_temp  sbin  scgi_temp  uwsgi_temp
 [root@465d13a11ffa nginx]# cat /proc/cpuinfo | grep processor        查看container的内核,以便配置nginx
 processor    : 0
[root@465d13a11ffa nginx]# vi /export/server/nginx/conf/nginx.conf
user  nginx nginx;
 worker_processes  6;
 error_log  logs/error.log;
 pid        logs/nginx.pid;
 events {
         use epoll;
     worker_connections  1024;
 }
 http {
     include       mime.types;
     default_type  application/octet-stream;
     log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                       '$status $body_bytes_sent "$http_referer" '
                       '"$http_user_agent" "$http_x_forwarded_for"';
     access_log  logs/access.log  main;
     sendfile        on;
     tcp_nopush     on;
     keepalive_timeout  65;
     #gzip  on;
     server {
      listen       80;
         server_name  localhost;
         location / {
             root   /export/App;
             index  index.html index.htm;
         }
         error_page  404              /404.html;
         error_page   500 502 503 504  /50x.html;
         location = /50x.html {
             root   html;
         }
 }
 }
  

记得,nginx的工作进程是由nginx用户起的,因此需要添加nginx用户的! 配置完成及得检测配置的完整性和正确性!(然后启动# /export/server/nginx/sbin/nginx)


 [root@465d13a11ffa nginx]# /export/server/nginx/sbin/nginx -t
 nginx: the configuration file /export/server/nginx/conf/nginx.conf syntax is ok
 nginx: configuration file /export/server/nginx/conf/nginx.conf test is successful
 
 [root@465d13a11ffa nginx]# ps -ef | grep nginx
 root     11065     1  0 07:17 ?        00:00:00 nginx: master process ./nginx
 nginx    11066 11065  0 07:17 ?        00:00:00 nginx: worker process
 nginx    11067 11065  0 07:17 ?        00:00:00 nginx: worker process
 nginx    11068 11065  0 07:17 ?        00:00:00 nginx: worker process
 nginx    11069 11065  0 07:17 ?        00:00:00 nginx: worker process
 nginx    11070 11065  0 07:17 ?        00:00:00 nginx: worker process
 nginx    11071 11065  0 07:17 ?        00:00:00 nginx: worker process
 root     11088     1  0 07:25 ?        00:00:00 grep --color=auto nginx

测试: 在配置文件中,将index.html(主机名/容器ID)文件默认放在/export/App/下(即为默认发布目录,一定注意权限哦) [root@465d13a11ffa nginx]# curl http://localhost 465d13a11ffa

编写nginx系统启动脚本!

#vim /etc/init.d/nginx 
 #!/bin/bash
 #
 # chkconfig: - 85 15
 # description: nginx is a World Wide Web server. It is used to serve
 # Source Function Library
 # Nginx Settings
 NGINX_SBIN="/export/server/nginx/sbin/nginx"
 NGINX_CONF="/export/server/nginx/conf/nginx.conf"
 NGINX_PID="/export/server/nginx/logs/nginx.pid"
 RETVAL=0
 prog="Nginx"
 
 start() {
         echo -n $"Starting $prog: "
         mkdir -p /dev/shm/nginx_temp
         $NGINX_SBIN -c $NGINX_CONF
         RETVAL=$?
         if [ $RETVAL -eq 0 ];then
                 echo "OK!"
                 else
                 echo "ERROR,please test the $NGINX_CONF"
         fi
 }
 stop() {
         echo -n $"Stopping $prog: "
         pkill  nginx
         rm -rf /dev/shm/nginx_temp
         RETVAL=$?
         if [ $RETVAL -eq 0 ];then
                 echo "OK!"
                 else
                 echo "ERROR,please test the $NGINX_CONF"
         fi
 }
 
 reload(){
         echo -n $"Reloading $prog: "
         $NGINX_SBIN -s reload
         RETVAL=$?
         if [ $RETVAL -eq 0 ];then
                 echo "OK!"
                 else
                 echo "ERROR,please test the $NGINX_CONF"
         fi
 }
 restart(){
         stop
         start
 }
 configtest(){
     $NGINX_SBIN -c $NGINX_CONF -t
     return 0
 }
 case "$1" in
   start)
         start
         ;;
   stop)
         stop
         ;;
   reload)
         reload
         ;;
   restart)
 restart
         ;;
   configtest)
         configtest
         ;;
   *)
         echo $"Usage: $0 {start|stop|reload|restart|configtest}"
         RETVAL=1
 esac
 exit $RETVAL
 

外网测试:

 [root@xuxuebiao ~]# curl http://10.0.0.2/index.html
 465d13a11ffa

(思考:在Docker管理端可以访问到,怎样设置网络可以在windows主机上进行访问呢!)