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
原文地址:https://www.cnblogs.com/zabcd/p/13366715.html