LNMP建設及位址重寫
LNMP部署
FastCGI(快速通用網關接口)是nginx伺服器與PHP溝通的一種語言标準
一、安裝nginx,mariadb,php
安裝nginx參考 web伺服器nginx 文檔https://blog.csdn.net/yy1506438689/article/details/106838675
[[email protected] ~]# yum install -y php php-mysql php-fpm ==>>安裝php相關軟體包
[[email protected] ~]# yum install -y mariadb mariadb-server mariadb-devel ==>>安裝資料庫相關包
mariadb(資料庫用戶端軟體)、mariadb-server(資料庫伺服器軟體)、mariadb-develop(其他用戶端軟體的依賴包)、php(解釋器)、php-fpm(程序管理器服務)、php-mysql(php的資料庫擴充包)
[[email protected] ~]# systemctl start mariadb ==>>啟動服務
[[email protected] ~]# systemctl enable mariadb ==>>服務加入開機自啟動
[[email protected] ~]# systemctl status mariadb ==>>檢視服務狀态
[[email protected] ~]# systemctl start php-fpm
[[email protected] ~]# systemctl enable php-fpm
[[email protected] ~]# systemctl status mariadb
[[email protected] ~]# netstat -ntulp|grep :80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 4266/nginx: master
[[email protected] ~]# netstat -ntulp|grep :3306
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 4917/mysqld
[[email protected] ~]# netstat -ntulp|grep :9000
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 5021/php-fpm: maste
二、建構LNMP平台
1.php配置
[[email protected] ~]# vim /etc/php-fpm.d/www.conf
listen = 127.0.0.1:9000 ==>>php端口号
pm.max_children = 50 ==>>最大程序數量
pm.start_servers = 5 ==>>最小程序數量
2.nginx配置
[[email protected] ~]# vim /usr/local/nginx/html/test.php ==>>添加測試腳本
<?php
$i=33;
echo $i;
?>
[[email protected] conf]# vim nginx.conf ==>>添加php的location
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf; ==>>通路這個檔案,在conf目錄下
}
-------------------------------------------------------------------------
location比對使用者的位址欄(從域名後面開始)location /abc { deny all;}
-------------------------------------------------------------------------
3.資料庫配置
[[email protected] ~]# cd /usr/local/nginx/html/
[[email protected] html]# vim mysql.php
<?php
$mysqli = new mysqli('localhost','root','','mysql');
if (mysqli_connect_errno()){
die('Unable to connect!'). mysqli_connect_error();
}
$sql = "select * from user";
$result = $mysqli->query($sql);
while($row = $result->fetch_array()){
printf("Host:%s",$row[0]);
printf("</br>");
printf("Name:%s",$row[1]);
printf("</br>");
}
?>
四、位址重寫
rewrite 舊位址 新位址 [選項]
last不再讀其他rewirte
break不在讀其他語句,結束請求
redircet臨時重定向
permanent永久重定向
1.将通路a.html重定向到b.html
[[email protected] ~]# vim /usr/local/nginx/conf/nginx.conf
server{
listen 80;
server_name www.a.com;
rewrite /a.html /b.html; ==>>在此處進行重寫
location / {
root www;
index index.html index.htm;
}
}
[[email protected] www]# /usr/local/nginx/sbin/nginx -s reload
2.将通路a.html位址欄跳轉到b.html
[[email protected] ~]# vim /usr/local/nginx/conf/nginx.conf
server{
listen 80;
server_name www.a.com;
rewrite /a.html /b.html redirect; ==>>在此處進行重寫,添加redirect實作位址欄跳轉
location / {
root www;
index index.html index.htm;
}
}
[[email protected] www]# /usr/local/nginx/sbin/nginx -s reload
3.通路根下所有頁面都跳轉到http://www.tmooc.cn
[[email protected] www]# vim /usr/local/nginx/conf/nginx.conf
server{
listen 80;
server_name www.a.com;
rewrite ^/ http://www.baidu.com; ==>>重寫根下位址跳轉到http://www.baidu.com
location / {
root www;
index index.html index.htm;
}
}
[[email protected] www]# /usr/local/nginx/sbin/nginx -s reload
4.實作www.a.com/a.html跳轉到www.b.com/a.html
[[email protected] www]# vim /usr/local/nginx/conf/nginx.conf
server{
listen 80;
server_name www.a.com;
rewrite ^/(.*) http://www.b.com/$1; ==>>重寫
location / {
root www;
index index.html index.htm;
}
}
[[email protected] www]# /usr/local/nginx/sbin/nginx -s reload
5.實作不同辨別通路相同連結傳回頁面不同
[[email protected] ~]# mkdir /usr/local/nginx/www/firefox ==>>添加測試資料
[roo[email protected] ~]# echo firefox >/usr/local/nginx/www/firefox/b.html
[[email protected] ~]# vim /usr/local/nginx/conf/nginx.conf
server{
listen 80;
server_name www.a.com;
if ($http_user_agent ~* windows){ ==>>搜尋作業系統為windows的跳轉到firefox目錄下
rewrite ^/(.*) /firefox/$1;
}
location / {
root www;
index index.html index.htm;
}
}
[[email protected] ~]# /usr/local/nginx/sbin/nginx -s reload