How to install and configure Nginx in centos

Source: Internet
Author: User


Nginx can be installed using the default packages on various platforms. This article describes how to use the source code for compilation and installation, including the specific compilation parameter information.

Before the official start, you need to install the gcc g ++ development library in the compiling environment in advance. You have installed it by default.

You can use the following commands to compile the ububtu platform environment:

Apt-get install build-essential
Apt-get install libtool
Use the following commands in the centos compiling environment:

Install make:

Yum-y install gcc automake autoconf libtool make
Install g ++:

Yum install gcc-c ++

Start Now
---------------------------------------------------------------------------
Generally, we need to install pcre and zlib first. The former is used to rewrite, and the latter is used to compress gzip.
1. Select the source code directory
This document selects/usr/local/src.

Cd/usr/local/src

2. Install the PCRE library

Ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/ download the latest PCRE source package, use the following command to download the compilation and installation of PCRE package:

Cd/usr/local/src
Wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.34.tar.gz
Tar-zxvf pcre-8.34.tar.gz
Cd pcre-8.34
./Configure
Make
Make install

3. Install the zlib library

Http://zlib.net/zlib-1.2.8.tar.gz to download the latest zlib source package, use the following command to download the compilation and installation of zlib package:

Cd/usr/local/src

Wget http://zlib.net/zlib-1.2.8.tar.gz
Tar-zxvf zlib-1.2.8.tar.gz
Cd zlib-1.2.8
./Configure
Make
Make install

4. Install ssl (ssl is not installed in some vps by default)

Cd/usr/local/src
Wget http://www.openssl.org/source/openssl-1.0.1c.tar.gz
Tar-zxvf openssl-1.0.1c.tar.gz

5. Install nginx

Nginx generally has two versions: stable version and development version. You can select one of these two versions based on your purpose, the following describes how to install Nginx in the/usr/local/nginx Directory:

Cd/usr/local/src
Wget http://nginx.org/download/nginx-1.4.2.tar.gz
Tar-zxvf nginx-1.4.2.tar.gz
Cd nginx-1.4.2

./Configure -- sbin-path =/usr/local/nginx
-- Conf-path =/usr/local/nginx. conf
-- Pid-path =/usr/local/nginx. pid
With-http_ssl_module
-- With-pcre =/usr/local/src/pcre-8.34
-- With-zlib =/usr/local/src/zlib-1.2.8
-- With-openssl =/usr/local/src/openssl-1.0.1c

Make
Make install
-- With-pcre =/usr/src/pcre-8.34 refers to the source code path of the pcre-8.34.
-- With-zlib =/usr/src/zlib-1.2.7 refers to the source code path of the zlib-1.2.7.

After the installation is successful, the/usr/local/nginx directory is as follows:

Fastcgi. conf koi-win nginx. conf. default
Fastcgi. conf. default logs scgi_params
Fastcgi_params mime. types scgi_params.default
Fastcgi_params.default mime. types. default uwsgi_params
Html nginx uwsgi_params.default
Koi-utf nginx. conf win-utf
6. Start
Ensure that Port 80 of the system is not occupied by other programs. Run the/usr/local/nginx command to start Nginx,

Netstat-ano | grep 80
If no result is found, run the command. If no result is found, ignore this step. (sudo must be enabled in ubuntu; otherwise, it can only run on the foreground)

Sudo/usr/local/nginx
Open the browser to access the IP address of this machine. If the browser displays Welcome to nginx! Nginx has been installed and runs successfully.

 

-----------------------------------------------------
Nginx is installed here. If you only process static html, you do not need to continue the installation.

If you need to process the php script, you also need to install php-fpm.

The following installation troubleshooting

Appendix: possible errors and some help information

1.1 pcre compilation error

Libtool: compile: unrecognized option '-DHAVE_CONFIG_H'
Libtool: compile: Try 'libtool -- help' for more information.
Make [1]: *** [pcrecpp. lo] Error 1
Make [1]: Leaving directory '/usr/local/src/pcre-8.34'
Make: *** [all] Error 2


Solution: install g ++. Do not forget to re-configure

Apt-get install g ++
Apt-get install build-essential
Make clean
./Configure
Make

1.2 make error

Make: *** No rule to make target 'build', needed by 'default'. Stop.
./Configure: error: SSL modules require the OpenSSL library.
You can either do not enable the modules, or install the OpenSSL library
Into the system, or build the OpenSSL library statically from the source
With nginx by using -- with-openssl = option.
Follow the installation method in step 2 or
Under ubuntu

Apt-get install openssl
Apt-get install libssl-dev
Under centos

Yum-y install openssl-devel
2. nginx compilation options

Make is used for Compilation. It reads commands from Makefile and then compiles the commands.

Make install is used for installation. It also reads commands from Makefile and installs them to the specified location.

The configure command is used to detect the target features of your installation platform. It defines all aspects of the system, including the method for handling nginx connections that are allowed. For example, it will detect whether you have CC or GCC, and does not need CC or GCC, it is a shell script. At the end of execution, it creates a Makefile file. The configure command of nginx supports the following parameters:

-- Prefix = path defines a directory to store files on the server, that is, the nginx installation directory. /Usr/local/nginx is used by default.
-- Sbin-path = path: set the path of the nginx executable file. The default value is prefix/sbin/nginx.
-- Conf-path = path: specifies the path of the nginx. conf configuration file. Nginx can be started using different configuration files through the-c option in the command line. The default value is prefix/conf/nginx. conf.
-- Pid-path = path: Set the nginx. pid file and the process number of the stored master process. After the installation is complete, you can change the file name at any time and use the PID command in the nginx. conf configuration file. By default, the file name is prefix/logs/nginx. pid.
-- Error-log-path = path: Set the name of the master error, warning, and diagnostic file. After the installation is complete, you can change the file name at any time and use the error_log command in the nginx. conf configuration file. By default, the file name is prefix/logs/error. log.
-- Http-log-path = path: Set the name of the log file of the HTTP server of the master request. After the installation is complete, you can change the file name at any time and use the access_log command in the nginx. conf configuration file. By default, the file name is prefix/logs/access. log.
-- User = name: the user who sets the nginx worker process. After the installation is complete, you can change the user command used in the nginx. conf configuration file at any time. The default user name is nobody.
-- Group = name: Set the user group of the nginx worker process. After the installation is complete, you can change the user command used in the nginx. conf configuration file at any time. By default, it is a non-privileged user.
-- With-select_module -- without-select_module enables or disables building a module to allow the server to use the select () method. This module is automatically created if the platform does not support kqueue, epoll, rtsig or/dev/poll.
-- With-poll_module -- the without-poll_module enables or disables building a module to allow the server to use the poll () method. This module is automatically created if the platform does not support kqueue, epoll, rtsig or/dev/poll.
-- Without-http_gzip_module-does not compile the response module of the compressed HTTP server. The zlib library is required to compile and run this module.
-- The without-http_rewrite_module does not compile the rewrite module. Compiling and running this module requires support from the PCRE library.
-- Without-http_proxy_module-do not compile the http_proxy module.
-- With-http_ssl_module-use the https protocol module. By default, this module is not built. It is necessary to establish and run the OpenSSL library of this module.
-- With-pcre = path-set the source code path of the PCRE library. The source code of the PCRE library (version 4.4-8.30) needs to be downloaded and decompressed from the PCRE website. The rest of the work is done by Nginx./configure and make. The regular expression is used in the location command and the ngx_http_rewrite_module module.
-- With-pcre-jit-compiled PCRE contains "just-in-time compilation" (in 1.1.12, pcre_jit command ).
-- With-zlib = path-specifies the source code path of the zlib library. Download and decompress zlib (version 1.1.3-1.2.5. The rest of the work is done by Nginx./configure and make. The ngx_http_gzip_module must use zlib.
-- With-cc-opt = parameters-setting additional parameters will be added to the CFLAGS variable. For example, you need to use -- with-cc-opt = "-I/usr/local/include when using the PCRE library on FreeBSD .. To add the number of files supported by select (): -- with-cc-opt = "-D FD_SETSIZE = 2048 ".
-- With-ld-opt = parameters-set the additional parameters, which will be used during the link. For example, if you use the PCRE library of the system in FreeBSD, specify -- with-ld-opt = "-L/usr/local/lib ".

Typical instance (the following content must be written in the same row to show multiple rows)

./Configure
-- Sbin-path =/usr/local/nginx
-- Conf-path =/usr/local/nginx. conf
-- Pid-path =/usr/local/nginx. pid
With-http_ssl_module
-- With-pcre = ../pcre-4.4
-- With-zlib = ../zlib-1.1.3

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.