1师倔、lnmp架構(gòu)概述
lnmp是一套技術(shù)組合灌旧,L =Linux 、N =nginx 悴晰、M~ = MySQL 慢睡、P~ = PHP
2、nginx 與 fastcgi工作流程
用戶通過(guò)http協(xié)議發(fā)送請(qǐng)求铡溪,請(qǐng)求會(huì)先抵達(dá)lnmp架構(gòu)中的nginx
nginx會(huì)根據(jù)用戶的請(qǐng)求進(jìn)行判斷有沒(méi)有l(wèi)ocation漂辐,有,則判斷是靜態(tài)頁(yè)面還是動(dòng)態(tài)頁(yè)面
nginx會(huì)根據(jù)用戶的請(qǐng)求進(jìn)行判斷有沒(méi)有l(wèi)ocation棕硫,有髓涯,則判斷是靜態(tài)頁(yè)面還是動(dòng)態(tài)頁(yè)面
如果是靜態(tài),nginx直接返回哈扮,如果是動(dòng)態(tài)纬纪,nginx會(huì)將請(qǐng)求發(fā)給fastcgi協(xié)議
然后fastcgi會(huì)將請(qǐng)求交給php-fpm管理進(jìn)程,php-fpm管理進(jìn)程會(huì)調(diào)用工作進(jìn)程warrap
然后warrap工作進(jìn)程會(huì)調(diào)用PHP
序進(jìn)行解析滑肉,解析代碼PHP直接返回包各,查詢數(shù)據(jù)庫(kù),PHP連接數(shù)據(jù)庫(kù)的用戶和密碼進(jìn)行查詢
最終數(shù)據(jù)由 mysql數(shù)據(jù)庫(kù)--->php程序---->warrap工作進(jìn)程---->php-fpm管理進(jìn)程----->fastcgi協(xié)議---->nginx--->user用戶
操作
1靶庙、架構(gòu)環(huán)境部署
#1问畅、卸載舊版本
[root@web01 ~]# yum remove php-mysql-5.4 php php-fpm php-common -y
#2、安裝擴(kuò)展源
[root@web01 ~]# yum localinstall -y http://mirror.webtatic.com/yum/el7/webtatic-release.rpm
#3、安裝新版本
[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按声、啟動(dòng)
[root@web01 ~]# systemctl restart 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.
#5膳犹、安裝數(shù)據(jù)庫(kù)
[root@web01 ~]# yum install mariadb-server mariadb
#6、啟動(dòng)數(shù)據(jù)庫(kù)
[root@web01 ~]# systemctl restart 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.
2签则、须床、nginx與PHP集成的原理
#1、編寫能解析PHP的nginx配置文件
[root@web01 ~]# cd /etc/nginx/conf.d/
[root@web01 conf.d]# vim php.yangyang.com.conf
[root@web01 conf.d]# cat php.yangyang.com.conf
server{
listen 80;
server_name php.yangyang.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;
}
}
#2渐裂、編寫PHP代碼豺旬,測(cè)試訪問(wèn)效果
[root@web01 conf.d]# cat /code/info.php
<?php
phpinfo();
?>
#3、檢測(cè)柒凉、重啟
[root@web01 conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntaxis ok
nginx: configuration file /etc/nginx/nginx.conf test is sucessful
[root@web01 conf.d]# systemctl restart nginx
#3族阅、host劫持
10.0.0.7 php.yangyang.com
3、PHP與MySQL集成的原理膝捞。
#1.啟動(dòng)數(shù)據(jù)庫(kù)
[root@web01 ~]# systemctl start mariadb
#2.配置連接密碼
[root@web01 ~]# mysqladmin password yangyang.com
#3.測(cè)試登錄mysql
[root@web01 ~]# mysql -uroot -pyangyang.com
MariaDB [(none)]>
#4.編寫php連接數(shù)據(jù)庫(kù)的代碼
[root@web01 ~]# cat code/mysqli.php
<?php
$servername = "localhost";
$username = "root";
$password = "yangyang.com";
// 創(chuàng)建連接
$conn = mysqli_connect($servername, $username, $password);
// 檢測(cè)連接
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "php連接MySQL數(shù)據(jù)庫(kù)成功";
?>
#5坦刀、測(cè)試PHP命令
[root@web01 ~]# php /code/mysqli.php
#6、通過(guò)瀏覽器訪問(wèn)(下面的圖)
4蔬咬、通過(guò)LNMP架構(gòu)部署Wordpress鲤遥、edusoho、phpmyadmin林艘、ecshop盖奈、
#1、配置php的文件
[root@web01 ~]# cat /etc/yum.repos.d/php.repo
[webtatic-php]
name = php Repository
baseurl = http://us-east.repo.webtatic.com/yum/el7/x86_64/
gpgcheck = 0
#2狐援、集成PHP的配置文件(定義域名以及站點(diǎn)的目錄位置)
root@web01 conf.d]# cat blog.yangyang.com.conf
server {
listen 80;
server_name blog.yangyang.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;
}
}
#3钢坦、根據(jù)Nginx配置,初始化環(huán)境,然后上傳代碼
#1.準(zhǔn)備站點(diǎn)目錄
[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.創(chuàng)建數(shù)據(jù)庫(kù)名
[root@web01 code]# mysql -uroot -pyangyang.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.統(tǒng)一Nginx PHP的權(quán)限 為 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
5、通過(guò)LNMP架構(gòu)部署 Wecenter
#1啥酱、編寫nginx的配置文件
[root@web01 ~ ]# cd /etc/nginx/conf.d/
[root@web01 conf.d]# cat zh.yangyang.com.conf
server{
listen 80;
server_name zh.yangyang.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 code]# rz WeCenter_3-3-2.zip
[root@web01 code]# ll
WeCenter_3-3-2.zip
[root@web01 code]# unzip WeCenter_3-3-2.zip
[root@web01 code]# mkdir zh
[root@web01 code]# chown -R www.www /code
#3镶殷、登錄數(shù)據(jù)庫(kù)逛万,創(chuàng)建庫(kù)名稱
[root@web01 conf.d]# mysql -uroot -pyangyang.com
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 72
Server version: 5.5.64-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)]> create database zh;
Query OK, 1 row affected (0.00 sec)
#MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
| wordpress |
| zh |
+--------------------+
6 rows in set (0.00 sec)
MariaDB [(none)]> BYE
#3、重啟nginx服務(wù)
[root@web01 conf.d]# systemctl restart nginx
#4批钠、配置host劫持
10.0.0.7 zh.yangyang.com
6宇植、通過(guò)LNMP架構(gòu)部署kodcloud
#1、匹配nginx配置文件
[root@web01 ~ ]# cd /etc/nginx/conf/
[root@web01 conf]# cat /etc/nginx/conf.d/kodcloud.yangdan.com.conf
server{
listen 80;
server_name kodcloud.yangdan.com;
root /code/kodcloud;
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]# /code
[root@web01 code]# mkdir /kodcloud
[root@web01 code]# wget http://static.kodcloud.com/update/download/kodexplorer4.40.zip
[root@web01 code]# unzip kodexplorer4.40.zip -d /code/kodcloud
[root@web01 code]# chown -R www.www /code/
#3、登錄數(shù)據(jù)庫(kù)拷呆,創(chuàng)建庫(kù)名稱
[root@web01 code]# mysql -uroot -pyangyang.com
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 210
Server version: 5.5.64-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)]> create database kodcloud;
Query OK, 1 row affected (0.00 sec)
#MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| kodcloud |
| mysql |
| performance_schema |
| test |
| wordpress |
| zh |
+--------------------+
7 rows in set (0.00 sec)
#MariaDB [(none)]> exit
Bye
#4闲坎、重啟nginx服務(wù)
[root@web01 code]# systemctl restart nginx
7疫粥、搭建edusoho視頻網(wǎng)站
#1、配置
[root@web01 conf]# cd /etc/ngimx/conf/
[root@web01 conf]# vim edusoho.yangdan.com.conf
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;
}
# 配置設(shè)置圖片格式文件
location ~* \.(jpg|jpeg|gif|png|ico|swf)$ {
# 過(guò)期時(shí)間為3年
expires 3y;
# 關(guān)閉日志記錄
access_log off;
# 關(guān)閉gzip壓縮腰懂,減少CPU消耗梗逮,因?yàn)閳D片的壓縮率不高。
gzip off;
}
# 配置css/js文件
location ~* \.(css|js)$ {
access_log off;
expires 3y;
}
# 禁止用戶上傳目錄下所有.php文件的訪問(wèn)绣溜,提高安全性
location ~ ^/files/.*\.(php|php5)$ {
deny all;
}
# 以下配置允許運(yùn)行.php的程序慷彤,方便于其他第三方系統(tǒng)的集成。
location ~ \.php$ {
# [改] 請(qǐng)根據(jù)實(shí)際php-fpm運(yùn)行的方式修改
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;
}
}
#2怖喻、上傳代碼底哗,修改屬組與屬主
[root@web01 conf]# mkdir /code/edusoho/web/
[root@web01 conf]# cd /code
[root@web01 code]# rz edusoho-8.3.43.zip
[root@web01 code]# unzip edusoho-8.3.43.zip -d /code/edusoho/web/
[root@web01 code]# chown -R www.www /code
[root@web01 code]# mysql -uroot -pyangyang.com
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 439
Server version: 5.5.64-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)]> create database edusoho;
Query OK, 1 row affected (0.00 sec)
#MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| edusoho |
| kodcloud |
| mysql |
| performance_schema |
| test |
| wordpress |
| zh |
+--------------------+
8 rows in set (0.00 sec)
MariaDB [(none)]> Bye
#3、測(cè)試锚沸,重啟nginx服務(wù)
[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
#4跋选、劫持
10.0.0.7 edusoho.yangdan.com