1. 程式人生 > 實用技巧 >Linux系統LNMP架構

Linux系統LNMP架構

LNMP架構:

就是linux、nginx、MySQL、php結合

準備環境

還原虛擬機器快照,使用優化好的環境

第一步安裝nginx

  # 開啟瀏覽器輸入nginx.org,進入nginx官網
  # 點選頁面右側的download,然後檢視穩定版本,點選頁面最下面的 
  stable and mainline 
  # 然後找到RHEL/CentOS根據官方檔案格式配置yum源
  [root@web01 ~]# cat /etc/yum.repos.d/nginx.repo 
  [nginx-stable]
  name=nginx stable repo
  baseurl=http://nginx.org/packages/centos/$releasever/$base
  arch/
  gpgcheck=1
  enabled=1
  gpgkey=https://nginx.org/keys/nginx_signing.key
  module_hotfixes=true
  # 開啟yum快取,方便打包
  [root@web01 ~]# cat /etc/yum.conf 
  [main]
  cachedir=/var/cache/yum/$basearch/$releasever
  keepcache=1  # 這裡改成1,預設是0
  debuglevel=2
  logfile=/var/log/yum.log
  exactarch=1
  obsoletes=1
  gpgcheck=1
  plugins=1
  installonly_limit=5
  bugtracker_url=http://bugs.centos.org/set_project.php?
  project_id=23&ref=http://bugs.centos.org/bug_report_page.p
  hp?category=yum
  distroverpkg=centos-release

  #  配置完成後下載nginx服務
  [root@web01 ~]# yum install -y nginx
  # 下載完成後開啟nginx並加人開機自啟動
  [root@web01 ~]# systemctl start nginx
  [root@web01 ~]# systemctl enable nginx
  # 檢查埠和程序
  [root@web01 conf.d]# netstat -lntup|grep '80'
  tcp        0      0 0.0.0.0:80              0.0.0.0:*
  [root@web01 conf.d]# !ps
  ps -ef |grep nginx

第二步安裝PHP

  # 刪除系統自帶的PHP版本
  [root@web01 conf.d]# yum remove php-mysql-5.4 php php-fpm 
  php-common
  # 更換PHP的源
  [root@web01 conf.d]# cat /etc/yum.repos.d/php.repo 
  [php-webtatic]
  name = PHP Repository
  baseurl = http://us-east.repo.webtatic.com/yum/el7/x86_64/
  gpgcheck = 0
  # 下載PHP和依賴包
  [root@web01 conf.d]# 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
  # 下載完成後找到快取目錄裡面的所有以.rpm結尾的包打包
  建立一個存放包的目錄,把包複製過去
  mkdir /root/nginx_php
  然後用find xargs 和cp組合拷貝到建立的目錄中
  [root@web01 conf.d]# find /var/cache/yum -type f -name 
  '*.rpm'|xargs cp -t /root/nginx_php
  最後使用tar打包
  [root@web01 conf.d]# tar -zcf nginx_php.tgz 
  /root/nginx_php/
  # 以上步驟做完之後修改nginx和PHP的啟動使用者讓兩者統一
  [root@web01 ~]# groupadd www -g 666
  [root@web01 ~]# useradd www -u 666 -g 666 -s /sbin/nologin 
  -M
  # 然後修改nginx和PHP的配置檔案,更換使用者
  [root@web01 ~]# cat /etc/nginx/nginx.conf 

  user  www;
  worker_processes  1;
  [root@web01 ~]# vim /etc/php-fpm.d/www.conf 
  [root@web01 ~]# cat /etc/php-fpm.d/www.conf 
  ; Start a new pool named 'www'.
  [www]

  ; Unix user/group of processes
  ; Note: The user is mandatory. If the group is not set, 
  the default user's group
  ;       will be used.
  ; RPM: apache Choosed to be able to access some dir as 
  httpd
  user = www
  ; RPM: Keep a group allowed to write in log dir.
  group = www

第三步安裝MySQL的小夥伴mariadb-server

  # 直接安裝
  [root@web01 ~]# yum install -y mariadb-server
  # 啟動並加入開機自啟
  [root@web01 ~]# systemctl reload nginx.service 
  [root@web01 ~]# systemctl enable nginx.service
  # 檢測埠
  [root@web01 ~]# netstat -lntup
  Active Internet connections (only servers)
  Proto Recv-Q Send-Q Local Address           Foreign 
  Address         State       PID/Program name    
  tcp        0      0 127.0.0.1:9000          0.0.0.0:*     
            LISTEN      9089/php-fpm: maste 
  tcp        0      0 0.0.0.0:3306            0.0.0.0:*     
            LISTEN      9448/mysqld         
  tcp        0      0 0.0.0.0:111             0.0.0.0:*     
            LISTEN      1/systemd           
  tcp        0      0 0.0.0.0:80              0.0.0.0:*     
            LISTEN      10362/nginx: master 
  tcp        0      0 0.0.0.0:22              0.0.0.0:*     
            LISTEN      6712/sshd           
  tcp        0      0 127.0.0.1:25            0.0.0.0:*     
            LISTEN      6843/master         
  tcp6       0      0 :::111                  :::*           
           LISTEN      1/systemd           
  tcp6       0      0 :::22                   :::*           
           LISTEN      6712/sshd           
  tcp6       0      0 ::1:25                  :::*           
           LISTEN      6843/master         
  # 設定密碼並連線資料庫
  [root@web01 ~]# mysqladmin -uroot password '456'
  [root@web01 ~]# mysql -uroot -p123
  Welcome to the MariaDB monitor.  Commands end with ; or 
  \g.
  Your MariaDB connection id is 7
  Server version: 5.5.65-MariaDB MariaDB Server

nginx連線PHP

使用模組 ngx_http_fastcgi_module

  ### 編輯nginx的配置檔案
  [root@web01 wzh]# cat /etc/nginx/conf.d/php.conf 
  # server層
  server {
  #監聽80埠
          listen 80;
  #指定域名
          server_name php.wzh.com;

  #當輸入域名,沒有接任何uri的時候,會走location /
          location / {
  #站點目錄:/code/wordpress
                  root /code/wzh;
  #index.php的程式碼,如果沒有index.php,那麼就找index.html頁面
                  index index.php index.html;
          }

  #當訪問到區分大小寫,以php結尾的url時
          location ~ \.php$ {
                  root /code/wzh;
                  # 代理後端的php服務
                  fastcgi_pass 127.0.0.1:9000;
                  # 預設頁面時index.php
                  fastcgi_index index.php;
  #當請求過來之後,會看這一行,  在/code/wordpress目錄下 wp-
  admin/setup-config.php,交給php解析
                  fastcgi_param  SCRIPT_FILENAME 
   $document_root$fastcgi_script_name;
  #包含fastcgi 變數解析檔案
                  include        fastcgi_params;
          }
  }
  # 根據配置檔案內容創建出相關的目錄並在目錄下寫一個info.php結尾的檔案
  [root@web01 ~]# mkdir /code/wzh -p
  [root@web01 ~]# cd /code/wzh
  [root@web01 wzh]# vim wzh_info.php
  [root@web01 wzh]# cat wzh_info.php 
  <?php
      phpinfo()
  ?>
  # 檢查nginx語法,重新載入nginx
  [root@web01 ~]# nginx -t
  [root@web01 ~]# systemctl reload nginx
  # 在hosts檔案裡面做域名解析
  windows+r開啟執行框輸入drivers,找到etc點開,然後用管理員身份開啟
  裡面的hosts檔案在下面做域名解析
  10.0.0.7    php.wzh.com
  # 開啟網站訪問

** 然後新增WordPress**

  # 首先把Wordpress的包上傳到虛擬機器
  # 然後解壓WordPress的壓縮包
  [root@web01 wzh]# tar xf wordpress-5.0.3-zh_CN.tar.gz 
  [root@web01 wzh]# ll
  total 10844
  drwxr-xr-x 5 1006 1006     4096 Jan 11  2019 wordpress
  -rw-r--r-- 1 root root 11098483 May 20 22:57 wordpress-
  5.0.3-zh_CN.tar.gz
  # 修改nginx和PHP連線配置的站點目錄
  # server層
  server {
  #監聽80埠
          listen 80;
  #指定域名
          server_name php.wzh.com;

  #當輸入域名,沒有接任何uri的時候,會走location /
          location / {
  #站點目錄:/code/wordpress
                  root /code/wzh/wordpress;
  #index.php的程式碼,如果沒有index.php,那麼就找index.html頁面
                  index index.php index.html;
          }

  #當訪問到區分大小寫,以php結尾的url時
          location ~ \.php$ {
                  root /code/wzh/wordpress;
                  # 代理後端的php服務
                  fastcgi_pass 127.0.0.1:9000;
                  # 預設頁面時index.php