Nginx
we have chosen to download the source code of the application and compile it manually,
There are two reasons behind this choice—
a) The package may not be available in the enabled repositories of your Linux distribution. On top of that, the rare repositories that offer to download and install Nginx automatically mostly contain outdated versions.
B) More importantly, there is the fact that we need to configure a variety of significant compile time options. As a result of this choice, your system will require some tools and libraries for the compilation process.
GCC — GNU Compiler Collection
Nginx is a program written in C, so you will first need to install a compiler tool such as the GNU Compiler Collection (GCC) on your system. GCC usually comes with most distributions, but if, for some reason, you do not already have it, this step will be required. GCC is a collection of free open source compilers for various languages—C, C++, Java, Ada, FORTRAN, and so on.
First, make sure it isn't already installed on your system:
[alex@example.com ~]$ gcc
If you get the following output, GCC is correctly installed on your system and you can skip to the next section:
gcc: no input files
If you receive the following message, you will have to proceed with the installation of the compiler:
~bash: gcc: command not found
GCC can be installed using the default repositories of your package manager. Depending on your distribution, the package manager will vary—yum for Red
Hat-based distribution, apt for Debian and Ubuntu, yast for SuSE Linux, and
so on. Here is the typical way to proceed with the download and installation of
the GCC package:
[root@example.com ~]# yum install gcc
If you use apt-get:
[root@example.com ~]# apt-get install gcc
PCRE library
The Perl Compatible Regular Expression (PCRE) library is required for compiling Nginx. The Rewrite and HTTP Core modules of Nginx use PCRE for the syntax of their regular expressions. You will need to install two packages—pcre and pcre-devel. The first one provides the compiled version of the library, whereas the second one provides development headers and source for compiling projects.
Using yum:
[root@example.com ~]# yum install pcre pcre-devel
Or install all PCRE-related packages:
[root@example.com ~]# yum install pcre*
If you use apt-get:
[root@example.com ~]# apt-get install libpcre3 libpcre3-dev
If these packages are already installed on your system, you will receive a message saying something like Nothing to do, in other words, the package manager did not install or update any component.
zlib library
The zlib library provides developers with compression algorithms. It is required for the use of gzip compression in various modules of Nginx.
Similar to PCRE, you will need both the library and its source—zlib and zlib-devel.
Using yum:
[root@example.com ~]# yum install zlib zlib-devel
Using apt-get:
[root@example.com ~]# apt-get install zlib1g zlib1g-dev
These packages install quickly and have no known dependency issues.
OpenSSL
The OpenSSL library will be used by Nginx to serve secure web pages. We thus need to install the library and its development package. Install openssl and openssl-devel:
[root@example.com ~]# yum install openssl openssl-devel
Using apt-get:
[root@example.com ~]# apt-get install openssl openssl-dev
Now that you have installed all the prerequisites, you are ready to download and compile the Nginx source code.
Downloading Nginx
The official website, which is at www.nginx.net, is rather simple and does not provide much information or documentation, other than links for downloading
the latest versions. On the contrary, you will find a lot of interesting documentation and examples on the official wiki—wiki.nginx.org.
[root@example.com ~]$ mkdir src && cd src
[root@example.com src]$ wget http://nginx.org/download/nginx-0.7.66.tar.gz
[root@example.com src]$ tar zxf nginx-0.7.66.tar.gz
You have successfully downloaded and extracted Nginx. Now, the next step will be to configure the compilation process in order to obtain a binary that perfectly
fits your operating system.
Configure options
The easy way :
Execute the following three commands to build and install a working version of Nginx:
[root@example.com nginx-0.7.66]# ./configure
Running this command should initiate a long procedure of verifications to ensure that your system contains all the necessary components. If the configuration process fails, please make sure to check the prerequisites section again, as it is the most common cause of errors
[root@example.com nginx-0.7.66]# make
[root@example.com nginx-0.7.66]# make install
This last step will copy the compiled files as well as other resources to the installation directory, by default, /usr/local/nginx
Path options
The options offered by the configuration switches may change according to the version you downloaded. Run the configure --help command to list the available switches for your setup.
[alex@example.com nginx-0.7.66]# ./configure --conf-path=/etc/nginx/nginx.conf
Switch Usage Default Value
--prefix=… The base folder in which Nginx will be installed. /usr/local/nginx.
--sbin-path=… The path where the nginx binary file should be installed. <prefix>/sbin/nginx.
--conf-path=… The path of the main configuration file. <prefix>/conf/nginx.conf.
--error-log-path=… The location of your error log. <prefix>/logs/error.log
--pid-path=… The path of the Nginx pid file <prefix>/logs/nginx.pid.
--lock-path=… The location of the lock file. <prefix>/logs/nginx.lock.
--with-perl_modules_path=… Defines the path to the Perl modules.
--with-perl=… Path to the Perl binary file
--http-log-path=… Defines the location of the access logs. <prefix>/logs/access.log.
--http-client-body-temp-path=… Directory used for storing temporary files generated by client requests. <prefix>/client_body_temp.
--http-proxy-temp-path=… Location of the temporary files used by the proxy. <prefix>/proxy_temp.
--http-fastcgi-temp-path=… Location of the temporary files used by the HTTP FastCGI module. <prefix>/fastcgi_temp.
--builddir=… Location of the application build.
Prerequisites options
Compiler options
--with-cc=… Specifies an alternate location for the C compiler.
--with-cpp=… Specifies an alternate location for the C preprocessor.
--with-cc-opt=… Defines additional options to be passed to the C compiler command line.
--with-ld-opt=… Defines additional options to be passed to the C linker command line.
--with-cpu-opt=… Specifies a different target processor architecture, among the following values: pentium, pentiumpro, pentium3, pentium4, athlon, opteron, sparc32, sparc64, and ppc64.
PCRE options
--without-pcre Disables usage of the PCRE library. This setting is not recommended, as it will remove support for regular expressions, consequently disabling the Rewrite module.
--with-pcre Forces usage of the PCRE library.
--with-pcre=… Allows you to specify the path of the PCRE library source code.
--with-pcre-opt=… Additional options for building the PCRE library.
MD5 options
--with-md5=… Specifies the path to the MD5 library sources.
--with-md5-opt=… Additional options for building the MD5 library.
--with-md5-asm Uses assembler sources for the MD5 library.
SHA1 options
--with-sha1=… Specifies the path to the SHA1 library sources.
--with-sha1-opt=… Additional options for building the SHA1 library.
--with-sha1-asm Uses assembler sources for the SHA1 library.
zlib options
--with-zlib=… Specifies the path to the zlib library sources.
--with-zlib-opt=… Additional options for building the zlib library.
--with-zlib-asm=… Uses assembler optimizations for the following target architectures: pentium, pentiumpro.
OpenSSL options
--with-openssl=… Specifies the path of the OpenSSL library sources.
--with-openssl-opt=… Additional options for building the OpenSSL library.
Module options
Modules enabled by default
Modules enabled by default Description
--without-http_charset_module Disables the Charset module for re-encoding web pages.
--without-http_gzip_module Disables the Gzip compression module.
--without-http_ssi_module Disables the Server Side Include module.
--without-http_userid_module Disables the User ID module providing user identification via cookies.
--without-http_access_module Disables the Access module allowing access configuration for IP address ranges.
--without-http_auth_basic_module Disables the Basic Authentication module.
--without-http_autoindex_module Disables the Automatic Index module.
--without-http_geo_module Disables the Geo module allowing you to define variables depending on IP address ranges.
--without-http_map_module Disables the Map module that allows you to declare map blocks.
--without-http_referer_module Disables the Referer control module.
--without-http_rewrite_module Disables the Rewrite module.
--without-http_proxy_module Disables the Proxy module for transferring requests to other servers
--without-http_fastcgi_module Disables the FastCGI module for interacting with a FastCGI process.
--without-http_memcached_module Disables the Memcached module for interacting with the memcache daemon.
--without-http_limit_zone_module Disables the Limit Zone module for restricting resource usage according to defined zones.
--without-http_limit_req_module Disables the Limit Requests module allowing you to limit the amount of requests per user.
--without-http_empty_gif_module Disables the Empty Gif module for serving a blank GIF image from memory.
--without-http_browser_module Disables the Browser module for interpreting the User Agent string.
--without-http_upstream_ip_hash_module Disables the Upstream module for configuring load-balanced architectures.
Modules disabled by default
Modules disabled by default Description
--with-http_ssl_module Enables the SSL module for serving pages using HTTPS.
--with-http_realip_module Enables the Real IP module for reading the real IP address from the request header data.
--with-http_addition_module Enables the Addition module which lets you append or prepend data to the response body.
--with-http_xslt_module Enables the XSLT module for applying XSL transformations to XML documents.
Note: You will need to install the libxml2 and libxslt libraries on your system if you wish to compile these modules.
--with-http_image_filter_module Enables the Image Filter module that lets you apply modification to images.
Note: You will need to install the libgd library on your system if you wish to compile this module.
--with-http_geoip_module Enables the GeoIP module for achieving geographic localization using MaxMind's GeoIP binary database.
Note: You will need to install the libgeoip library on your system if you wish to compile this module.
--with-http_sub_module Enables the Substitution module for replacing text in web pages.
--with-http_dav_module Enables the WebDAV module (Distributed Authoring and Versioning via Web).
--with-http_flv_module Enables the FLV module for special handling of .flv (flash video) files.
--with-http_gzip_static_module Enables the Gzip Static module for sending pre-compressed files.
--with-http_random_index_module Enables the Random Index module for picking a random file as the directory index.
--with-http_secure_link_module Enables the Secure Link module to check the presence of a keyword in the URL.
--with-http_stub_status_module Enables the Stub Status module, which generates a server statistics and information page.
--with-google_perftools_module Enables the Google Performance Tools module.
Miscellaneous options
Mail server proxy options
--with-mail Enables mail server proxy module. Supports POP3, IMAP4, SMTP. It is disabled by default.
--with-mail_ssl_module Enables SSL support for the mail server proxy. It is disabled by default.
--without-mail_pop3_module Disables the POP3 module for the mail server proxy. It is enabled by default when the mail server proxy module is enabled.
--without-mail_imap_module Disables the IMAP4 module for the mail server proxy. It is enabled by default when the mail server proxy module is enabled.
--without-mail_smtp_module Disables the SMTP module for the mail server proxy. It is enabled by default when the mail server proxy module is enabled.
Event management:
--with-rtsig_module Enables the rtsig module to use rtsig as event notification mechanism.
--with-select_module Enables the select module to use select as event notification mechanism. By default, this module is enabled unless a better method is found on the system—kqueue, epoll, rtsig, or poll.
--without-select_module Disables the select module.
--with-poll_module Enables the poll module to use poll as event notification mechanism. By default, this module is enabled if available, unless a better method is found on the system—kqueue, epoll, or rtsig.
--without-poll_module Disables the poll module.
User and group options
--user=… Default user account for starting the Nginx worker processes. This setting is used only if you omit to specify the user directive in the configuration file.
--group=… Default user group for starting the Nginx worker processes. This setting is used only if you omit to specify the group directive in the configuration file.
Other options
--with-ipv6 Enables IPv6 support.
--without-http Disables the HTTP server.
--without-http-cache Disables HTTP caching features.
--add-module=PATH Adds a third-party module to the compile process by specifying its path. This switch can be repeated indefinitely if you wish to compile multiple modules.
--with-debug Enables additional debugging information to be logged
Testing the configuration
[alex@example.com ~]$ /usr/local/nginx/sbin/nginx –t
What is an init script?
An init script, also known as service startup script or even sysv script is a shell script respecting a certain standard. The script will control a daemon application by responding to some commands such as start, stop, and others, which are triggered at two levels. Firstly, when the computer starts, if the service is scheduled to be started for the system runlevel, the init daemon will run the script with the start argument. The other possibility for you is to manually execute the script by calling it from
the shell.
[root@example.com ~]# service httpd start
Or if your system does not come with the service command:
[root@example.com ~]# /etc/init.d/httpd start
Note that since service httpd start and /etc/init.d/httpd start essentially do the same thing, with the exception that the second command will work on all operating systems
Creating an init script for Nginx
First, create a file called nginx with the text editor of your choice, and save it in the /etc/init.d/ directory (on some systems, /etc/init.d/ is actually a symbolic link to /etc/rc.d/init.d/). Copy the following script carefully. Make sure that you change the paths to make them correspond to your actual setup.
You will need root permissions to save the script into the init.d directory.
#! /bin/sh
# Author: Ryan Norbauer http://norbauerinc.com
# Modified: Geoffrey Grosenbach http://topfunky.com
# Modified: Clement NEDELCU
# Reproduced with express authorization from its contributors
set –e
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DESC="nginx daemon"
NAME=nginx
DAEMON=/usr/local/nginx/sbin/$NAME
SCRIPTNAME=/etc/init.d/$NAME
# If the daemon file is not found, terminate the script.
test -x $DAEMON || exit 0
d_start() {
$DAEMON || echo -n " already running"
}
d_stop() {
$DAEMON –s quit || echo -n " not running"
}
d_reload() {
$DAEMON –s reload || echo -n " could not reload"
}
case "$1" in
start)
echo -n "Starting $DESC: $NAME"
d_start
echo "."
;;
stop)
echo -n "Stopping $DESC: $NAME"
d_stop
echo "."
;;
reload)
echo -n "Reloading $DESC configuration..."
d_reload
echo "reloaded."
;;
restart)
echo -n "Restarting $DESC: $NAME"
d_stop
# Sleep for two seconds before starting again, this should give the
# Nginx daemon some time to perform a graceful stop.
sleep 2
d_start
echo "."
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|restart|reload}" >&2
exit 3
;;
esac
exit 0
Installing the script
Make the script executable. Granting executable permissions on the script is done with the chmod command:
[root@example.com ~]# chmod +x /etc/init.d/nginx
The last step here will be to make it so the script is automatically started at the proper runlevels.
Debian-based distributions
For the first one, a simple command will enable the init script for the system runlevel:
[root@example.com ~]# update-rc.d –f nginx defaults
This command will create links in the default system runlevel folders: for the reboot and shutdown runlevels, the script will be executed with the stop argument; for all other runlevels, the script will be executed with start. You can now restart your system and see your Nginx service being launched during the boot sequence.
Red Hat-based distributions
For the Red Hat-based systems family, the command differs, but you get an additional tool for managing system startup. Adding the service can be done
via the following command:
[root@example.com ~]# chkconfig --add nginx
Once that is done, you can then verify the runlevels for the service:
[root@example.com ~]# chkconfig --list nginx
Nginx 0:off 1:off 2:on 3:off 4:on 5:on 6:off
Another tool will be useful to you for managing system services, namely, ntsysv. It lists all services scheduled to be executed on system startup and allows you to enable or disable them at will. Note that you must first run the chkconfig --add nginx command, otherwise nginx will not appear in the list of services.
No comments:
Post a Comment