Nginx实现web架构

企业中网站架构

LNMP:Linux Nginx MySQL PHP
LAMP:Linux Apache MySQL PHP
LNMT:Linux Nginx MySQL Tomcat
LAMT:Linux Apache MySQL Tomcat


Nginx
Apache
运行:html css js

PHP:运行php代码
Tomcat:运行Java代码


nginx
php

location优先级

匹配符

匹配规则

优先级

=

精确匹配

1

^~

以某个字符串开头

2

~

区分大小写的正则匹配

3

~*

不区分大小写的正则匹配

4

!~

区分大小写不匹配的正则

5

!~*

不区分大小写不匹配的正则

6

/

通用匹配,任何请求都会匹配到

7

部署PHP

# 1.卸载Linux自带的旧版本php
[root@web01 ~]# yum remove php-mysql-5.4 php php-fpm php-common

# 2.添加php第三方源
[root@nginx ~]# vim /etc/yum.repos.d/php.repo
[php-webtatic]
name = PHP Repository
baseurl = http://us-east.repo.webtatic.com/yum/el7/x86_64/
gpgcheck = 0

# 3.安装php
[root@web01 ~]# yum -y install php71w php71w-cli php71w-common php71w-devel php71w-embedded php71w-gd php71w-mcrypt php71w-mbstring php71w-pdo php71w-xml php71w-fpm php71w-mysqlnd php71w-opcache php71w-pecl-memcached php71w-pecl-redis php71w-pecl-mongodb

# 4.创建用户
[root@web01 ~]# groupadd www -g 666
[root@web01 ~]# useradd www -u 666 -g 666 -s /sbin/nologin -M


# 5.修改nginx运行用户
[root@web01 ~]# vim /etc/nginx/nginx.conf
user www;
php-fpm作用:用来管理php程序运行
## php相关配置文件
/etc/php-fpm.conf # php管理进程配置文件
/etc/php.ini # php程序配置文件
/etc/php-fpm.d/www.conf # php管理进程的子配置文件

# 6.修改php的启动用户
[root@web01 ~]# vim /etc/php-fpm.d/www.conf
[www]
user = www
group = www

# 7.启动php并加入开机自启
[root@web01 ~]# systemctl start php-fpm
[root@web01 ~]# systemctl enable php-fpm

# 8.检查php进程和端口
[root@web01 ~]# ps -ef|grep php
root 8163 1 0 19:45 ? 00:00:00 php-fpm: master process (/etcphp-fpm.conf)
www 8164 8163 0 19:45 ? 00:00:00 php-fpm: pool www
www 8165 8163 0 19:45 ? 00:00:00 php-fpm: pool www
www 8166 8163 0 19:45 ? 00:00:00 php-fpm: pool www
www 8167 8163 0 19:45 ? 00:00:00 php-fpm: pool www
www 8168 8163 0 19:45 ? 00:00:00 php-fpm: pool www
root 8214 7212 0 19:49 pts/0 00:00:00 grep --color=auto php

[root@web01 ~]# netstat -lntup|grep php
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 8163/php-fpm: maste

配置nginx连接php

[root@web01 conf.d]# cat movie.conf 
server{
listen 80;
server_name movie.wls.com;

location /{
root /movie;
index index.php index.html;
}

location ~ \.php$ {
root /movie;
## nginx调用本机的9000端口(php-fpm程序)
fastcgi_pass 127.0.0.1:9000;
## 用php程序,解析哪个目录下的哪个.php的文件
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
## 包含php变量的文件
include /etc/nginx/fastcgi_params;
}
}

##更改站点目录权限
[root@web01 movie]# chown www.www /movie/


## 浏览器上传文件查看是否生成 user_data 目录 存放你的上传的文件
[root@web01 movie]# ll
total 80
-rw-r--r-- 1 root root 38772 Apr 27 2018 bg.jpg
-rw-r--r-- 1 root root 2633 May 4 2018 index.html
-rw-r--r-- 1 root root 52 May 10 2018 info.php
-rw-r--r-- 1 root root 27020 Jun 7 15:53 kaoshi_modify.zip
-rw-r--r-- 1 root root 1117 May 18 11:52 upload_file.php
drwxr-xr-x 2 www www 28 Jun 8 00:07 user_data

[root@web01 movie]# ll user_data/
total 76
-rw-r--r-- 1 www www 76653 Jun 8 00:07 33_wjh.jpg.jpg

架构(day11)_nginx

部署wordpress

# 1.编辑nginx配置文件
[root@web01 ~]# vim /etc/nginx/conf.d/blog.wjh.com.conf
server{
listen 80;
server_name blog.wjh.com;

location /{
root /blog;
index index.php index.html;
}

location ~ \.php$ {
root /blog;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}
}


# 2 重新加载nginx
[root@web01 conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 conf.d]# systemctl reload nginx

# 3.创建站点目录并授权
[root@web01 ~]# mkdir /blog/wordpress
[root@web01 wordpress]# chown -R www.www /blog/wordpress/

# 4.测试nginx连接php(编写php info代码)
[root@web01 ~]# vim /blog/info.php
<?php
phpinfo();
?>
# 5.windows域名解析
打开路径:C:\Windows\System32\drivers\etc 编辑hosts文件
10.0.0.7 blog.wjh.com

# 6.打开浏览器访问:http://blog.wjh.com/info.php

架构(day11)_php_02

# 7.下载wordpress代码
wordpress官网:https://wordpress.org/
[root@web01 blog]# wget https://cn.wordpress.org/latest-zh_CN.tar.gz

架构(day11)_php_03

架构(day11)_php_04

# 8.解压代码
[root@web01 blog]# tar xf latest-zh_CN.tar.gz
# 9.修改nginx配置文件
[root@web01 blog]# vim /etc/nginx/conf.d/blog.zls.com.conf
[root@web01 themes]# cat /etc/nginx/conf.d/blog.wjh.com.conf
server{
listen 80;
server_name blog.wjh.com;
root /blog/wordpress;

location /{
index index.php index.html;
}

location ~ \.php$ {
## nginx调用本机的9000端口(php-fpm程序)
fastcgi_pass 127.0.0.1:9000;
## 用php程序,解析哪个目录下的哪个.php的文件
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
## 包含php变量的文件
include /etc/nginx/fastcgi_params;
}
}

# 10.重新加载nginx
[root@web01 blog]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 blog]# systemctl reload nginx
# 11.打开浏览器,访问:http://blog.wjh.com/

架构(day11)_mysql_05

架构(day11)_mysql_06

安装数据库

## 数据库是C/S结构
## 默认端口:3306

# 1.安装mariadb
[root@web01 ~]# yum install -y mariadb-server

# 2.启动数据库并加入开机自启
[root@web01 ~]# systemctl start mariadb
[root@web01 ~]# systemctl enable mariadb

# 3.登录数据库
[root@web01 themes]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 98
Server version: 5.5.68-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

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

MariaDB [(none)]>

# 4.查看所有库
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test
+--------------------+

# 5.切换数据库
MariaDB [(none)]> use mysql

# 6.查看该库中的所有表
MariaDB [mysql]> show tables;

# 7.创建数据库
MariaDB [mysql]> create database 库名字;
MariaDB [mysql]> create database wordpress;

# 8.创建用户
MariaDB [(none)]> grant all on 所有库.所有表 to 用户名@'主机IP' identified by '密码';
MariaDB [(none)]> grant all on *.* to wp@'localhost' identified by '123';

# 9.查看用户
MariaDB [(none)]> select user,host from mysql.user;
+------+-----------+
| user | host
+------+-----------+
| root | 127.0.0.1 |
| root | ::1
| | localhost |
| root | localhost |
| wp | localhost |
| | web01 |
| root | web01 |
+------+-----------+
# 10.退出数据库
MariaDB [(none)]> exit
MariaDB [(none)]> quit
MariaDB [(none)]> \q

数据库名字:wordpress
连接用户名:wp
连接密码:123
连接IP:localhost

架构(day11)_nginx_07

架构(day11)_mysql_08

## 鼠标样式代码

<script type="text/javascript">
/* 鼠标特效 */
$(function() {
var a_idx = 0,
b_idx = 0;
c_idx = 0;
jQuery(document).ready(function($) {
$("body").click(function(e) {
var a = new Array("苍老师", "武藤兰", "吴洋科","泷泽萝拉", "杨静川", "姚剑涛"),
b = new Array("#09ebfc", "#ff6651", "#ffb351", "#51ff65", "#5197ff", "#a551ff", "#ff51f7", "#ff518e", "#ff5163", "#efff51"),
c = new Array("12", "14", "16", "18", "20", "22", "24", "26", "28", "30");
var $i = $("<span/>").text(a[a_idx]);
a_idx = (a_idx + 1) % a.length;
b_idx = (b_idx + 1) % b.length;
c_idx = (c_idx + 1) % c.length;
var x = e.pageX,
y = e.pageY;
$i.css({
"z-index": 999,
"top": y - 20,
"left": x,
"position": "absolute",
"font-weight": "bold",
"font-size": c[c_idx] + "px",
"color": b[b_idx]
});
$("body").append($i);
$i.animate({
"top": y - 180,
"opacity": 0
}, 1500, function() {
$i.remove();
});
});
});
var _hmt = _hmt || [];
})
</script>


[root@web01 QQ2.8]# cd /blog/wordpress/wp-content/themes/QQ2.8
[root@web01 QQ2.8]# vim header.php

优化nginx上传文件的大小

# 1.  先找到nginx的配置文件

nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful**

# 2. 可以看到打印结果是在`/etc/nginx/nginx.conf`,接下来编辑它
[root@web01 ~]# vim /etc/nginx/nginx.conf

# 3. 在http内加入client_max_body_size 0m(0代表无限制)

# 4. 在启动nginx就好了
[root@web01 ~]# systemctl start nginx
[root@web01 ~]# systemctl enable nginx

# 5.php修改
[root@web01 ~]# vim /etc/php.ini
upload_max_filesize = 0M(0代表无限制)