部署LNMP動態網站

部署LNMP動態網站,實現如下目標:php

  1. 安裝LNMP平臺相關軟件
  2. 配置Nginx實現動靜分離
  3. 配置數據庫,建立帳戶與密碼
  4. 上線Wordpress代碼
  5. 使用Wordpress後臺管理界面,調整Wordpress版式

    實現此案例須要按照以下步驟進行。html

    步驟一:安裝部署LNMP軟件mysql

    備註:mariadb(數據庫客戶端軟件)、mariadb-server(數據庫服務器軟件)、mariadb-devel(其餘客戶端軟件的依賴包)、php(解釋器)、php-fpm(進程管理器服務)、php-mysql(PHP的數據庫擴展包)。nginx

    1)安裝軟件包web

    1. [root@centos7 ~]# yum -y install gcc openssl-devel pcre-devel
    2. [root@centos7 ~]# useradd -s /sbin/nologin nginx
    3. [root@centos7 ~]# tar -xvf nginx-1.12.2.tar.gz
    4. [root@centos7 ~]# cd nginx-1.12.2
    5. [root@centos7 nginx-1.12.2]# ./configure \
    6. --user=nginx --group=nginx \
    7. --with-http_ssl_module \
    8. --with-http_stub_status_module
    9. [root@centos7 nginx-1.12.2]# make && make install
    10. [root@centos7 ~]# yum -y install mariadb mariadb-server mariadb-devel
    11. [root@centos7 ~]# yum -y install php php-mysql php-fpm

    2)啓動服務(nginx、mariadb、php-fpm)sql

    1. [root@centos7 ~]# /usr/local/nginx/sbin/nginx                 #啓動Nginx服務
    2. [root@centos7 ~]# echo "/usr/local/nginx/sbin/nginx" >> /etc/rc.local
    3. [root@centos7 ~]# chmod +x /etc/rc.local
    4. [root@centos7 ~]# ss -utnlp | grep :80                        #查看端口信息
    5. [root@centos7 ~]# systemctl start mariadb         #啓動mariadb服務器
    6. [root@centos7 ~]# systemctl enable mariadb     
    7.     
    8. [root@centos7 ~]# systemctl start php-fpm         #啓動php-fpm服務
    9. [root@centos7 ~]# systemctl enable php-fpm

    附加知識:systemd!!!數據庫

    源碼安裝的軟件默認沒法使用systemd管理,若是須要使用systemd管理源碼安裝的軟件須要手動編寫服務的service文件(編寫是能夠參考其餘服務的模板文件)。如下是nginx服務最終編輯好的模板。apache

    Service文件存儲路徑爲/usr/lib/system/system/目錄。vim

    1. [root@centos7 ~]# vim /usr/lib/systemd/system/nginx.service
    2. [Unit]
    3. Description=The Nginx HTTP Server
    4. #描述信息
    5. After=network.target remote-fs.target nss-lookup.target
    6. #指定啓動nginx以前須要其餘的其餘服務,如network.target等
    7. [Service]
    8. Type=forking
    9. #Type爲服務的類型,僅啓動一個主進程的服務爲simple,須要啓動若干子進程的服務爲forking
    10. ExecStart=/usr/local/nginx/sbin/nginx
    11. #設置執行systemctl start nginx後須要啓動的具體命令.
    12. ExecReload=/usr/local/nginx/sbin/nginx -s reload
    13. #設置執行systemctl reload nginx後須要執行的具體命令.
    14. ExecStop=/bin/kill -s QUIT ${MAINPID}
    15. #設置執行systemctl stop nginx後須要執行的具體命令.
    16. [Install]
    17. WantedBy=multi-user.target

    3)修改Nginx配置文件,實現動靜分離centos

    修改配置文件,經過兩個location實現動靜分離,一個location匹配動態頁面,一個loation匹配其餘全部頁面。

    注意修改默認首頁爲index.php!

    1. [root@centos7 ~]# vim /usr/local/nginx/conf/nginx.conf
    2. ...省略部分配置文件內容...
    3. location / {
    4. root html;
    5. index index.php index.html index.htm;
    6. }
    7. ...省略部分配置文件內容...
    8. location ~ \.php$ {
    9. root html;
    10. fastcgi_pass 127.0.0.1:9000;
    11. fastcgi_index index.php;
    12. include fastcgi.conf;
    13. }
    14. ...省略部分配置文件內容...
    15. [root@centos7 ~]# /usr/local/nginx/sbin/nginx -s reload            #從新加載配置

    4)配置數據庫帳戶與權限

    爲網站提早建立一個數據庫、添加帳戶並設置該帳戶有數據庫訪問權限。

    1. [root@centos7 ~]# mysql
    2. MariaDB [(none)]> create database wordpress character set utf8mb4;
    3. MariaDB [(none)]> grant all on wordpress.* to wordpress@'localhost' identified by 'wordpress';
    4. MariaDB [(none)]> grant all on wordpress.* to wordpress@'192.168.2.11' identified by 'wordpress';
    5. MariaDB [(none)]> flush privileges;
    6. MariaDB [(none)]> exit

    提示:在mysql和mariadb中%表明匹配全部,這裏是受權wordpress用戶能夠從任意主機鏈接數據庫服務器,生產環境建議僅容許特定的若干主機訪問數據庫服務器。

    步驟二:上線wordpress代碼

    1)上線PHP動態網站代碼

    1. [root@centos7 ~]# unzip wordpress.zip
    2. [root@centos7 ~]# cd wordpress
    3. [root@centos7 wordpress]# tar -xf wordpress-5.0.3-zh_CN.tar.gz
    4. [root@centos7 wordpress]# cp -r wordpress/* /usr/local/nginx/html/
    5. [root@centos7 wordpress]# chown -R apache.apache /usr/local/nginx/html/

    提示:動態網站運行過程當中,php腳本須要對網站目錄有讀寫權限,而php-fpm默認啓動用戶爲apache。

    2)初始化網站配置(使用客戶端訪問web服務器IP)

    1. [root@client ~]# firefox http://192.168.2.11/
相關文章
相關標籤/搜索