1.什么是LNMP架构
2.LNMP架构是如何工作的
location / {
index index.php;
}
location ~ .php$ {
fastcgi_pass 127.0.0.1:9000;
}
location ~ .(jpg|png|gif)$ {
root /code/images;
}
3.nginx与php,mysql之间是如何工作的。
4.如何安装LNMP架构
安装php
配置扩展源
yum localinstall -y http://mirror.webtatic.com/yum/el7/webtatic-release.rpm
安装php7.1
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
启动php
systemctl start php-fpm
安装mysql
touch /etc/yum.repos.d/mariadb.repo
然后写入如下内容
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.1/centos7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1
安装
yum install mariadb-server mariadb -y
启动
systemctl start mariadb
5.nginx与php集成的原理
1.编写能解析的php的nginx配置文件
[root@web01 conf.d]# cat php.oldxu.com.conf
server {
listen 80;
server_name php.oldxu.com;
root /code;
location / {
index index.php;
}
location ~ .php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
5.2.编写PHP代码,测试访问效果.
[root@web01 conf.d]# cat /code/info.php
<?php
phpinfo();
?>
5.3host劫持
6.php与mysql集成的原理
6.1启动数据库
[root@web01 ~]# systemctl start mariadb
6.2.配置连接密码
[root@web01 ~]# mysqladmin password oldxu.com
6.3测试登录mysql
[root@web01 ~]# mysql -uroot -poldxu.com
MariaDB [(none)]>
6.4编写php连接数据库的代码
[root@web01 ~]# /code/mysqli.php <?php $servername = "localhost"; $username = "root"; $password = "oldxu.com"; // 创建连接 $conn = mysqli_connect($servername, $username, $password); // 检测连接 if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } echo "php连接MySQL数据库成功"; ?>
6.5测试
(1)[root@web01 ~]# php /code/mysqli.php
(2)也可以通过浏览器的方式去测试
7.通过LNMP架构部署Wordpress、Wecenter、edusoho、phpmyadmin、ecshop、
7.1例
7.1.1.编写Nginx集成PHP的配置文件 (定义域名以及站点的目录位置)
[root@web01 conf.d]# cat blog.oldxu.com.conf
server {
listen 80;
server_name blog.oldxu.com;
root /code/wordpress;
location / {
index index.php;
}
location ~ .php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
7.1.2.根据Nginx配置,初始化环境,然后上传代码
1.准备站点目录
[root@web01 conf.d]# mkdir /code
2.下载wordpress代码
[root@web01 conf.d]# cd /code
[root@web01 code]# tar xf wordpress-5.2.3-zh_CN.tar.gz
3.创建数据库名
[root@web01 code]# mysql -uroot -poldxu.com
MariaDB [(none)]> create database wordpress;
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
| wordpress |
+--------------------+
5 rows in set (0.01 sec)
4.统一Nginx PHP的权限 为 www
[root@web01 code]# groupadd www -g 666
[root@web01 code]# useradd -u666 -g666 www
[root@web01 code]# sed -i '/^user/c user www;' /etc/nginx/nginx.conf
[root@web01 code]# chown -R www.www /code
[root@web01 code]# systemctl restart nginx
[root@web01 code]# sed -i '/^user/c user = www' /etc/php-fpm.d/www.conf
[root@web01 code]# sed -i '/^group/c group = www' /etc/php-fpm.d/www.conf
[root@web01 code]# systemctl restart php-fpm
7.2wecenter:
1.编写Nginx的配置文件 虚拟主机
[root@web01 conf.d]# cat zh.oldxu.com.conf
server {
listen 80;
server_name zh.oldxu.com;
root /code/zh;
client_max_body_size 100m;
location / {
index index.php;
}
location ~ .php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
2.上传代码,变更代码的属主和属组
[root@web01 conf.d]# cd /code
[root@web01 conf.d]# rz WeCenter_3-3-2.zip
[root@web01 conf.d]# mkdir zh
[root@web01 conf.d]# unzip WeCenter_3-3-2.zip -d /code/zh/
[root@web01 code]# chown -R www.www /code
3.登录数据库.创建库名称
[root@web01 code]# mysql -uroot -poldxu.com
MariaDB [(none)]> create database zh;
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
| wordpress |
| zh |
+--------------------+
6 rows in set (0.00 sec)
3.重启Nginx服务
[root@web01 code]# systemctl restart nginx
4.配置host劫持
7.3edusoho视频网站
配置文件
server {
listen 80;
server_name www.xxxx.com;
root /code/edusoho/web;
# 日志路径
access_log /var/log/nginx/example.com.access.log;
error_log /var/log/nginx/example.com.error.log;
location / {
index app.php;
try_files $uri @rewriteapp;
}
location @rewriteapp {
rewrite ^(.*)$ /app.php/$1 last;
}
location ~ ^/udisk {
internal;
root /code/edusoho/app/data/;
}
location ~ ^/(app|app_dev).php(/|$) {
fastcgi_pass 127.0.0.1:9000;
fastcgi_split_path_info ^(.+.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param HTTPS off;
fastcgi_param HTTP_X-Sendfile-Type X-Accel-Redirect;
fastcgi_param HTTP_X-Accel-Mapping /udisk=/code/edusoho/app/data/udisk;
fastcgi_buffer_size 128k;
fastcgi_buffers 8 128k;
}
# 配置设置图片格式文件
location ~* .(jpg|jpeg|gif|png|ico|swf)$ {
# 过期时间为3年
expires 3y;
# 关闭日志记录
access_log off;
# 关闭gzip压缩,减少CPU消耗,因为图片的压缩率不高。
gzip off;
}
# 配置css/js文件
location ~* .(css|js)$ {
access_log off;
expires 3y;
}
# 禁止用户上传目录下所有.php文件的访问,提高安全性
location ~ ^/files/.*.(php|php5)$ {
deny all;
}
# 以下配置允许运行.php的程序,方便于其他第三方系统的集成。
location ~ .php$ {
# [改] 请根据实际php-fpm运行的方式修改
fastcgi_pass 127.0.0.1:9000;
fastcgi_split_path_info ^(.+.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param HTTPS off;
}
}