有上图可以了解到,对于静态资源nginx自己就能处理,动态的资源nginx转交给助手fastcgi,助手fastcgi.再转交·给·php.
nginx配置文件如下
[root@web02 /etc/nginx]# vim conf.d/01.www.conf
server {
listen 80; ##监听的端口为80
server_name blog.oldboy.com; ##可以以什么域名访问
root /usr/share/nginx/html/www; ##指定站点目录
access_log /var/log/nginx/access_www.log main; ##访问日志存放路径
location / { ##location匹配uri模块
index index.php index.html index.htm; ##首页文件
try_files $uri $uri/ /index.php?$args; ##如果用户请求的uri不存在.返回后面默认的值
}
location ~* \.(php|php5)$ { ##location匹配不区分大小写以php或php5结尾的文件
fastcgi_pass 127.0.0.1:9000; ##把动态请求交给 php-fpm
fastcgi_index index.php; ##首页文件
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
设置nginx把请求转发给php的时候的 参数
网站站点目录
请求的uri
include fastcgi_params; 包含这个文件
}
}
linux查看网站的方法
[root@web01 /etc/nginx/conf.d]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 10.0.0.254 0.0.0.0 UG 0 0 0 eth0
10.0.0.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
169.254.0.0 0.0.0.0 255.255.0.0 U 1002 0 0 eth0
169.254.0.0 0.0.0.0 255.255.0.0 U 1003 0 0 eth1
172.16.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth1
[root@web01 /etc/nginx/conf.d]# ip r
default via 10.0.0.254 dev eth0
10.0.0.0/24 dev eth0 proto kernel scope link src 10.0.0.7
169.254.0.0/16 dev eth0 scope link metric 1002
169.254.0.0/16 dev eth1 scope link metric 1003
172.16.1.0/24 dev eth1 proto kernel scope link src 172.16.1.7
[root@web01 /etc/nginx/conf.d]# ip route
default via 10.0.0.254 dev eth0
10.0.0.0/24 dev eth0 proto kernel scope link src 10.0.0.7
169.254.0.0/16 dev eth0 scope link metric 1002
169.254.0.0/16 dev eth1 scope link metric 1003
172.16.1.0/24 dev eth1 proto kernel scope link src 172.16.1.7
1.Nginx与PHP之间的联系
[root@web01 ~]# cat /etc/nginx/conf.d/02-blog.conf
server {
listen 80;
server_name blog.oldboy.com;
access_log /var/log/nginx/access_blog.log main;
root /usr/share/nginx/html/blog;
location / {
index index.php index.html index.htm;
}
location ~* \.(php|php5)$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
解释:
fastcgi_pass 127.0.0.1:9000; 通过php(9000端口)静态的资源自己处理 动态的通过fastcgi_pass给中介(fastcgi)
fastcgi_index index.php; 站点目录
fastcgi_param
SCRIPT_FILENAME 脚本名字
document_root 网站站点目录
fastcgi_script_name 请求连接的URI
※2.Mariadb数据库
1.提前安装MySQL,重启MySQL的mariadb服务
image
2.查看进程号ss -lntup |grep mysql
3.查看进程 ps -ef |grep mysql
4.然后执行mysql命令进入
※①查看系统中所有数据库的命令
show databases;
结尾要加分号
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.01 sec)
MariaDB [(none)]>
※查看数据库表(行)
MariaDB [(none)]> show tables from mysql;
+---------------------------+
| Tables_in_mysql |
+---------------------------+
| columns_priv |
| db |
| event |
| func |
| general_log |
| help_category |
| help_keyword |
| help_relation |
| help_topic |
| host |
| ndb_binlog_index |
| plugin |
| proc |
| procs_priv |
| proxies_priv |
| servers |
| slow_log |
| tables_priv |
| time_zone |
| time_zone_leap_second |
| time_zone_name |
| time_zone_transition |
| time_zone_transition_type |
| user |
+---------------------------+
24 rows in set (0.00 sec)
※②查看系统中所有的用户信息
查看user表中的user和host字段
select user,host from mysql.user;
显示出user和host两个字段 在mysql数据库中 的user表中进行查找。
MariaDB [(none)]> select user,host from mysql.user;
+------+-----------+
| user | host |
+------+-----------+
| root | 127.0.0.1 |
| root | ::1 |
| | localhost |
| root | localhost |
| | web01 |
| root | web01 |
+------+-----------+
6 rows in set (0.00 sec)
MariaDB [(none)]>
MariaDB [(none)]> #select * from wordpress.wp_posts\G #查看所有表让内容对齐
MariaDB [(none)]> #select * from wordpress.wp_posts limit 1 \G #只查看一列让内容对齐
MariaDB [(none)]> #select user,host from mysql.user; #查看user表中的user和host字段(查看系统中所有的用户信息)
MariaDB [(none)]> #use mysql; #切换到数据库表
MariaDB [mysql]> #select user,host from user; #进入表后就不用加绝对路径mysql,直接查看相对路径的user和host字段
※③创建一个新的数据库wordpress
create database wordpress;
最后为数据库名字
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)
MariaDB [(none)]>
※④创建用户 并给权限,密码,允许谁登录
grant all on wordpress.* to 'wordpress'@'172.16.1.%' identified by '123456';
grant all on wordpress.* to 'wordpress'@'localhost' identified by '123456';
>grant 创建用户并给权限
>all 所有权限
>on 指定的是
>wordpress.* wordpress数据库的.所有表
>
>'wordpress'@'localhost' ‘用户名'@'ip.登录' (%==*)
>identified by '123456'; 密码是 '123456';
MariaDB [(none)]> grant all on wordpress.* to 'wordpress'@'172.16.1.%' identified by '123456';
Query OK, 0 rows affected (0.04 sec)
MariaDB [(none)]> grant all on wordpress.* to 'wordpress'@'localhost' identified by '123456';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> select user,host from mysql.user;
+-----------+------------+
| user | host |
+-----------+------------+
| root | 127.0.0.1 |
| wordpress | 172.16.1.% |
| root | ::1 |
| | localhost |
| root | localhost |
| wordpress | localhost |
| | web01 |
| root | web01 |
+-----------+------------+
8 rows in set (0.00 sec)
※⑤删除(谨慎操作)
删除表: drop database lcx;
删除用户: drop user lcx@'172.16.1.%';
删除空用户: drop user ''@'localhost';
※⑥刷新权限信息
flush privileges;
修改用户信息之后需要更新权限信息
※⑦ctrl c退出mysql,重新进入新创建的数据库用户
mysql -uwordpress -p
-p后直接输入密码也可以
-u与-p后不能加空格
-u:连接MySQL服务器的用户名
-p:连接MySQL服务器的密码
image
※⑧数据库备份
- 打包 + 定时任务 +rsync
- 备份:
备份全部表:
mysqldump -uroot -p -A >/root/all.sql
备份wordpress表:
mysqldump -uwordpress -p123456 -A >/root/wordpress.sql
备份并压缩
mysqldump -uroot -p -A|gzip >/root/all-gzip.sql.gz
- 将备份的数据库恢复: mysql -uroot -p </root/all.sql
3.搭建PHP环境
1.修改/etc/php-fpm.d/www.conf中的user与group为nginx
2.检查 用户与用户组是否为nginx
egrep -n 'user|group' /etc/php-fpm.d/www.conf
3.重启 php-fpm.service 服务
systemctl restart php-fpm.service
4.查看端口号与进程(9000)
ss -lntup|grep 9000
ps -ef |grep php
[12:45 root@web01 /etc/nginx/conf.d]# egrep -n '^user|^group' /etc/php-fpm.d/www.conf
8:user = nginx
10:group = nginx
[12:45 root@web01 /etc/nginx/conf.d]# systemctl restart php-fpm.service
[12:45 root@web01 /etc/nginx/conf.d]# ss -lntup|grep 9000
tcp LISTEN 0 128 127.0.0.1:9000 *:* users:(("php-fpm",pid=9013,fd=9),("php-fpm",pid=9012,fd=9),("php-fpm",pid=9011,fd=9),("php-fpm",pid=9010,fd=9),("php-fpm",pid=9009,fd=9),("php-fpm",pid=9008,fd=7))
[12:45 root@web01 /etc/nginx/conf.d]# ps -ef |grep php
root 9008 1 1 12:45 ? 00:00:00 php-fpm: master process (/etc/php-fpm.conf)
nginx 9009 9008 0 12:45 ? 00:00:00 php-fpm: pool www
nginx 9010 9008 0 12:45 ? 00:00:00 php-fpm: pool www
nginx 9011 9008 0 12:45 ? 00:00:00 php-fpm: pool www
nginx 9012 9008 0 12:45 ? 00:00:00 php-fpm: pool www
nginx 9013 9008 0 12:45 ? 00:00:00 php-fpm: pool www
root 9017 7472 0 12:45 pts/1 00:00:00 grep --color=auto php
Nginx与PHP之间:
image
5. 切换到blog站点目录下,添加以php结尾的文件
vim info.php
注意php的语法格式
[12:50 root@web01 /etc/nginx/conf.d]# cd /usr/share/nginx/html/blog/
[12:50 root@web01 /usr/share/nginx/html/blog]# vim info.php
<?php
phpinfo();
?>
浏览器查看网页http://10.0.0.7/info.php
image
如果访问没有显示出来,主要原因就是Nginx没有将请求抛给PHP,主要去看nginx的配置
首先保证02-blog.conf配置文件是在第一位,把其他的配置模块先压缩或注释掉。
image第二种就是配置的内容是否有错误,是否生效
image
PHP与MySQL之间:
image6.在blog站点目录下创建第二个文件mysqli.php
[root@web01 /blog]# vim mysqli.php
<?php
$servername = "localhost";
$username = "wordpress";
$password = "123456";
// 创建连接
$conn = mysqli_connect($servername, $username, $password);
// 检测连接
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "php连接MySQL数据库成功";
?>
搭建wordpress博客
image
1.解压后讲wordpress下的所有内容移动到blog站点目录下
image
修改权限blog站点目录的属主属组为nginx
[root@web01 ~]# chown -R nginx.nginx /usr/share/nginx/html/blog/
[root@web01 ~]# ll -d /usr/share/nginx/html/blog/
drwxr-xr-x 5 nginx nginx 4096 Jun 6 21:03 /usr/share/nginx/html/blog/
网友评论