天天看點

LAMP的實作

LAM(M)P的簡單說明:

    L: linux

    A: apache (httpd)

    M: mysql, mariadb

    M:memcached

    P: php, perl, python

說明:本實驗在centos7上實作,實驗的LAMP基于mariadb ,php,apache

注意:(1)關閉selinux

        vim   /etc/selinux/config

                SELINUX=disabled 

         (2)清除防火牆政策

                    iptables -F

1、安裝包 

    yum install httpd  mariadb-server  php

    yum install php-mysql    用于資料庫與PHP連接配接

2、啟動服務

     systemctl start httpd   mariadb  開啟httpd服務和資料庫mariadb服務

    ss -ntl 檢視端口是否打開

        httpd 預設80端口

        mariadb 預設3306端口

3、簡單實作LAP 

    (1)修改首頁面資訊  

            修改httpd的主配置檔案 vim  /etc/httpd/conf/httpd.conf

                               DirectoryIndex  index.php  index.html

    (2)編輯首頁面,用php代碼實作

            vim  /var/www/html/index.php          

<code>&lt;?php</code>

<code>            </code><code>phpinfo();</code>

<code>?&gt;</code>

             systemctl reload  httpd    重新加載服務

     (3)在浏覽器上測試:

                    http://192.168.191.107/ 

                出現下面資訊表示成功

4、實作LAMP

     (1)安裝完mariadb 預設任何人都能連接配接的,需要執行一個安全腳本

           mysql_secure_installation

          Enter current password for root (enter for none):       輸入之前的root密碼,回車表示沒有(這裡的root表示資料庫中的管理者使用者)

            Set root password? [Y/n] y       是否設定root密碼   ,y

            New password:                            輸入兩次密碼

            Re-enter new password: 

            Remove anonymous users? [Y/n] y        是否删除匿名登入,y  ,删除後僅資料庫中已存在的使用者可以登入

            Disallow root login remotely? [Y/n] n         是否允許root遠端連接配接

            Remove test database and access to it? [Y/n] n     是否删除test資料庫

            Reload privilege tables now? [Y/n] y        是否儲存以上設定

     (2)為了安全,在資料庫中,建立一個使用者,用來實作與php的連接配接

               1&gt;  mysql -uroot -pcentos  登入資料庫 root賬号,密碼centos

               2&gt;  create user 'test'@'192.168.191.%' identified by 'centos';     建立test使用者,密碼centos ,主機是192.168.191.#的都可以

               3&gt;mysql -utest -pcentos -h '192.168.191.107'   登入檢視,建立的普通使用者權限小,但是在這裡用來連接配接資料庫權限可以滿足,可以在後續管理資料庫中,對使用者授權。

     (3)寫測試代碼                     

     cd/var/www/html 

 vim index.php 

<code>mysql_connect(</code><code>'192.168.191.107'</code><code>,</code><code>'test'</code><code>,</code><code>'centos'</code><code>);</code>

<code>if</code> <code>(</code><code>$conn</code><code>)</code>

<code>        </code><code>echo</code> <code>"OK"</code><code>;</code>

<code>else</code>

<code>        </code><code>echo</code> <code>"Failure"</code><code>;</code>

<code>mysql_close();</code>

    (4) 重新開機 服務 systemctl reload mariadb

 5、測試

        方法(1)在浏覽器上測試http://192.168.191.107/

     方法(2)在另一台主機上

連接配接成功

本文轉自 hawapple 51CTO部落格,原文連結:http://blog.51cto.com/guanm/1974839