Home Backend Development PHP Tutorial LNMP environment construction-Nginx chapter

LNMP environment construction-Nginx chapter

Aug 08, 2016 am 09:24 AM
http module nginx usr

1.Nginx配置文件测试

<code>root<span>@kallen</span><span>:/usr/local/nginx/sbin</span><span># nginx -t </span><span>nginx:</span> the configuration file /etc/nginx/nginx.conf syntax is ok 
<span>nginx:</span> configuration file /etc/nginx/nginx.conf test is successful</code>
Copy after login

2.Nginx启动

<code>[root<span>@kallen</span> ~]<span># /usr/local/nginx/sbin/nginx </span></code>
Copy after login

3.Nginx负载均衡

Nginx 的upstream 目前支持4 种方式的分配——

(1)轮询(默认) :

每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。

(2)weight :

指定轮询几率,weight 和访问比率成正比,用于后端服务器性能不均的情况。

(3)ip_hash :

每个请求按访问ip 的hash 结果分配,这样每个访客固定访问一个后端服务器,可以解决session 的问题。可以针对同一个C 类地址段中的客户端选择同一个后端服务器,除非那个后端服务器宕了才会换一个。

(4)fair(第三方):

按后端服务器的响应时间来分配请求,响应时间短的优先分配。

(5)url_hash(第三方):

按访问url 的hash 结果来分配请求,使每个url 定向到同一个后端服务器,后端服务器为缓存时比较有效。

4.Nginx安装及配置

(1) nginx源码安装

<code>[root<span>@kallen</span> ~]<span># cd /usr/local/src/</span></code>
Copy after login
<code>[root<span>@kallen</span> ~]<span># wget http://syslab.comsenz.com/downloads/linux/nginx-0.9.6.tar.gz</span>
[root<span>@kallen</span> ~]<span># tar zxvf nginx-0.9.6.tar.gz</span>
[root<span>@kallen</span> ~]<span># cd nginx-0.9.6</span></code>
Copy after login
<code>./configure --prefix=/usr/local/nginx 
-<span>-sbin-path=<span>/usr/local</span><span>/nginx/sbin</span><span>/nginx 
</span></span>-<span>-conf-path=<span>/usr/local</span><span>/nginx/conf</span><span>/nginx.conf 
</span></span>-<span>-error-log-path=<span>/usr/local</span><span>/nginx/logs</span><span>/error.log 
</span></span>-<span>-http-log-path=<span>/usr/local</span><span>/nginx/logs</span><span>/access.log 
</span></span>-<span>-pid-path=<span>/usr/local</span><span>/nginx/var</span><span>/nginx.pid 
</span></span>-<span>-lock-path=<span>/usr/local</span><span>/nginx/var</span><span>/nginx.lock 
</span></span>-<span>-http-client-body-temp-path=<span>/dev/shm</span><span>/nginx_temp/client</span>_body 
</span>-<span>-http-proxy-temp-path=<span>/dev/shm</span><span>/nginx_temp/proxy</span></span>-<span>-http-fastcgi-temp-path=<span>/dev/shm</span><span>/nginx_temp/fastcgi</span></span>-<span>-user=www --group=www 
</span>-<span>-with-cpu-opt=pentium4F 
</span>-<span>-without-select_module 
</span>-<span>-without-poll_module 
</span>-<span>-with-http_realip_module 
</span>-<span>-with-http_sub_module 
</span>-<span>-with-http_gzip_static_module 
</span>-<span>-with-http_stub_status_module 
</span>-<span>-without-http_ssi_module 
</span>-<span>-without-http_userid_module 
</span>-<span>-without-http_geo_module 
</span>-<span>-without-http_memcached_module 
</span>-<span>-without-http_map_module 
</span>-<span>-without-mail_pop3_module 
</span>-<span>-without-mail_imap_module 
</span>-<span>-without-mail_smtp_module 
</span>-<span>-with-pcre=<span>/usr/local</span><span>/src/pcre</span>-<span>8.32</span>/ 
</span>-<span>-with-zlib=<span>/usr/local</span><span>/zlib</span></span></code>
Copy after login
<code>[root<span>@kallen</span> ~]<span># make && make install </span>
[root<span>@kallen</span> ~]<span># mkdir /dev/shm/nginx_temp</span></code>
Copy after login

有的nginx版本编译时会因为pcre编译不过去,需要修改一下
--with-pcre=/usr/local/src/pcre-8.32,前提是已经下载了pcre源码包pcre-7.8.tar.gz,并解压到/usr/local/src/pcre-8.32,不需要编译pcre.


在实际安装过程中可能需要手动安装以下依赖包:

a. 安装依赖软件

<code><span>apt</span><span>-</span><span>get</span><span>-</span><span>-</span><span>install</span><span>-</span><span>suggests</span><span>install</span><span>gcc</span><span>g</span><span>+</span><span>+</span><span>make</span></code>
Copy after login

b. 下载相关软件

<code>wget http://jaist<span>.dl</span><span>.sourceforge</span><span>.net</span>/project/pcre/pcre/<span>8.35</span>/pcre-<span>8.35</span><span>.tar</span><span>.gz</span>
wget http://zlib<span>.net</span>/zlib-<span>1.2</span><span>.8</span><span>.tar</span><span>.gz</span>
wget http://www<span>.openssl</span><span>.org</span>/source/openssl-<span>1.0</span><span>.1</span>g<span>.tar</span><span>.gz</span>
wget http://www<span>.canonware</span><span>.com</span>/download/jemalloc/jemalloc-<span>3.6</span><span>.0</span><span>.tar</span><span>.bz</span>2
wget http://tengine<span>.taobao</span><span>.org</span>/download/tengine-<span>2.0</span><span>.2</span><span>.tar</span><span>.gz</span></code>
Copy after login

c. 安装Pcre

<code>  tar zxvf pcre-<span>8.35</span><span>.tar</span><span>.gz</span>
  cd pcre-<span>8.35</span>
 ./configure --prefix=/usr/local/pcre-<span>8.35</span>
  make && make install</code>
Copy after login

d. 安装Zlib

<code>  tar zxvf zlib-<span>1.2</span><span>.8</span><span>.tar</span><span>.gz</span>
  cd zlib-<span>1.2</span><span>.8</span>
  ./configure --prefix=/usr/local/zlib-<span>1.2</span><span>.8</span>
  make && make install</code>
Copy after login

[ERROR]-1:

<code>./configure: error: <span>the</span> HTTP gzip module requires <span>the</span> zlib library. 
You can either disable <span>the</span> module <span>by</span><span>using</span><span>--without-http_gzip_module </span>
option, <span>or</span> install <span>the</span> zlib library <span>into</span><span>the</span><span>system</span>, <span>or</span> build <span>the</span> zlib library 
statically <span>from</span><span>the</span> source <span>with</span> nginx <span>by</span><span>using</span><span>--with-zlib=<path> opti</span></code>
Copy after login

[ERROR]-2:

<code>configure: error: You need a C<span>++ compiler for C+</span>+ support.
make[1]: *** [/usr/local/src/pcre-8.32/Makefile] Error 1
make[1]: Leaving directory <span>`/home/kallen/MyDOC/nginx-1.8.0'</span>
make: *** [build] Error 2</code>
Copy after login

安装完成后的配置信息如下:

<code>[Nginx <span>Configuration</span> Summary]
<span>Configuration</span> summary
+ using PCRE <span>library</span>: /usr/local/src/pcre-<span>8.32</span>
+ OpenSSL <span>library</span><span>is</span><span>not</span> used
+using builtin md5 code
+ sha1 <span>library</span><span>is</span><span>not</span> found
+ using zlib <span>library</span>: /usr/local/zlib
  nginx path prefix:<span>"/usr/local/nginx"</span>
  nginx binary <span>file</span>:<span>"/usr/local/nginx/sbin/nginx"</span>
  nginx <span>configuration</span> prefix:<span>"/usr/local/nginx/conf"</span>
  nginx <span>configuration</span><span>file</span>:<span>"/usr/local/nginx/conf/nginx.conf"</span>
  nginx pid <span>file</span>:<span>"/usr/local/nginx/var/nginx.pid"</span>
  nginx error log <span>file</span>:<span>"/usr/local/nginx/logs/error.log"</span>
  nginx http <span>access</span> log <span>file</span>:<span>"/usr/local/nginx/logs/access.log"</span>
  nginx http client request <span>body</span> temporary files:<span>"/dev/shm/nginx_temp/client_body"</span>
  nginx http proxy temporary files:<span>"/dev/shm/nginx_temp/proxy"</span>
  nginx http fastcgi temporary files:<span>"/dev/shm/nginx_temp/fastcgi"</span>
  nginx http uwsgi temporary files:<span>"uwsgi_temp"</span>
  nginx http scgi temporary files:<span>"scgi_temp"</span></code>
Copy after login

(2) 编写nginx启动脚本

<code>[root<span>@kallen</span> init.d]<span># vi /etc/init.d/nginx</span></code>
Copy after login

写入以下内容:

<code><span>#!/bin/bash</span><span>#        </span><span># Startup script for the Nginx HTTP Server</span><span>#</span><span># Kallen Ding, Apr 23 2015</span>NGINX_PATH=<span>"/usr/local/nginx/"</span>
OPTIONS=<span>"-c <span>${NGINX_PATH}</span>conf/nginx.conf"</span>
prog=nginx
nginx=<span>${NGINX_PATH}</span>sbin/nginx
pidfile=/var/run/nginx.pid

<span># Source function library.</span>
. /etc/rc.d/init.d/functions

<span><span>start</span></span>() {
    <span>echo</span> -n <span>"Starting <span>$prog</span>: "</span>
    daemon --pidfile=<span>${pidfile}</span><span>$nginx</span><span>$OPTIONS</span>
    RETVAL=$?
    <span>echo</span><span>return</span><span>$RETVAL</span>
}

<span><span>stop</span></span>() {
    <span>echo</span> -n <span>"Stopping <span>$prog</span>: "</span>
    killproc -p <span>${pidfile}</span><span>$nginx</span>
    RETVAL=$?
    <span>echo</span>
}
<span><span>reload</span></span>() {
    <span>echo</span> -n $<span>"Reloading <span>$prog</span>: "</span>
    killproc -p <span>${pidfile}</span><span>$nginx</span> -HUP
    RETVAL=$?
    <span>echo</span>
}

<span># See how we were called.</span><span>case</span><span>"<span>$1</span>"</span><span>in</span>
  start)
    start
    ;;
  stop)
    stop
    ;;
  restart)
    stop
    start
    ;;
  reload)
        reload
    ;;
  status)
    status <span>$prog</span>
    RETVAL=$?
    ;;
  *)
    <span>echo</span><span>"Usage: <span>$prog</span> {start|stop|restart|reload|status}"</span>
    RETVAL=<span>3</span><span>esac</span><span>exit</span><span>$RETVAL</span></code>
Copy after login

保存后,更改/etc/init.d/nginx的权限

<code>[root<span>@kallen</span> ~]<span># chmod 755 /etc/init.d/nginx</span>
[root<span>@kallen</span> ~]<span># chkconfig --add nginx</span>
[root<span>@kallen</span> ~]<span># chkconfig nginx on</span></code>
Copy after login

以上就介绍了LNMP环境搭建——Nginx篇,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1656
14
PHP Tutorial
1257
29
C# Tutorial
1229
24
How to configure nginx in Windows How to configure nginx in Windows Apr 14, 2025 pm 12:57 PM

How to configure Nginx in Windows? Install Nginx and create a virtual host configuration. Modify the main configuration file and include the virtual host configuration. Start or reload Nginx. Test the configuration and view the website. Selectively enable SSL and configure SSL certificates. Selectively set the firewall to allow port 80 and 443 traffic.

How to check whether nginx is started How to check whether nginx is started Apr 14, 2025 pm 01:03 PM

How to confirm whether Nginx is started: 1. Use the command line: systemctl status nginx (Linux/Unix), netstat -ano | findstr 80 (Windows); 2. Check whether port 80 is open; 3. Check the Nginx startup message in the system log; 4. Use third-party tools, such as Nagios, Zabbix, and Icinga.

How to check the name of the docker container How to check the name of the docker container Apr 15, 2025 pm 12:21 PM

You can query the Docker container name by following the steps: List all containers (docker ps). Filter the container list (using the grep command). Gets the container name (located in the "NAMES" column).

How to start containers by docker How to start containers by docker Apr 15, 2025 pm 12:27 PM

Docker container startup steps: Pull the container image: Run "docker pull [mirror name]". Create a container: Use "docker create [options] [mirror name] [commands and parameters]". Start the container: Execute "docker start [Container name or ID]". Check container status: Verify that the container is running with "docker ps".

How to create containers for docker How to create containers for docker Apr 15, 2025 pm 12:18 PM

Create a container in Docker: 1. Pull the image: docker pull [mirror name] 2. Create a container: docker run [Options] [mirror name] [Command] 3. Start the container: docker start [Container name]

How to check nginx version How to check nginx version Apr 14, 2025 am 11:57 AM

The methods that can query the Nginx version are: use the nginx -v command; view the version directive in the nginx.conf file; open the Nginx error page and view the page title.

How to configure cloud server domain name in nginx How to configure cloud server domain name in nginx Apr 14, 2025 pm 12:18 PM

How to configure an Nginx domain name on a cloud server: Create an A record pointing to the public IP address of the cloud server. Add virtual host blocks in the Nginx configuration file, specifying the listening port, domain name, and website root directory. Restart Nginx to apply the changes. Access the domain name test configuration. Other notes: Install the SSL certificate to enable HTTPS, ensure that the firewall allows port 80 traffic, and wait for DNS resolution to take effect.

What to do if nginx server is hung What to do if nginx server is hung Apr 14, 2025 am 11:42 AM

When the Nginx server goes down, you can perform the following troubleshooting steps: Check that the nginx process is running. View the error log for error messages. Check the syntax of nginx configuration. Make sure nginx has the permissions you need to access the file. Check file descriptor to open limits. Confirm that nginx is listening on the correct port. Add firewall rules to allow nginx traffic. Check reverse proxy settings, including backend server availability. For further assistance, please contact technical support.

See all articles