在CentOS下源码编译安装nginx教程
来源:互联网 发布:国产野牛气垫船知乎 编辑:程序博客网 时间:2024/06/06 02:48
Introduction
nginx pronounced as "engine x" is an HTTP and reverse proxy server, as well as a mail proxy server.
nginx
is an opensource web server which uses epoll mechanism to serve clients as opposed to Apache which uses a thread based model which delegates the requests to an instance in the thread pool. nginx
is being used more over Apache because of its speed. nginx
has over 13% market share and increasing continuously.
Why compile from source
Compiling from the source code is useful when:
- Upgrading to the latest version right after the release.
- Fixing security vulnerabilities
- Fixing known bugs which has been impacting your service
- Modifying defaults like the server name
- Applying patches with fixing known bugs
- Software repositories are not upgraded with the latest version because of the dependency hierarchy
Its painful because:
- You need to keep up to date with software versions
- Your server software might depend on older versions in the dependency tree
Modules and 3rd Party Modules
nginx has many modules which add features on top of existing VPS. Some of the famous 3rd party modules are:
- SPDY was included in nginx 1.5.0 (earlier was provided as a patch)
- google pagespeed(source) speeds up your site and reduces page load time by automatically applying web performance best practices to pages and associated assets (CSS, JavaScript, images) without requiring you to modify your existing content or workflow
- ModSecurity is an open source web application firewall to reduce known attacks on application servers. *TCP Proxy allows nginx to proxy through tcp server instead of the default socket mode.
Setup your VPS
Create a new VPS or choose an existing one
Create a new VPS with your domain name. We will be using 'example.com' as the domain or you can use the server IP instead of the hostname.
Setting Up Pre-requisites to Compile from Source
Securely Login into your VPS
You can login into the VPS with your password for your host.
ssh root@example.com
Install dependencies for nginx
We have few pre-requisites to be installed to compile which include development libraries along with source code compilers.
yum -y install gcc gcc-c++ make zlib-devel pcre-devel openssl-devel
Lets first create a directory to store our source code:
mkdir -p src && cd src
Compiling from Source
Downloading the source code
Lets get the current nginx version number from http://nginx.org/en/download.html
Run the following commands to download the source.
nginxVersion="1.5.5"wget http://nginx.org/download/nginx-$nginxVersion.tar.gztar -xzf nginx-$nginxVersion.tar.gzln -sf nginx-$nginxVersion nginx
Preparing the nginx source
We first want to prepare nginx
with necessary basic options.
For a full list of options you can look at ./configure --help
Options for basic file path names
These options are the basic variables which we override to use default system paths at /etc/
to ensure it works simliar when installed via rpm
. The user
and group
option are used to run the nginx worker processes in non-privileged.
--user--group--prefix--sbin-path--conf-path--lock-path--pid-path--http-log-path--error-log-path
Other options
--with-http_gzip_static_module
option enables nginx to usegzip
(Before serving a file from disk to a gzip-enabled client, this module will look for a precompressed file in the same location that ends in ".gz". The purpose is to avoid compressing the same file each time it is requested.).[recommended for reducing size of information sent]--with-http_stub_status_module
option enables other plugins over nginx to allow us to get the status (This module provides the ability to get some status from nginx.). [recommended for getting stats]--with-http_ssl_module
- required if you want to run a HTTPS server. See How To Create a SSL Certificate on nginx for CentOS 6--with-pcre
option enables to match routes via Regular Expression Matching when defining routes. [recommended, you will find more use of this once you start adding and matching routes]--with-file-aio
- enables asynchronous I/O, better than the default send file option(recommended if you are allowing users to download static files)--with-http_realip_module
is used for getting the IP of the client when behind a load balancer. This is useful when serving content behind CloudFlare like services.--without-http_scgi_module
- Disable SCGI module (normally used when running CGI scripts)--without-http_uwsgi_module
- Disable UWSGI module (normally used when running CGI scripts)--without-http_fastcgi_module
- Disable FastCGI module (normally used when running CGI scripts)
Our configuration options
cd nginx./configure \--user=nginx \--group=nginx \--sbin-path=/usr/sbin/nginx \--prefix=/etc/nginx \--conf-path=/etc/nginx/nginx.conf \--error-log-path=/var/log/nginx/error.log \--pid-path=/var/run/nginx.pid \--lock-path=/var/run/nginx.lock \--with-http_stub_status_module \--http-log-path=/var/log/nginx/access.log \--with-http_gzip_static_module \--with-http_ssl_module \--without-http_scgi_module \--with-pcre \--with-file-aio \--with-http_realip_module \--without-http_uwsgi_module \--without-http_fastcgi_module
Compiling the nginx source
Once we are able to configure the source which even checks for additional requirements like the compiler(gcc, g++) which we installed in the pre-requisites
step:
makemake install
Running the VPS
Add the user nginx to the system. This is a one time command:
useradd -r nginx
We need to setup the file
/etc/init.d/nginx
to run when system starts:#!/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# user: nginx# config: /etc/nginx/nginx.conf# pidfile: /var/run/nginx.pid# Source function library.# Check that networking is up.. /etc/rc.d/init.d/functions# Source networking configuration.. /etc/sysconfig/network[ "$NETWORKING" = "no" ] && exit 0start() {nginx="/usr/sbin/nginx"prog=$(basename $nginx)NGINX_CONF_FILE="/etc/nginx/nginx.conf"lockfile=/var/run/nginx.lock [ -x $nginx ] || exit 5[ $retval -eq 0 ] && touch $lockfile[ -f $NGINX_CONF_FILE ] || exit 6 echo -n $"Starting $prog: " daemon $nginx -c $NGINX_CONF_FILE retval=$? echo return $retval}stop() {stopecho -n $"Stopping $prog: " killproc $prog -QUIT retval=$? echo [ $retval -eq 0 ] && rm -f $lockfile return $retval}restart() { configtest || return $? start$nginx -t -c $NGINX_CONF_FILE}reload() { configtest || return $? echo -n $"Reloading $prog: " killproc $nginx -HUP RETVAL=$? echo}force_reload() { restart}configtest() {}rh_status() {$1status $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 ;;condrestart|try-restart)restart|configtest) $1 ;; reload) rh_status_q || exit 7 $1 ;; force-reload) force_reload ;; status) rh_status ;;esacrh_status_q || exit 0 ;; *) echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"exit 2Optionally, you can obtain the source from:
wget -O /etc/init.d/nginx https://gist.github.com/sairam/5892520/raw/b8195a71e944d46271c8a49f2717f70bcd04bf1a/etc-init.d-nginx
This file should be made executable so that we can use it via 'service nginx ':
chmod +x /etc/init.d/nginx
Set the service to start whenever the system boots:
chkconfig --add nginxchkconfig --level 345 nginx onConfigure /etc/nginx/nginx.conf to set
types_hash_bucket_size
andserver_names_hash_bucket_size
which needs to be increased.http {include mime.types;default_type application/octet-stream;# add the below 2 lines under http around line 20types_hash_bucket_size 64;server_names_hash_bucket_size 128;Start the server. This will start the VPS on port 80.
service nginx start
Setup Complete
Visit example.com or your IP address in the browser. You will see:
Welcome to nginx!
Congratulations! Your brand new nginx server is up.
- 在CentOS下源码编译安装nginx教程
- centos 下nginx源码编译安装
- 学习-在Centos下安装Nginx教程
- 在Centos下安装Nginx教程
- CentOS源码编译安装Nginx
- Centos下编译安装nginx
- centos下nginx编译安装
- CentOS 下编译安装 Nginx
- Nginx + PHP + MySQL 在 CentOS 下的编译安装
- 在CentOS 7下编译安装Nginx+PHP+MySQL环境
- 【Nginx】CentOS下编译安装nginx
- Linux(CentOS) - 源码编译安装Nginx
- linux下通过源码编译安装Nginx
- nginx在cygwin源码编译和安装
- Linux(centos)下mysql编译安装教程
- Centos下Cmake源码编译安装MySQL
- CentOS 7.0 下 源码编译安装PostgreSQL
- CentOS 7.0 下 源码编译安装PostgreSQL
- View
- 走遍中国 —— 大西北
- sendmessage和postmessage的区别
- Java复制文件夹的所有内容到指定文件夹
- Android 插件化原理解析——插件加载机制
- 在CentOS下源码编译安装nginx教程
- Android学习之腾讯互动直播开发经验小结
- 360隐私保险箱 vs misuo
- js学习笔记1
- C#中Socket通信编程的同步实现
- Spark使用CombineTextInputFormat缓解小文件过多导致Task数目过多的问题
- 在MFC MDI程序中屏蔽掉菜单栏
- 二极管的分类
- maven项目在eclipse中的显示问题(一)