天天看點

ECS 7 天實踐訓練營(三)- 搭建公衆号管理系統微擎

一、 介紹

微擎是一家開源的 公衆平台 SAAS 領域技術服務提供商,成立于 2013 年 7 月,總部位于 安徽 宿州。微擎是免費開源的公衆平台管理系統,擁有近百萬的注冊使用者數,開發者認證超 20,000 名,3,000 + 款應用插件,10,000 + 應用場景,服務規模超 200,000 家,直接間接服務使用者過億 -- 百度百科

二、搭建 LNMP 環境

  1. 檢視 wget 是否安裝
    `rpm -qa wget`
    
    沒有則安裝
    
    `yum install wget`
               
  2. 檢視是否安裝編譯器
    `rpm -qa gcc`
    
    沒有則安裝
    
    `yum install gcc gcc-c++`
               
  3. 安裝 Nginx
  • 安裝 Nginx 依賴:
yum -y install pcre pcre-devel
     yum -y install zlib zlib-devel
     yum -y install openssl openssl-devel           
  • 下載下傳并解壓 Nginx:
cd /usr/local/src
     wget http://nginx.org/download/nginx-1.18.0.tar.gz
     tar -zxvf nginx-1.18.0.tar.gz           
  • 編譯安裝 Nginx:
cd nginx-1.18.0
     ./configure --prefix=/usr/local/nginx
     make
     make install           
  • 建立 nginx 賬号:
groupadd nginx
     useradd -M -g nginx -s /sbin/nologin nginx
     cd /usr/local/nginx/conf           

vim nginx.conf

将第一行 user 改為:

user nginx nginx

完成後執行指令

/usr/local/nginx/sbin/nginx -t

ESC+:wq+Enter 退出并儲存

  1. 啟動 Nginx:

    /usr/local/nginx/sbin/nginx

    ,浏覽器輸入伺服器公網 IP,出現 Welcome to nginx! 則啟動成功
  2. 安裝 MySQL
  • 下載下傳并應用 MySQL 的 yum 資源包:
cd ~
     wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm
     rpm -ivh mysql-community-release-el7-5.noarch.rpm
     yum update
     yum install mysql-server           
  • 改變檔案屬主和屬組:
    `chown mysql:mysql -R /var/lib/mysql`
               
  • 初始化并啟動 MySQL:
    `mysqld --initialize`
    
    `service mysqld start`
               
  • 修改密碼并登陸 MySQL:
    `mysqladmin -u root password "your-password"`
    
    `mysql -u root -p`
    
    輸入密碼登陸,出現`mysql>`則成功
    
               
  1. 安裝 PHP
  • 安裝 PHP 依賴:
yum install libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel           
  • 下載下傳 PHP:
    `wget https://www.php.net/distributions/php-7.3.21.tar.gz`
    
    `tar -zxvf php-7.3.21.tar.gz`
               
  • 編譯安裝:
cd php-7.3.21
     ./configure --prefix=/usr/local/php --disable-fileinfo --enable-fpm --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-openssl --with-zlib --with-curl --enable-ftp --with-gd --with-xmlrpc --with-jpeg-dir --with-png-dir --with-freetype-dir --enable-gd-native-ttf --enable-mbstring --with-mcrypt=/usr/local/libmcrypt --enable-zip --enable-mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-mysql-sock=/var/lib/mysql/mysql.sock --without-pear --enable-bcmath
     make
     make install           
  • 移動 PHP 配置檔案:
    `cp php.ini-development /etc/php.ini`
               
  • 配置 php-fpm 運作賬号:
cd /usr/local/php/etc
     cp php-fpm.conf.default php-fpm.conf
     cd php-fpm.d
     cp www.conf.default www.conf           

vim www.conf

将其中的

user=nobody

group=nobody

改為

user=nginx

group=nginx

  1. 使 Nginx 支援 PHP
  • 修改配置檔案:

    vim /usr/local/nginx/conf/nginx.conf

#location ~ .php$ {
         #    root           html;
         #    fastcgi\_pass   127.0.0.1:9000;
         #    fastcgi\_index  index.php;
         #    fastcgi\_param  SCRIPT\_FILENAME  /scripts$fastcgi\_script\_name;
         #    include        fastcgi\_params;
         #}           

将其中的改為注釋取消,ESC+:wq+Enter 退出并儲存

  • 将 Nginx 添加到系統服務:
    `vim /etc/init.d/nginx`
    
    ​ 内容為:
    
               
#!/bin/sh
     # nginx - this script starts and stops the nginx daemin
     #
     # chkconfig:   - 85 15
     # description:  Nginx is an HTTP(S) server, HTTP(S) reverse 
     #               proxy and IMAP/POP3 proxy server
     # processname: nginx
     # config:      /usr/local/nginx/conf/nginx.conf
     # pidfile:     /usr/local/nginx/logs/nginx.pid
     # Source function library.
     . /etc/rc.d/init.d/functions
     # Source networking configuration.
     . /etc/sysconfig/network
     # Check that networking is up.
     \[ "$NETWORKING" = "no" \] && exit 0
          nginx="/usr/local/nginx/sbin/nginx"
          prog=$(basename $nginx)
          NGINX\_CONF\_FILE="/usr/local/nginx/conf/nginx.conf"
          lockfile=/var/lock/subsys/nginx
          start() {
              \[ -x $nginx \] || exit 5
              \[ -f $NGINX\_CONF\_FILE \] || exit 6
              echo -n $"Starting $prog: "
              daemon $nginx -c $NGINX\_CONF\_FILE
              retval=$?
              echo
              \[ $retval -eq 0 \] && touch $lockfile
              return $retval
          }
             stop() {
              echo -n $"Stopping $prog: "
              killproc $prog -QUIT
              retval=$?
              echo
              \[ $retval -eq 0 \] && rm -f $lockfile
              return $retval
          }
                      restart() {
              configtest || return $?
              stop
              start
          }
               reload() {
              configtest || return $?
              echo -n $"Reloading $prog: "
              killproc $nginx -HUP
              RETVAL=$?
              echo
          }
          force\_reload() {
              restart
          }
               configtest() {
            $nginx -t -c $NGINX\_CONF\_FILE
          }
                    rh\_stlatus() {
              status $prog
          }
               rh\_status\_q() {
              rh\_status >/dev/null 2>&1
          }
          case "$1" in
              start)
                  rh\_status\_q && exit 0
             $1
             ;;
              stop)
                       rh\_status\_q || exit 0
             $1
             ;;
              restart|configtest)
             $1
             ;;
              reload)
             rh\_status\_q || exit 7
             $1
             ;;
                   force-reload)
             force\_reload
             ;;
         status)
             rh\_status
             ;;
                   condrestart|try-restart)
                  rh\_status\_q || exit 0
                 ;;
              \*)
                  echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
             exit 2
          esac           

chmod 755 /etc/init.d/nginx

chkconfig –add nginx

  • 重新開機 Nginx:
    `service nginx restart`
    
               
  1. 設定 php-fpm 為系統服務:
  • 添加配置檔案:
    `vim /etc/systemd/system/php-fpm.service`
    
    内容為
    
               
\[Unit\]
          Description=php-fpm
          After=network.target
          \[Service\]
          Type=forking
          ExecStart=/usr/local/php/sbin/php-fpm
          PrivateTmp=True
          \[Install\]
          WantedBy=multi-user.target           
  1. 設定 php-fpm 開機自啟并啟動 php-fpm:
  • 設定 php-fpm 開機自啟動:
    `systemctl enable php-fpm.service`
               
  • 啟動 php-fpm:
    `systemctl start php-fpm.service`
               
  • 檢視啟動是否成功:
    `ps aux | grep php-fpm`
    
               

三、搭建微擎

  1. 建立站點:
  • 建立 Nginx 配置檔案
    `mkdir -p /usr/local/nginx/vhost/weengine`
    
    `vim /usr/local/nginx/vhost/vhost_weengine.conf`
    
    内容為:
    
               
server {
             listen       80;
             server\_name  localhost;
                  #charset koi8-r;
                  #access\_log  logs/host.access.log  main;
                  location / {
                 root   vhost/weengine;
                 index  index.html index.htm;
             }
                  #error\_page  404              /404.html;
                  # redirect server error pages to the static page /50x.html
             #
             error\_page   500 502 503 504  /50x.html;
             location = /50x.html {
                 root   html;
             }
                  # proxy the PHP scripts to Apache listening on 127.0.0.1:80
             #
             #location ~ .php$ {
             #    proxy\_pass   http://127.0.0.1;
             #}
                  # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
             #
             location ~ .php$ {
                 root           html;
                 fastcgi\_pass   127.0.0.1:9000;
                 fastcgi\_index  index.php;
                 fastcgi\_param  SCRIPT\_FILENAME /usr/local/nginx/vhost/weengine$fastcgi\_script\_name;
                 include        fastcgi\_params;
             }
                  # deny access to .htaccess files, if Apache's document root
             # concurs with nginx's one
             #
             #location ~ /.ht {
             #    deny  all;
             #}
         }
          vim /usr/local/nginx/conf/nginx.conf
           修改nginx預設端口listen       800;
          include /usr/local/nginx/vhost/\*.conf;           

vim /usr/local/nginx/conf/nginx.conf

在 http{} 代碼塊最後添加

include /usr/local/nginx/vhost/*.conf;

重新開機 nginx

service nginx restart

  • 下載下傳并解壓微擎
    `cd /usr/local/nginx/vhost/weengine`
    
    `wget https://cdn.w7.cc/download/WeEngine-Laster-Online.zip`
    
    `unzip https://cdn.w7.cc/download/WeEngine-Laster-Online.zip`
    
               
  1. 配置微擎
  • 浏覽器輸入公網 IP/install.php 按提示一步步配置即可

參考文章:

LNMP 環境搭建 (linux+Nginx + Mysql + PHP) service nginx start|stop|reload 報錯:Failed to reload nginx.service: Unit not found.【解決方案】