Pre-requisites for setting up Nginx
I was trying to set up Nginx on OpenShift and I'd like to share my experience here.
To get started, you need to have an OpenShift account. You can register for a free OpenShift account.
I recommend you to install the command line client. Follow the getting started guide to install the client tools and to setup the environment on your machine.
Create OpenShift application
To create an OpenShift application, you can use the client tools that you just installed.
Let's name the application as mysite and with diy-0.1 type.
rhc app create mysite diy-0.1
Show the newly created application info:
rhc app show -a mysite
Use the SSH information from the Git URL to SSH into your application.
ssh <random-strings>@mysite-<your-namespace>.rhcloud.com
The environment variables that are available to you in your OpenShift application can be found here. Those environment variables are very useful.
Install Nginx
Now we can start the Nginx installation. Navigate to the tmp dir and download the Nginx source.
cd $OPENSHIFT_TMP_DIR
wget <a href="http://nginx.org/download/nginx-1.2.2.tar.gz" title="http://nginx.org/download/nginx-1.2.2.tar.gz">http://nginx.org/download/nginx-1.2.2.tar.gz</a>
tar zxf nginx-1.2.2.tar.gz
cd nginx-1.2.2
If you run
./configure --prefix=$OPENSHIFT_DATA_DIR
directly, you will get the following errors:
checking for PCRE library ... not found
checking for PCRE library in /usr/local/ ... not found
checking for PCRE library in /usr/include/pcre/ ... not found
checking for PCRE library in /usr/pkg/ ... not found
checking for PCRE library in /opt/local/ ... not found
./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with Nginx by using --with-pcre=<path> option.
There is no way for us to install the PCRE lib into the system, so we are left with second option which is to build it from the source directly.
cd $OPENSHIFT_TMP_DIR
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.31.tar.bz2
tar jxf pcre-8.31.tar.bz2
You can check the standard and optional HTTP modules that are supported by Nginx here so that you can enable them when configuring the makefile.
cd nginx-1.2.2
./configure --prefix=$OPENSHIFT_DATA_DIR --with-pcre=$OPENSHIFT_TMP_DIR/pcre-8.31
You should be able to see the following output if it's successful. Those information will be needed for you to configure Nginx later on.
Configuration summary
+ using PCRE library: /tmp//pcre-8.31
+ OpenSSL library is not used
+ md5: using system crypto library
+ sha1: using system crypto library
+ using system zlib library
nginx path prefix: "/var/lib/stickshift/c45cdc9a27944dc5b1cd7cb9e5c9f8c7/mysite/runtime/"
nginx binary file: "/var/lib/stickshift/c45cdc9a27944dc5b1cd7cb9e5c9f8c7/mysite/runtime//sbin/nginx"
nginx configuration prefix: "/var/lib/stickshift/c45cdc9a27944dc5b1cd7cb9e5c9f8c7/mysite/runtime//conf"
nginx configuration file: "/var/lib/stickshift/c45cdc9a27944dc5b1cd7cb9e5c9f8c7/mysite/runtime//conf/nginx.conf"
nginx pid file: "/var/lib/stickshift/c45cdc9a27944dc5b1cd7cb9e5c9f8c7/mysite/runtime//logs/nginx.pid"
nginx error log file: "/var/lib/stickshift/c45cdc9a27944dc5b1cd7cb9e5c9f8c7/mysite/runtime//logs/error.log"
nginx http access log file: "/var/lib/stickshift/c45cdc9a27944dc5b1cd7cb9e5c9f8c7/mysite/runtime//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"
Now we can compile and install the Nginx.
make install
Once it's done, you can navigate to $OPENSHIFT_DATA_DIR where your Nginx is installed.
Configure Nginx
OpenShift only allow an internal IP address and port for your application which are available through $OPENSHIFT_INTERNAL_IP and $OPENSHIFT_INTERNAL_PORT enviroment variables. And these values may change.
It will be easy if we can simply specify these environment variables directly to nginx.conf by using the env directive. But those env variables can only be referred in the main block of the config, not the http, server or location blocks.
So, we need to do a little trick here to bind the internal IP address and port in Nginx configuration dynamically. Let's edit the Nginx configuration file:
vi $OPENSHIFT_DATA_DIR/conf/nginx.conf
Modify the listen value to:
http {
…
server {
listen $OPENSHIFT_IP:$OPENSHIFT_PORT;
server_name localhost;
…
}
…
}
Rename the modified configuration file.
mv $OPENSHIFT_DATA_DIR/conf/nginx.conf $OPENSHIFT_DATA_DIR/conf/nginx.conf.template
We will modify the $OPENSHIFT_IP and $OPENSHIFT_PORT values when the start action hook is called. I am going to show it to you in the next section.
Final touch up
To start up your application automatically, edit the .openshift/action_hooks/start
file.
Exit from your ssh session. On your machine,
cd mysite
vi .openshift/action_hooks/start
#!/bin/bash
# The logic to start up your application should be put in this
# script. The application will work only if it binds to
# $OPENSHIFT_INTERNAL_IP:8080
# nohup $OPENSHIFT_REPO_DIR/diy/testrubyserver.rb $OPENSHIFT_INTERNAL_IP $OPENSHIFT_REPO_DIR/diy > $OPENSHIFT_DIY_LOG_DIR/server.log 2>&1 &
# replace the $OPENSHIFT_INTERNAL_IP and $OPENSHIFT_INTERNAL_PORT before starting up the server
sed -e "s/`echo '$OPENSHIFT_IP:$OPENSHIFT_PORT'`/`echo $OPENSHIFT_INTERNAL_IP:$OPENSHIFT_INTERNAL_PORT`/" $OPENSHIFT_DATA_DIR/conf/nginx.conf.template > $OPENSHIFT_DATA_DIR/conf/nginx.conf
nohup $OPENSHIFT_DATA_DIR/sbin/nginx > $OPENSHIFT_DIY_LOG_DIR/server.log 2>&1 &
git commit -a -m "start nginx when starting up the app"
git push
Finally, use your browser again to navigate to http://mysite-.rhcloud.com. The same welcome page earlier will be displayed. And we are done.
Note:
Use rhc tail -a mysite
command to troubleshoot the problem if you are having problem with the start script.
저자 소개
채널별 검색
오토메이션
기술, 팀, 인프라를 위한 IT 자동화 최신 동향
인공지능
고객이 어디서나 AI 워크로드를 실행할 수 있도록 지원하는 플랫폼 업데이트
오픈 하이브리드 클라우드
하이브리드 클라우드로 더욱 유연한 미래를 구축하는 방법을 알아보세요
보안
환경과 기술 전반에 걸쳐 리스크를 감소하는 방법에 대한 최신 정보
엣지 컴퓨팅
엣지에서의 운영을 단순화하는 플랫폼 업데이트
인프라
세계적으로 인정받은 기업용 Linux 플랫폼에 대한 최신 정보
애플리케이션
복잡한 애플리케이션에 대한 솔루션 더 보기
오리지널 쇼
엔터프라이즈 기술 분야의 제작자와 리더가 전하는 흥미로운 스토리
제품
- Red Hat Enterprise Linux
- Red Hat OpenShift Enterprise
- Red Hat Ansible Automation Platform
- 클라우드 서비스
- 모든 제품 보기
툴
체험, 구매 & 영업
커뮤니케이션
Red Hat 소개
Red Hat은 Linux, 클라우드, 컨테이너, 쿠버네티스 등을 포함한 글로벌 엔터프라이즈 오픈소스 솔루션 공급업체입니다. Red Hat은 코어 데이터센터에서 네트워크 엣지에 이르기까지 다양한 플랫폼과 환경에서 기업의 업무 편의성을 높여 주는 강화된 기능의 솔루션을 제공합니다.