LNMP服务

部署Nginx软件
1)安装支持软件:
Nginx的配置及运行需要pcre、zlib等软件包的支持,因此应预先安装这些软件的开发包(devel),以便提供相应的库和头文件,确保Nginx的安装顺利完成。
[root@nginx~]#systemctl stop firewalld
[root@nginx~]#iptables-F
[root@nginx~]#setenforce 0

[root@nginx~]# yum -y install pcre-devel zlib-devel openssl-devel gcc gcc-c++ make
2)创建运行用户、组:
Nginx服务程序默认以nobody身份运行,建议为其创建专门的用户账号,以便更准确地控制其访问权限,增加灵活性、降低安全风险。如:创建一个名为nginx的用户,不建立宿主目录,也禁止登录到shell环境。
[root@nginx~]#useradd -M -s /sbin/nologin nginx

3)编译安装nginx:
释放nginx源码包
[root@nginx~]# tar xf nginx-1.14.2.tar.gz -C /usr/src/

4)配置编译:
[root@nginx~]# cd /usr/src/nginx-1.14.2/
[[email protected]]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module && make && make install

⦁ --prefix 设定Nginx的安装目录
⦁ --user和--group 指定Nginx运行用户和组
⦁ --with-http_stub_status_module 启用http_stub_status_module模块以支持状态统计
⦁ --with-http_ssl_module 启用SSL模块
⦁ --with-http_flv_module 启用FLV模块,提供寻求内存使用基于时间的偏移量文件

为了使Nginx服务器的运行更加方便,可以为主程序nginx创建链接文件,以便管理员直接执行nginx命令就可以调用Nginx的主程序。
[[email protected]]# ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/
[[email protected]]#ll /usr/local/bin/nginx
lrwxrwxrwx 1 root root 27 12-29 07:24 /usr/local/bin/nginx -> /usr/local/nginx/sbin/nginx

5)Nginx的运行控制:
查看语法
[root@nginxconf]#nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

6)启动、停止Nginx:
直接运行nginx即可启动Nginx服务器,这种方式将使用默认的配置文件,若要改用其他配置文件,需添加"-c 配置文件路径"选项来指定路径。需要注意的是,若服务器中已安装有httpd等其他WEB服务软件,应采取措施(修改端口,停用或卸载其他软件)避免部突。
[root@nginxconf]# netstat -anpt |grep :80
[root@nginxconf]#nginx
[root@nginxconf]# netstat -anpt |grep :80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 6810/nginx: master

通过检查 Nginx程序的监听状态,或者在浏览器中访问此WEB服务(默认页面将显示"Welcome to nginx!"),可以确认Nginx服务是否正常运行。

主程序Nginx支持标准的进程信号,通过kill或者killall命令传送
⦁ HUP 重载配置 等同于-1
⦁ QUIT 退出进程 等同于-3
⦁ KILL 杀死进程 等同于-9
[root@nginx~]#killall -s HUP nginx
[root@nginx~]#killall -s QUIT nginx
[root@nginx~]# netstat -anpt |grep :80

当Nginx进程运行时,PID号默认存放在/usr/local/nginx/logs/目录下的nginx.pid文件中,因此若改用kill命令,也可以根据nginx.pid文件中的PID号来进行控制。

为了使Nginx服务的启动、停止、重载等操作更加方便,可以编写Nginx服务脚本,并使用chkconfig和systemctl工具来进行管理,也更加符合RHEL系统的管理习惯。
[root@nginx~]# vim /etc/init.d/nginx
#!/bin/bash
# chkconfig: 2345 99 20
# description: Nginx Server Control Script
PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"

扫描二维码关注公众号,回复: 7270950 查看本文章

case "$1" in
start)
$PROG
;;
stop)
kill -s QUIT $(cat $PIDF)
;;
restart)
$0 stop
$0 start
;;
reload)
kill -s HUP $(cat $PIDF)
;;
*)
echo "Usage: $0 {start|stop|restart|reload}"
exit 1
esac
exit 0

[root@nginx~]#chmod +x /etc/init.d/nginx
[root@nginx~]#chkconfig --add nginx
[root@nginx~]#chkconfignginx on
[root@nginx~]#chkconfig --list nginx
nginx 0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启用 6:关闭

这样就可以通过nginx脚本来启动、停止、重启、重载Nginx服务器了。
nginx.conf文件结构
在Nginx服务器的主配置文件nginx.conf中,包括全局配置、I/O事件配置、HTTP配置这三大块内容,配置语句的格式为"关键字 值;"(末尾以分号表示结束),以"#"开始的部分表示注释。

1)全局配置
由各种配置语句组成,不使用特定的界定标记。全局配置部分包括运行用户、工作进程数、错误日志、PID存放位置等基本设置。
常用配置项:
⦁ user nginx [nginx]; //运行用户,Nginx的运行用户实际是编译时指定的nginx,若编译时未指定则默认为nobody。
⦁ worker_processes 2;//指定nginx启动的工作进程数量,建议按照cpu数目来指定,一般和CPU核心数相等。
⦁ worker_cpu_affinity 00000001 00000010; //为每个进程分配cpu核心,上例中将2个进程分配到两个cpu,当然可以写多个,或者将一个进程分配到多个cpu
⦁ worker_rlimit_nofile 102400; //这个指令是指当一个nginx进程打开的最多文件数目,理论值应该是最多打开文件数(ulimit-n)与nginx进程数相除,但是nginx分配请求并不是那么均匀,所以最好与ulimit -n的值保持一致。(通过"ulimit–n 数值"可以修改打开的最多文件数目)
⦁ error_log logs/error.log; //全局错误日志文件的位置
⦁ pid logs/nginx.pid; //PID文件的位置

2)I/O事件配置:
使用"events {}"界定标记,用来指定Nginx进程的I/O响应模型,每个进程的连接数等设置
events {
//使用epoll模型,对于2.6以上的内核,建议使用epoll模型以提高性能
worker_connections 4096; //每个进程允许的最多连接数(默认为1024),每个进程的连接数应根据实际需要来定,一般在10000以下,理论上每台nginx服务器的最大连接数为worker_processes*worker_connections,具体还要看服务器的硬件、带宽等。
}

3)HTTP配置
使用"http{}"界定标记,包括访问日志、HTTP端口、网页目录、默认字符集、连接保持、以
及虚拟主机、PHP解析等一系列设置。其中大部分配置语句包含在子界定标记"server {}"内。
http {
include mime.types;
default_type application/octet-stream;

log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';

access_log logs/access.log main; //访问日志位
sendfile on; //支持文件发送(下载)
keepalive_timeout 65; //连接保持超时

server { //web服务的监听配置
listen 80; //监听地址及端口(IP:PORT)
server_name www.crushlinux.com; //网站名称(FQDN)
charset utf-8; //网页的默认字符集

location / { //跟目录配置
root html; //网站根目录的位置安装位置的html中
index index.html index.htm; //默认首页(索引页)
}

error_page 500 502 503 504 /50x.html; //内部错误的反馈页面
location = /50x.html { //错误页面配置
root html;
}


状态统计模块
Nginx内置了HTTP_STUB_STATUS状态统计模块,用来反馈当前的WEB访问情况。配置
编译参数时可添加--with-http_stub_stastus_module来启用此模块。要使用Nginx的状态统计功能,除了启用内建模块以外,还需要修改nginx.conf文件,指定访问位置并打开stub_status配置。在http{}配置的server{}子配置内添加如下配置项
[root@nginx~]# vim /usr/local/nginx/conf/nginx.conf
location /status {
stub_status on; //打开状态统计功能
access_log off; //关闭此位置的日志记录
}
[root@nginxconf]#systemctl restart nginx

浏览器访问 http://192.168.200.111/status

Active connections 表示当前活跃的连接数,
第三行的三个数字表示Nginx当前总共处理了3个连接,成功创建3次握手,总共处理了12个请求。
Reading表示Nginx读取到客户端Header信息数,
Writing表示Nginx返回给客户端的Header信息数
Waiting表示Nginx已经处理完,正在等候下一次请求指令时的驻留连接数。
虚拟主机应用
使用Nginx搭建虚拟主机服务器时,每个虚拟WEB站点拥有独立的"server {}"配置段,各自监听的IP地址、端口号可以单独指定,当然网站名称也是不同的。

例如:要创建两个站点www.crushlinux.com和www.cloud.com
为两个虚拟WEB主机分别建立根目录,并准备测试首页
[root@nginx~]#mkdir /usr/local/nginx/html/crushlinux
[root@nginx~]#mkdir /usr/local/nginx/html/cloud
[root@nginx~]# echo "<h1>www.crushlinux.com</h1>" >/usr/local/nginx/html/crushlinux/index.html
[root@nginx~]# echo "<h1>www.cloud.com</h1>" > /usr/local/nginx/html/cloud/index.html
[root@nginx~]# vim /usr/local/nginx/conf/nginx.conf
http {
include mime.types;
default_type application/octet-stream;

log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';

access_log logs/access.log main;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name www.crushlinux.com;
charset utf-8;
access_log logs/crushlinux.access.log main;

location / {
root html/crushlinux;
index index.html index.htm;
}
}

server {
listen 80;
server_name www.cloud.com;
charset utf-8;
access_log logs/cloud.access.log main;

location / {
root html/cloud;
index index.html index.htm;
}
}
}

[root@nginx~]#systemctl restart nginx

[root@nginx~]# vim /etc/hosts
192.168.200.111 www.crushlinux.com
192.168.200.111 www.cloud.com

虚拟主机访问测试
[root@nginx~]#elinks --dump http://www.crushlinux.com
www.crushlinux.com
[root@nginx~]#elinks --dump http://www.cloud.com
www.cloud.com


LNMP架构及应用部署
LNMP也称为LEMP,E来自于Nginx的发音[engine x]
LNMP就像构建LAMP平台一样,构建LNMP平台也需要用到Linux服务器,MySQL数据库,PHP解释环境等应用。P(PHP、Perl、Python)
wamp是什么?xampp
                                                                                                                            两个方法
二进制安装mysql
安装命令
[root@localhost ~]# yum -y install libaio
二进制软件包
root@localhost ~]# rz
[root@localhost ~]# ls
anaconda-ks.cfg nginx-1.14.2.tar.gz 模板 图片 下载 桌面
mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz 公共 视频 文档 音乐
解压到local
[root@localhost ~]# tar xf mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
进入到解压目录下
[root@localhost ~]# cd /usr/local/
用mv命令改个名字叫mysql
[root@localhost local]# mv mysql-5.7.24-linux-glibc2.12-x86_64/ mysql
创建用户合组
[root@localhost local]# useradd -M -s /sbin/nologin mysql
修改当前目录的拥有者为新建的mysql
[root@localhost local]# chown -R mysql:mysql /usr/local/mysql/
初始化mysql记录密码
[root@localhost local]# /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysql --initialize
修改配置文件
[root@localhost local]# cat /etc/my.cnf
[mysqld]
datadir=/usr/local/mysql/data
socket=/tmp/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
[mysqld_safe]
log-error=/usr/local/mysql/data/mysql.log
pid-file=/usr/local/mysql/data/mysql.pid

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
[root@localhost local]# cp mysql/support-files/mysql.server /etc/init.d/mysqld
加上执行权限
[root@localhost local]# chmod +x /etc/init.d/mysqld
加入到系统服务里 以后用systemctl启动
[root@localhost local]# chkconfig --add mysqld
[root@localhost local]# systemctl start mysqld
进入到mysql 容易出错 做一个软连接
[root@localhost local]# ln -s /usr/local/mysql/bin/* /bin/
修改密码
[root@localhost ~]# mysqladmin -uroot -p'tNih<t:a%9:X' password 123
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.
登入到数据库
[root@localhost ~]# mysql -uroot -p123
                                                                               忘记密码
[root@localhost ~]# systemctl stop mysqld
[root@localhost ~]# ps aux |grep mysqld
进入数据库
[root@localhost ~]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.24 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>
查看授权表
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.01 sec)
进入数据库
mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;
mysql> desc user;
mysql> update user set authentication_string=PASSWORD('123123') where user='root';
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 1
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> exit
Bye
[root@localhost ~]# ps aux |grep mysqld
[root@localhost ~]# kill -9 49482
[root@localhost ~]# kill -9 49340
[root@localhost ~]# kill -9 49726
[root@localhost ~]# ps aux |grep mysqld
root 49796 0.0 0.0 112656 972 pts/0 R+ 11:30 0:00 grep --color=auto mysqld
[root@localhost ~]# systemctl start mysqld
[root@localhost ~]# mysql -uroot -p123123
1、安装Mysql数据库
1)基于源码包安装MySQL
[root@nginx~]# rpm -q mysqlmysql-servermariadbmairadb-server
ncurses-devel是cmake的依赖包
[root@nginx~]# yum -y install ncurses-devel
[root@nginx~]# rpm -q ncurses-devel
ncurses-devel-5.9-14.20130511.el7_4.x86_64

建议采用yum安装方式
[root@nginx~]# yum -y install cmake
[root@nginx~]# rpm -q cmake
cmake-2.8.12.2-2.el7.x86_64

创建运行用户
[root@nginx~]#useradd -M -s /sbin/nologin mysql

解包,配置,编译,安装
[root@nginx~]# tar xf mysql-5.7.24.tar.gz -C /usr/src/
[root@nginx~]# cd /usr/src/mysql-5.7.24/
[root@nginx mysql-5.7.24]#cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS=all -DSYSCONFDIR=/etc&& make && make install

⦁ -DCMAKE_INSTALL_PREFIX=/usr/local/mysql //数据库程序安装目录
⦁ -DDEFAULT_CHARSET=utf8 //指定字符集编码
⦁ -DDEFAULT_COLLATION=utf8_general_ci //默认的字符集校对规则,utf8_general_ci适用于utf-8字符集的通用规则
⦁ -DWITH_EXTRA_CHARSETS=all //指定额外支持的字符集编码
⦁ -DSYSCONFDIR=/etc //指定配置文件存放目录

报错处理:
------------------------------------------------------------------------------CMake Error at cmake/boost.cmake:81 (MESSAGE):
You can download it with -DDOWNLOAD_BOOST=1 -DWITH_BOOST=<directory>

This CMake script will look for boost in <directory>. If it is not there,
it will download and unpack it (in that directory) for you.

If you are inside a firewall, you may need to use an http proxy:

export http_proxy=http://example.com:80

Call Stack (most recent call first):
cmake/boost.cmake:238 (COULD_NOT_FIND_BOOST)
CMakeLists.txt:507 (INCLUDE)


-- Configuring incomplete, errors occurred!
See also "/usr/src/mysql-5.7.24/CMakeFiles/CMakeOutput.log".
See also "/usr/src/mysql-5.7.24/CMakeFiles/CMakeError.log".
------------------------------------------------------------------------------

解决办法:
a.在/usr/local下创建一个名为boost的文件夹
[root@nginx~]#mkdir /usr/local/boost

b.进入目录并下载boost
[root@nginx~]# cd /usr/local/boost
[root@nginxboost]#wget https://sourceforge.net/projects/boost/files/boost/1.59.0/boost_1_59_0.tar.gz

c.解压boost
[root@nginxboost]# tar xf boost_1_59_0.tar.gz

d.继续cmake,添加上红色部分  复制时注意空格
[root@nginx mysql-5.7.24]#cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS=all -DSYSCONFDIR=/etc -DWITH_BOOST=/usr/local/boost && make && make install

2)安装后的调整
对数据库目录进行权限设置
[root@nginx~]# cd /usr/local/mysql/
[root@nginxmysql]#chown -R mysql:mysql ./

建立配置文件(CentOS7系统默认支持MariaDB数据库,系统默认的/etc/my.cnf配置文件是MariaDB的配置文件 )
[root@nginxmysql]# vim /etc/my.cnf
[mysqld]
datadir=/usr/local/mysql/data
socket=/tmp/mysql.sock

[mysqld_safe]
log-error=/usr/local/mysql/data/mysql.log
pid-file=/usr/local/mysql/data/mysql.pid

3)初始化数据库
[root@nginxmysql]# ./bin/mysqld --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --initialize
2018-12-08T01:51:39.798903Z 1 [Note] A temporary password is generated for root@nginx: TvC:Rm1ZlxtG
⦁ --basedir=/usr/local/mysql/ //指定安装目录(产品目录)
⦁ --datadir=/usr/local/mysql/data //指定数据目录
⦁ --user=mysql //指定用户身份

4)设置环境变量
[root@nginx mysql-5.7.24]# echo "PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile
[root@nginx mysql-5.7.24]# source /etc/profile

5)添加系统服务
添加MySQL为系统服务,以便通过systemctl命令进行管理
[root@nginx mysql-5.7.24]# cp support-files/mysql.server /etc/init.d/mysqld
[root@nginx mysql-5.7.24]#chmod +x /etc/init.d/mysqld
/etc/init.d/mysqld start
[root@nginx~]# netstat -lnpt | grep mysqld
tcp6 0 0 :::3306 :::* LISTEN 2520/mysqld
后期修改数据库用户的密码:
[root@nginx~]#mysqladmin -u root -p 'TvC:Rm1ZlxtG' password '123456'

2、安装PHP解析环境
较新版本(如5.6)的PHP已经自带FPM(fastCGI process manager,FastCGI进程管理器)模块,用来对PHP解析实例进行管理,优化解析效率,因此在配置PHP编译选项时应添加"--enable-fpm"以启用此模块。

为了提高PHP解析效率,建议将相应版本的ZendGuardLander也装上。

1)编译安装php
[root@nginx~]# yum -y install gd libxml2-devel libjpeg-devel libpng-devel
[root@nginx~]# tar xf php-5.6.39.tar.gz -C /usr/src/
[root@nginx~]# cd /usr/src/php-5.6.39/
[[email protected]]# ./configure --prefix=/usr/local/php5 --with-gd --with-zlib --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-config-file-path=/usr/local/php5 --enable-mbstring --enable-fpm --with-jpeg-dir=/usr/lib && make && make install

2)安装后的调整
[[email protected]]#cp php.ini-production /usr/local/php5/php.ini
[[email protected]]# ln -s /usr/local/php5/bin/* /usr/local/bin/
[[email protected]]# ln -s /usr/local/php5/sbin/* /usr/local/sbin/

3)安装ZendGuardLander(注意:若是64位系统,该软件得到其官网下载64位的相应软件包,若用32位的就会报错。下载地址:http://www.zend.com/en/products/guard/downloads

[root@nginx~]# tar xf zend-loader-php5.6-linux-x86_64_update1.tar.gz -C /usr/src/
[root@nginx~]# cd /usr/src/zend-loader-php5.6-linux-x86_64/
[root@nginx zend-loader-php5.6-linux-x86_64]# cp ZendGuardLoader.so /usr/local/php5/lib/php/

[root@nginx zend-loader-php5.6-linux-x86_64]#vim /usr/local/php5/php.ini
zend_extension=/usr/local/php5/lib/php/ZendGuardLoader.so
zend_loader.enable=1
3、配置Nginx支持PHP环境
若要让Nginx能够解析PHP网页,有两种方法可以选择;其一:Nginx充当中介,将访问PHP页面的WEB请求转交给其他服务器(LAMP)去处理;其二:通过使用PHP的FPM模块来调用本机的PHP环境。

如果选用FPM方式,则需要先启动php-fpm进程,以便监听PHP解析请求。参考范例建立php-fpm.conf配置文件,并修改其中的PID文件、运行用户、服务数(进程数量)等相关设置,然后启动php-fpm程序即可(默认监听本机的9000端口)
[root@nginx~]# cd /usr/local/php5/etc/
[root@nginxetc]# cp php-fpm.conf.default php-fpm.conf
[root@nginxetc]#useradd -M -s /sbin/nologin php
[root@nginxetc]# vim php-fpm.con
25 pid = run/php-fpm.pid //确认pid文件位置
149 user = php //运行用户
150 group = php //运行组
246pm.start_servers = 20 //启动时开启的进程数
251pm.min_spare_servers = 5 //最少空闲进程数
256pm.max_spare_servers = 35 //最大空闲进程数cd
241pm.max_children = 50 //最多空闲进程数

[root@nginxetc]# /usr/local/sbin/php-fpm
[root@nginxetc]# netstat -anpt |grep php-fpm
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 23027/php-fpm.conf)

在php-fpm.conf文件中,pid配置行指出了PID信息的存放位置,对应的实际路径为/usr/local/php5/var/run/php-fpm.pid,根据上述信息,可以修改Nginx服务脚本,以便在启动/停止Nginx服务器时将php-fpm进程也自动启动/停止。
[root@nginxetc]# vim /etc/init.d/nginx
#!/bin/bash
# chkconfig: 2345 99 20
# description: Nginx Server Control Script
PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"
PROG_FPM="/usr/local/sbin/php-fpm"
PIDF_FPM="/usr/local/php5/var/run/php-fpm.pid"
case "$1" in
start)
$PROG
$PROG_FPM
;;
stop)
kill -s QUIT $(cat $PIDF)
kill -s QUIT $(cat $PIDF_FPM)
;;
restart)
$0 stop
$0 start
;;
reload)
kill -s HUP $(cat $PIDF)
;;
*)
echo "Usage: $0 (start|stop|restart|reload)"
exit 1
esac
exit 0

这样,一旦启动或关闭nginx服务,php-fpm程序也会随之启动或关闭,不需要额外再启动或关闭php-fpm.

配置Nginx支持PHP解析:
无论是将PHP页面交给LAMP服务器去解析,还是调用本机的php-fpm进程进行解析,都需要在"server{}"配置段中添加location设置,以便指定当访问.php面页时采取何种操作。
对于第一种方法使用Nginx的反向代理功能(转交给其他WEB服务器处理),使用的配置语句如下所示,例如,交给IP地址为192.168.200.112的LAMP服务器处理,从而实现由Nginx负责静态页面,LAMP负责动态页面的分离效果。
[root@nginxetc]# vim /usr/local/nginx/conf/nginx.conf
server {
……
location ~ \.php$ { //访问.php页面的配置段
proxy_passhttp://192.168.200. HYPERLINK "http://192.168.200.112/" HYPERLINK "http://192.168.200.112/" HYPERLINK "http://192.168.200.112/" HYPERLINK "http://192.168.200.112/" HYPERLINK "http://192.168.200.112/" HYPERLINK "http://192.168.200.112/" HYPERLINK "http://192.168.200.112/"112 HYPERLINK "http://192.168.200.112/" HYPERLINK "http://192.168.200.112/" HYPERLINK "http://192.168.200.112/" HYPERLINK "http://192.168.200.112/" HYPERLINK "http://192.168.200.112/" HYPERLINK "http://192.168.200.112/" HYPERLINK "http://192.168.200.112/":80; //LAMP服务器的监听地址
}
}
Nginx 服务器的反向代理服务是其最常用的重要功能,由反向代理服务也可以衍生出很多与此相关的 Nginx 服务器重要功能,比如后面会介绍的负载均衡。当然在了解反向代理之前,我们需要先知道什么是代理以及什么是正向代理。
1、代理
  在Java设计模式中,代理模式是这样定义的:给某个对象提供一个代理对象,并由代理对象控制原对象的引用。
  可能大家不太明白这句话,在举一个现实生活中的例子:比如我们要买一间二手房,虽然我们可以自己去找房源,但是这太花费时间精力了,而且房屋质量检测以及房屋过户等一系列手续也都得我们去办,再说现在这个社会,等我们找到房源,说不定房子都已经涨价了,那么怎么办呢?最简单快捷的方法就是找二手房中介公司(为什么?别人那里房源多啊),于是我们就委托中介公司来给我找合适的房子,以及后续的质量检测过户等操作,我们只需要选好自己想要的房子,然后交钱就行了。
  代理简单来说,就是如果我们想做什么,但又不想直接去做,那么这时候就找另外一个人帮我们去做。那么这个例子里面的中介公司就是给我们做代理服务的,我们委托中介公司帮我们找房子。
  Nginx 主要能够代理如下几种协议,其中用到的最多的就是做Http代理服务器。

2、正向代理
  弄清楚什么是代理了,那么什么又是正向代理呢?
  这里我再举一个例子:大家都知道,现在国内是访问不了 Google的,那么怎么才能访问 Google呢?我们又想,美国人不是能访问 Google吗(这不废话,Google就是美国的),如果我们电脑的对外公网 IP 地址能变成美国的 IP 地址,那不就可以访问 Google了。你很聪明,VPN 就是这样产生的。我们在访问 Google 时,先连上 VPN 服务器将我们的 IP 地址变成美国的 IP 地址,然后就可以顺利的访问了。
  这里的 VPN 就是做正向代理的。正向代理服务器位于客户端和服务器之间,为了向服务器获取数据,客户端要向代理服务器发送一个请求,并指定目标服务器,代理服务器将目标服务器返回的数据转交给客户端。这里客户端是要进行一些正向代理的设置的。
  PS:这里介绍一下什么是 VPN,VPN 通俗的讲就是一种中转服务,当我们电脑接入 VPN 后,我们对外 IP 地址就会变成 VPN 服务器的公网 IP,我们请求或接受任何数据都会通过这个VPN 服务器然后传入到我们本机。这样做有什么好处呢?比如 VPN 游戏加速方面的原理,我们要玩网通区的 LOL,但是本机接入的是电信的宽带,玩网通区的会比较卡,这时候就利用 VPN 将电信网络变为网通网络,然后在玩网通区的LOL就不会卡了(注意:VPN 是不能增加带宽的,不要以为不卡了是因为网速提升了)。
  可能听到这里大家还是很抽象,没关系,和下面的反向代理对比理解就简单了。
3、反向代理
  反向代理和正向代理的区别就是:正向代理代理客户端,反向代理代理服务器。
反向代理,其实客户端对代理是无感知的,因为客户端不需要任何配置就可以访问,我们只需要将请求发送到反向代理服务器,由反向代理服务器去选择目标服务器获取数据后,在返回给客户端,此时反向代理服务器和目标服务器对外就是一个服务器,暴露的是代理服务器地址,隐藏了真实服务器IP地址。
  下面我们通过两张图来对比正向代理和方向代理:


  理解这两种代理的关键在于代理服务器所代理的对象是什么,正向代理代理的是客户端,我们需要在客户端进行一些代理的设置。而反向代理代理的是服务器,作为客户端的我们是无法感知到服务器的真实存在的。
  总结起来还是一句话:正向代理代理客户端,反向代理代理服务器。
对于第二种方法(调用本机的php-fpm进程),使用的配置语句如下所示。在conf/目录下的fastcgi.conf文件中已经包含必需的宏设置,可通过include语句添加进来。
[root@nginxetc]# vim /usr/local/nginx/conf/nginx.conf
server {
…… //省略部分信息
location / {
root html;
index index.php index.html index.htm;
}
location ~ \.php$ { //访问php页面的配置段
root html; //PHP网页文档根目录
fastcgi_pass 127.0.0.1:9000; //php-fpm的监听地址
fastcgi_indexindex.php; //PHP首页文件
include fastcgi.conf; //包括fastcgi.conf样本配置
}
}
完整文档

本文档中我选择的是第二种方法,完成修改后重新加载nginx服务即可生效。
[root@nginxetc]#killall -HUP nginx

在PHP文档根目录下创建一个测试网页,以便测试PHP语名能否正常解析,以及能否访问MYSQL数据库。测试页内容如下:
[root@nginx~]# cat /usr/local/nginx/html/php.php
<?php
phpinfo();
?>

[root@nginxetc]# vim /usr/local/nginx/html/mysql.php
<?php
$link=mysqli_connect('localhost','root','123456'); //连接mysql数据库
if($link) echo "<h1>恭喜你,大功告成!!</h1>"; //连接成功则返回信息
mysqli_close(); //关闭数据库连接
?>

4、LNMP平台中部署WEB应用
LNMP平台与LAMP平台是非常相似的,区别主要在于所用WEB服务软件的不同,而这与使用PHP开发的WEB应用程序并无太大关系,因此PHP应用的部署方法也是类似的。

部署ComsenzDiscuz BBS论坛系统
Discuz!社区论坛是一个采用PHP与MySQL等多种数据库构建的性能优异,功能全面的且安全稳定的社区论坛(BBS)软件,其官方网站为http://www.discuz.net
[root@nginx~]# unzip ComsenzDiscuz-DiscuzX-master.zip
[root@nginx~]# cd DiscuzX/
[root@nginxDiscuzX]# ls upload/
admin.phpapi.php config crossdomain.xml favicon.ico group.phpindex.php m misc.phpportal.phpsearch.php static uc_client
api archiver connect.php data forum.phphome.php install member.phpplugin.php robots.txt source template uc_server

上传bbs代码:
[root@nginxDiscuzX]# mv upload/ /usr/local/nginx/html/bbs

浏览器访问http://192.168.200.111/bbs/install/index.php

设置权限及模板文件
[root@nginx~]# cd /usr/local/nginx/html/bbs/config/
[root@nginxconfig]# cp config_global_default.php config_global.php
[root@nginxconfig]# cp config_ucenter_default.php config_ucenter.php

[root@nginx~]# cd /usr/local/nginx/html/bbs
[root@nginxbbs]#chmod -R 777 config/ data/ uc_client/ uc_server/


准备数据库并配置相关授权
[root@nginx~]#mysql -uroot -p123456
mysql> create database bbs;
Query OK, 1 row affected (0.00 sec)

mysql> grant all on bbs.* to 'bbs'@'localhost' identified by 'bbs123456';
Query OK, 0 rows affected, 1 warning (0.07 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)



[root@nginx~]# vim /usr/local/nginx/conf/nginx.conf
user nginxnginx;
worker_processes 2;
error_log logs/error.log;
pid logs/nginx.pid;
worker_rlimit_nofile 102400;

events {
use epoll;
worker_connections 4096;
}


http {
include mime.types;
default_type application/octet-stream;

log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';

access_log logs/access.log main;
sendfile on;
keepalive_timeout 65;

server {
listen 80;
server_name localhost;
charset utf-8;

location / {
root html/bbs;
index index.php index.html index.htm;
}

location ~ \.php$ {
root html/bbs;
fastcgi_pass 127.0.0.1:9000;
fastcgi_indexindex.php;
include fastcgi.conf;
}

location /status {
stub_status on;
access_log off;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
[root@nginxetc]#killall -HUP nginx

浏览器访问h HYPERLINK "http://192.168.200.111/" HYPERLINK "http://192.168.200.111/" HYPERLINK "http://192.168.200.111/" HYPERLINK "http://192.168.200.111/" HYPERLINK "http://192.168.200.111/" HYPERLINK "http://192.168.200.111/" HYPERLINK "http://192.168.200.111/"ttp://192.168.200.111

猜你喜欢

转载自www.cnblogs.com/liu1584712/p/11516540.html
今日推荐