Nginx和Apache對比(重點):
1.輕量級,採用 C 進行編寫,一樣的 web 服務,會佔用更少的內存及資源
2.nginx 處理靜態文件好,靜態處理性能比 apache 高三倍以上,apache 在處理動態請求有優點
3.nginx 做爲負載均衡服務器,支持 7 層負載均衡
4.抗併發,nginx 以 epoll and kqueue 做爲開發模型
nginx部署:
第一步:配置yum源(原基礎上添加)
[root@ken ~]vim /etc/yum.repos.d
[root@ken yum.repos.d] ls
[root@ken yum.repos.d]vim local.repo
[epel]
name=epel
enabled=1
gpgcheck=0
第二步:下載nginx
[root@ken yum.repos.d]# yum install nginx -y
第三步:啓動nginx
[root@ken yum.repos.d]# systemctl restart nginx
nginx反向代理–動靜分離(設置三臺虛擬機)
192.168.64.123 主服務器
192.168.64.45 靜態節點
192.168.64.3 動態節點
第一步:在主服務器部署nginx並配置動靜分離規則
[root@ken html]#vim /etc/nginx/nginx.conf
location ~ html$ {
proxy_pass http://192.168.64.45;
}
location ~ php$ {
proxy_pass http://192.168.64.3;
}
第二步:檢測語法重啓nginx
[root@ken html]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@ken html]# systemctl restart nginx
第二步:配置靜態服務器(打開靜態服務器)
[root@localhost ~]# cd /var/www/html
[root@localhost html]# ls
[root@localhost html]# echo "zhangpan" >> /var/www/html/index.html
[root@localhost html]# cat index.html
第三步:配置動態服務器
[root@localhost ~]# cd /var/www/html
[root@localhost html]# yum install php httpd -y
[root@localhost html]#vim /var/www/html/index.php(寫入下邊的動態服務)
<?php
phpinfo();
?>
第四步:瀏覽器訪問主服務器
nginx反向代理–不徹底代理
第一步:編輯nginx文件,編輯規則(在主服務器中編輯)(把以前的刪掉)
[root@ken html]#vim /etc/nginx/nginx.conf
location /admin{
proxy_pass http://192.168.64.45;
}
第二步:檢測並重啓
[root@ken html]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@ken html]# systemctl restart nginx
第三步:瀏覽器訪問
訪問不到
第四步:建立文件(在靜態服務器中)
[root@ken html]# mkdir admin
[root@ken html]# echo 「56566」 > admin/index.html
第五步:瀏覽器再次訪問
訪問成功
總結:不徹底代理的話location後面定義的訪問uri會自動填補到IP地址的後面,location /admin{
proxy_pass http://192.168.64.5;
}
nginx反向代理–徹底代理
第一步:編輯nginx文件,編輯規則(在主服務器中編輯)(把以前的刪掉)
[root@ken html]#vim /etc/nginx/nginx.conf
location /admin{
proxy_pass http://192.168.64.45/;
}
第二步:檢測並重啓
[root@ken html]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@ken html]# systemctl restart nginx
第三步:瀏覽器訪問
能夠訪問
總結:徹底代理的話location後面定義的訪問uri不會自動填補到IP地址的後面,location /admin{
proxy_pass http://192.168.64.5/;
}
nginx負載均衡演示
第一步:編寫規則
[root@ken html]#vim /etc/nginx/nginx.conf(在主服務器中)
upstream pan {
server 192.168.64.5 weight=6 max_fails=2 fail_timeout=2;
server 192.168.64.7 weight=1 max_fails=2 fail_timeout=2;
}
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /var/www/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
proxy_pass http://pan;
}
第二步:檢查語法並重啓
[root@ken html]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@ken html]# systemctl restart nginx
第三步:瀏覽器訪問主節點
LNMP架構上線網站
第一步:下載相關的軟件包(在主服務器中)
[root@ken html]# yum install nginx php php-mysql mariadb-server php-fpm -y
第二步:編輯php匹配規則
[root@ken html]#vim /etc/nginx/nginx.conf(在主服務器中)
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /var/www/html;
index index.php index.html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
}
location ~ php$ {
fastcgi_pass 127.0.0.1:9000;
include fastcgi.conf;
}
第三步:檢測並重啓
[root@ken html]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@ken html]# systemctl restart nginx
第四步:啓動php-fpm(在原基礎上添加)
[root@ken ~]# cd /etc/yum.repos.d
[root@ken /etc/yum.repos.d ]# ls
[root@ken /etc/yum.repos.d ]#vim local.repo
[centos]
name=centos base
enabled=1
gpgcheck=0
[root@ken yum.repos.d]# systemctl restart php-fpm
[root@ken yum.repos.d]# ss -tnl
第五步:重啓數據庫
[root@ken yum.repos.d]# systemctl restart mariadb
第六步:建立數據庫添加用戶
MariaDB [(none)]> create database pan;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> grant all on pan.* to ken@’localhost’ identified by ‘123’;
Query OK, 0 rows affected (0.00 sec)
第七步:上傳wordpress安裝包至nginx網站根目錄下/var/www/html並解壓
[root@ken html]# yum install unzip -y
[root@ken html]# unzip wordpress-3.3.1-zh_CN.zip
第八步:配置數據庫文件
[root@ken html]# cp wp-config-sample.php wp-config.php
[root@ken html]# vim wp-config.php
define(‘DB_NAME’, ‘pan’);
/** MySQL 數據庫用戶名 */
define(‘DB_USER’, ‘pan’);
/** MySQL 數據庫密碼 */
define(‘DB_PASSWORD’, ‘123’);
第九步:瀏覽器訪問