一、nginx的location配置
使用Nginx Location可以可以控制访问网站的路径,但一个server可以有多个location配置,多个location的优先级该如何区分
1、语法
Syntax: location [ = | ~ | ~* | ^~ ] uri { ... }
location @name { ... }
Default: —
Context: server, location
2、location匹配符
匹配符 | 匹配规则 | 优先级 |
= | 精准匹配 | 1 |
^~ | 以某个字符串开头 | 2 |
~ | 区分大小写的正则匹配 | 3 |
~* | 不区分大小写的正则匹配 | 4 |
/ | 通用匹配,任何请求都会匹配到 | 5 |
3、location应用场景
#通用匹配,任何请求都会匹配到
location / {
...
}
#严格区分大小写,匹配一PHP结尾的都走这个location
location ~ \.php$ {
...
}
#严格区分大小写,匹配以jsp结尾的都走这个location
location ~ \.jsp$ {
....
}
#不区分大小写匹配,只要用户访问 . jpg,gif,png,js,css 都走这条location
location ~* .*\.(jpg | gif | png | js | css)$ {
...
}
http://linux.test.com/1.PHP
http://linux.test.com/1.JPG
http://linux.test.com/1.jsp
http://linux.test.com/1.Gif
http://linux.test.com/1.PnG
http://linux.test.com/1.Jsp
二、LNMP架构
1、简介
LNMP是一套技术组合,L=Linux,N=Nginx,M=MysQL,P=PHP
不仅仅只有这些服务,还有很多
redis\elasticsearch\kibana\logstash\zabbix\git\jenkins\kafka\hbase\hadoop\spark\flink
2、搭建nginx
①配置官方源
[root@web01 ~]# vim /etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
②安装nginx
[root@web01 ~]# yum install -y nginx
③配置nginx
[root@web01 ~]# vim /etc/nginx/nginx.conf #统一服务用户名便于管理
user www;
④创建用户
[root@web01 ~]# groupadd www -g 666
[root@web01 ~]# useradd www -u 666 -g 666 -s /sbin/nologin -M
⑤启动nginx
[root@web01 ~]# systemctl start nginx
[root@web01 ~]# systemctl enable nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.#验证启动
[root@web01 ~]# ps -ef | grep nginx
root 9953 1 0 11:17 ? 00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
www 9954 9953 0 11:17 ? 00:00:00 nginx: worker process
3、安装PHP
①安装PHP
1.上传包
[root@web01 ~]# rz
[root@web01 ~]# ll
-rw-r--r--. 1 root root 19889622 Nov 22 15:52 php.tar.gz2.解压包
[root@web01 ~]# tar xf php.tar.gz3.本地安装php的rpm包
[root@web01 ~]# yum localinstall -y *.rpm
②配置PHP
[root@web01 ~]# vim /etc/php-fpm.d/www.conf
user = www
group = www
③启动服务
[root@web01 ~]# systemctl start php-fpm
[root@web01 ~]# systemctl enable php-fpm
Created symlink from /etc/systemd/system/multi-user.target.wants/php-fpm.service to /usr/lib/systemd/system/php-fpm.service.
④启动验证
[root@web01 ~]# ps -ef | grep php-fpm
root 10195 1 0 11:29 ? 00:00:00 php-fpm: master process (/etc/php-fpm.conf)
www 10196 10195 0 11:29 ? 00:00:00 php-fpm: pool www
www 10197 10195 0 11:29 ? 00:00:00 php-fpm: pool www
www 10198 10195 0 11:29 ? 00:00:00 php-fpm: pool www
www 10199 10195 0 11:29 ? 00:00:00 php-fpm: pool www
www 10200 10195 0 11:29 ? 00:00:00 php-fpm: pool www
4、搭建 mariadb
①安装
[root@web01 ~]# yum install -y mariadb-server
②启动服务
[root@web01 ~]# systemctl start mariadb
[root@web01 ~]# systemctl enable mariadb
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.
③验证启动
[root@web01 ~]# ps -ef | grep mariadb
mysql 11006 10841 1 12:06 ? 00:00:00 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --log-error=/var/log/mariadb/mariadb.log --pid-file=/var/run/mariadb/mariadb.pid --socket=/var/lib/mysql/mysql.sock
④连接
[root@web01 ~]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \
Your MariaDB connection id is 2
Server version: 5.5.68-MariaDB MariaDB ServerCopyright (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)]> show databases; #查看数据库
+----------------------------+
| Database |
+----------------------------+
| information_schema |
| mysql |
| performance_schema|
| test |
+----------------------------+
4 rows in set (0.00 sec)
⑤设置数据库密码
[root@web01 ~]# mysqladmin -uroot password '123'
#使用密码连接数据库
[root@web01 ~]# mysql -uroot -p123
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 5
Server version: 5.5.68-MariaDB MariaDB ServerCopyright (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)]>
5、测试PHP和mariadb关联
①编写PHP测试连接数据库代码
[root@web01 ~]# vim /code/zuoye/test.php
<?php
$servername = "localhost";
$username = "root";
$password = "123";// 创建连接
$conn = mysqli_connect($servername, $username, $password);// 检测连接
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "小哥哥,php可以连接MySQL...";
?><img style='width:100%;height:100%;' src=https://blog.driverzeng.com/zenglaoshi/php_mysql.png>
三、搭建WordPress博客
1、上传代码
[root@web01 code]# rz
[root@web01 code]# ll
-rw-r--r--. 1 root root 11098483 Sep 12 17:52 wordpress-5.0.3-zh_CN.tar.gz
2、减压代码
[root@web01 code]# tar xf wordpress-5.0.3-zh_CN.tar.gz
3、授权减压文件
[root@web01 code]# chown -R www.www wordpress
4、配置nginx
[root@web01 code]# vim /etc/nginx/conf.d/linux.wp.com.conf
server {
listen 80;
server_name linux.wp.com;location / {
root /code/wordpress;
index index.php;
}location ~* \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME /code/wordpress/$fastcgi_script_name;
include fastcgi_params;
}
}
5、重启访问
#检查配置
[root@web01 code]# 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 code]# systemctl restart nginx
6、访问测试
#配置hosts
10.0.0.7 linux.wp.com#访问
http://linux.wp.com/
7、创建数据库
[root@web01 code]# mysql -uroot -p123
MariaDB [(none)]> create database wordpress;
Query OK, 1 row affected (0.00 sec)MariaDB [(none)]> show databases;
+-----------------------------+
| Database |
+-----------------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
| wordpress |
+------------------------------+
5 rows in set (0.00 sec)
在浏览器输入IP地址进行访问