CentOS7系統手動部署LNMP環境操作演示

gydtep發表於2019-02-14

本文件介紹如何手動在ECS例項上搭建LNMP環境(CentOS 7),其中LNMP分別代表Linux、Nginx、MySQL和PHP。

專案配置

本篇教程在示例步驟中使用了以下版本的軟體。操作時,請您以實際軟體版本為準。

  • 作業系統:CentOS 7.2 64位

  • Nginx版本:Nginx 1.10.2

  • MySQL版本:MySQL 5.6.24

  • PHP版本:PHP 5.6.38

適用物件

適用於熟悉Linux作業系統,剛開始使用阿里雲進行建站的個人使用者。

基本流程

使用雲伺服器ECS搭建LNMP平臺的操作步驟如下:

  1. 準備編譯環境。

  2. 安裝Nginx。

  3. 安裝MySQL。

  4. 安裝PHP-FPM。

  5. 測試訪問。

步驟一:準備編譯環境。

本文主要說明手動安裝LNMP平臺的操作步驟,您也可以在 雲市場 購買LNMP映象直接啟動ECS,以便快速建站。

  1. 使用嚮導建立例項

    說明 本篇教程建立的ECS例項選用了CentOS 7.2 64位的作業系統,專有網路和公網IP。

  2. 使用管理終端連線ECS例項

  3. 輸入命令cat /etc/redhat-release檢視系統版本。
    154391310132170_zh-CN.png

  4. 關閉防火牆。

    輸入systemctl status firewalld命令檢視當前防火牆的狀態。


    154391310132172_zh-CN.png

    如果防火牆的狀態引數是active,則防火牆為開啟狀態。如果防火牆的狀態引數是inactive,則防火牆為關閉狀態。如上圖所示,此處防火牆為開啟狀態,需要執行如下命令關閉防火牆:

  • 如果您想臨時關閉防火牆,輸入命令systemctl stop firewalld

    說明 這只是暫時關閉防火牆,下次重啟Linux後,防火牆還會開啟。

  • 如果您想永久關閉防火牆,輸入命令systemctl disable firewalld

    說明 您可參考firewalld官網資訊來決定何時開啟防火牆。

關閉SELinux。

  • 如果您想臨時關閉SELinux,輸入命令setenforce 0

    說明 這只是暫時關閉SELinux,下次重啟Linux後,SELinux還會開啟。

  • 如果您想永久關閉SELinux,輸入命令vi /etc/selinux/config編輯SELinux配置檔案。回車後,把游標移動到SELINUX=enforcing這一行,按下i鍵進入編輯模式,修改為SELINUX=disabled, 按下Esc鍵,然後輸入:wq並回車以儲存並關閉SELinux配置檔案。

    說明 您可參考redhat關於SELinux的官方文件來決定何時開啟SELinux。

  1. 輸入getenforce命令檢視當前SELinux的狀態。
    154391310121065_zh-CN.png

  2. 如果SELinux狀態引數是Enforcing,則SELinux為開啟狀態。如果SELinux狀態引數是Disabled, 則SELinux為關閉狀態。如上圖所示,此處SELinux為開啟狀態,需要執行如下命令關閉SELinux:

  3. 重啟系統使設定生效。

參考新增安全組規則,放行所需埠入方向規則。

步驟二:安裝Nginx。

  1. 安裝依賴包。

    yum groupinstall "Development tools" -yyum install zlib-devel pcre-devel openssl-devel -yyum install epel-release -yyum install perl perl-devel perl-ExtUtils-Embed libxslt libxslt-devel libxml2 libxml2-devel gd gd-devel GeoIP GeoIP-devel -y
  2. 下載原始碼包解壓編譯。

    wget http://nginx.org/download/nginx-1.10.2.tar.gz
    tar xvf nginx-1.10.2.tar.gz -C /usr/local/src
    cd /usr/local/src/nginx-1.10.2
    ./configure --prefix=/etc/nginx 
    --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/tmp/nginx/client --http-proxy-temp-path=/var/tmp/nginx/proxy --http-fastcgi-temp-path=/var/tmp/nginx/fcgi --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi --http-scgi-temp-path=/var/tmp/nginx/scgi --user=nginx --group=nginx --with-pcre --with-http_v2_module --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-http_auth_request_module --with-mail --with-mail_ssl_module --with-file-aio --with-ipv6 --with-http_v2_module --with-threads --with-stream --with-stream_ssl_modulemake && make install
    mkdir -p /var/tmp/nginx/client
  3. 輸入命令nginx -v可檢視Nginx的版本號。
    154391310132179_zh-CN.png

  4. 新增執行Nginx服務程式的使用者。

    useradd nginx
    chown -R nginx:nginx /etc/nginx/
  5. 新增nginx.service啟動配置檔案。

    輸入命令vi /usr/lib/systemd/system/nginx.service開啟Nginx的啟動配置檔案,按下i鍵,然後在配置檔案中寫下如下內容:

    [Unit]Description=nginx - high performance web serverDocumentation=https://nginx.org/en/docs/After=network-online.target remote-fs.target nss-lookup.targetWants=network-online.target 
    
    [Service]Type=forkingPIDFile=/var/run/nginx.pidExecStartPre=/usr/sbin/nginx -t -c /etc/nginx/nginx.confExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.confExecReload=/bin/kill -s HUP $MAINPIDExecStop=/bin/kill -s TERM $MAINPID [Install] 
    WantedBy=multi-user.target

    按下Esc鍵,然後輸入:wq並回車以儲存並關閉Nginx啟動配置檔案。

  6. 啟動Nginx服務並設定開機自動啟動。

    systemctl start nginx
    systemctl enable nginx
  7. 登入ECS管理控制檯,單擊左側導航欄中的例項,在例項列表中找到正在部署環境的例項,從這個例項的IP地址項中複製它的公網IP,用瀏覽器訪問這個IP地址可看到預設歡迎頁面。
    154391310121168_zh-CN.png

步驟三:安裝MySQL。

  1. 準備編譯環境。

    yum install ncurses-devel bison gnutls-devel –y
    yum install cmake -y
  2. 準備MySQL資料存放目錄。

    mkdir /mnt/datagroupadd -r mysqluseradd -r -g mysql -s /sbin/nologin mysqlid mysql
  3. 更改資料目錄屬主和屬組。

    chown -R mysql:mysql /mnt/data
  4. 下載穩定版原始碼包解壓編譯。

    wget https://downloads.mysql.com/archives/get/file/mysql-5.6.24.tar.gztar xvf mysql-5.6.24.tar.gz -C  /usr/local/srccd /usr/local/src/mysql-5.6.24cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql 
    > -DMYSQL_DATADIR=/mnt/data 
    > -DSYSCONFDIR=/etc 
    > -DWITH_INNOBASE_STORAGE_ENGINE=1 
    > -DWITH_ARCHIVE_STORAGE_ENGINE=1 
    > -DWITH_BLACKHOLE_STORAGE_ENGINE=1 
    > -DWITH_READLINE=1 
    > -DWITH_SSL=system 
    > -DWITH_ZLIB=system 
    > -DWITH_LIBWRAP=0 
    > -DMYSQL_TCP_PORT=3306 
    > -DDEFAULT_CHARSET=utf8 
    > -DDEFAULT_COLLATION=utf8_general_ci 
    > -DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock 
    > -DWITH_SYSTEMD=1 
    > -DINSTALL_SYSTEMD_UNITDIR=/usr/lib/systemd/system 
    make && make install
  5. 修改安裝目錄的屬組為mysql。

    chown -R mysql:mysql /usr/local/mysql/
  6. 初始化資料庫並複製配置檔案。

    cd /usr/local/mysql/usr/local/mysql/scripts/mysql_install_db --user=mysql --datadir=/mnt/data/mv /etc/my.cnf /etc/my.cnf.bak
    cp /usr/local/mysql/support-files/my-default.cnf /etc/my.cnf
  7. 修改配置檔案中的安裝路徑及資料目錄存放路徑。

    echo -e "basedir = /usr/local/mysql
    datadir = /mnt/data
    " >> /etc/my.cnf
  8. 新增mysql.service啟動配置檔案。

    輸入命令vi /usr/lib/systemd/system/mysql.service開啟MySQL的啟動配置檔案,按下i鍵,然後在配置檔案中寫下如下內容:

    [Unit]Description=MySQL Community ServerAfter=network.targetAfter=syslog.target
    
    [Install]WantedBy=multi-user.targetAlias=mysql.service
    
    [Service]User=mysqlGroup=mysqlPermissionsStartOnly=trueExecStart=/usr/local/mysql/bin/mysqldTimeoutSec=600Restart=alwaysPrivateTmp=false

    按下Esc鍵,然後輸入:wq並回車以儲存並關閉MySQL啟動配置檔案。

  9. 設定PATH環境變數。

    echo "export PATH=$PATH:/usr/local/mysql/bin" > /etc/profile.d/mysql.shsource /etc/profile.d/mysql.sh
  10. 設定開機啟動MySQL。

    systemctl enable mysql
  11. 啟動MySQL服務。

    systemctl start mysql
    mysql –h 127.0.0.1

步驟四:安裝PHP-FPM。

Nginx作為web伺服器,當它接收到請求後,不支援對外部程式的直接呼叫或者解析,必須通過FastCGI進行呼叫。如果是PHP請求,則交給PHP直譯器處理,並把結果返回給客戶端。PHP-FPM是支援解析PHP的一個FastCGI程式管理器。提供了更好管理PHP程式的方式,可以有效控制記憶體和程式、可以平滑過載PHP配置。

  1. 安裝依賴包。

    yum install libmcrypt libmcrypt-devel mhash mhash-devel libxml2 libxml2-devel bzip2 bzip2-devel
  2. 下載穩定版原始碼包解壓編譯。

    wget http://cn2.php.net/get/php-5.6.38.tar.bz2/from/this/mirror
    cp mirror php-5.6.38.tar.bz2
    tar xvf php-5.6.38.tar.bz2 -C /usr/local/src
    cd /usr/local/src/php-5.6.38
    ./configure --prefix=/usr/local/php 
    --with-config-file-scan-dir=/etc/php.d --with-config-file-path=/etc --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --with-openssl --enable-xml --enable-sockets --enable-fpm --with-mcrypt --with-bz2make && make install
  3. 新增PHP和PHP-FPM配置檔案。

    cp /usr/local/src/php-5.6.38/php.ini-production /etc/php.inicd /usr/local/php/etc/
    cp php-fpm.conf.default php-fpm.confsed -i `s@;pid = run/php-fpm.pid@pid = /usr/local/php/var/run/php-fpm.pid@` php-fpm.conf
  4. 新增php-fpm.service啟動配置檔案。

    輸入命令vi /usr/lib/systemd/system/php-fpm.service開啟PHP-FPM的啟動配置檔案,按下i鍵,然後在配置檔案中寫下如下內容:

    [Unit]Description=The PHP FastCGI Process ManagerAfter=network.target[Service]Type=simplePIDFile=/usr/local/php/var/run/php-fpm.pid ExecStart=/usr/local/php/sbin/php-fpm --nodaemonize --fpm-config /usr/local/php/etc/php-fpm.confExecReload=/bin/kill -USR2 $MAINPIDPrivateTmp=true[Install]WantedBy=multi-user.target

    按下Esc鍵,然後輸入:wq並回車以儲存並關閉PHP-FPM啟動配置檔案。

  5. 啟動PHP-FPM服務並設定開機自動啟動。

    systemctl start php-fpm
    systemctl enable php-fpm
  6. 啟動服務。

    service php-fpm start
  7. 新增Nginx對FastCGI的支援。

    1. 備份預設的Nginx配置檔案。

      cp /etc/nginx/nginx.conf /etc/nginx/nginx.confbakcp nginx.conf.default nginx.conf.default.bakcp /etc/nginx/nginx.conf.default /etc/nginx/nginx.conf
    2. 輸入命令vi /etc/nginx/nginx.conf開啟Nginx的配置檔案,按下i鍵,在所支援的主頁面格式中新增PHP格式的主頁,類似如下:

      location / {
        root   /etc/nginx/html;  index  index.php index.html index.htm;
      }
    3. 取消以下內容前面的註釋:

      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;
      }
    4. root html;改成root /etc/nginx/html;

    5. fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;改成fastcgi_param SCRIPT_FILENAME /etc/nginx/html/$fastcgi_script_name;

    6. 按下Esc鍵,然後輸入:wq並回車以儲存並關閉Nginx配置檔案。

  8. 輸入命令systemctl restart nginx重新載入Nginx的配置檔案。

  9. 輸入命令vi /etc/nginx/html/index.php開啟index.php檔案,按下i鍵,然後在檔案中寫入如下內容:

    <?php$conn=mysql_connect(`127.0.0.1`,`root`,``);if ($conn){echo "LNMP platform connect to mysql is successful!";
    }else{echo "LNMP platform connect to mysql is failed!";
    }
    phpinfo();?>
  10. 按下Esc鍵,然後輸入:wq並回車以儲存並關閉index.php檔案。

步驟五:測試訪問

登入 ECS管理控制檯,單擊左側導航欄中的例項,在例項列表中複製正在部署環境的例項的公網IP地址。用瀏覽器訪問這個公網IP地址,如您看見如下圖所示頁面,則表示LNMP平臺構建完成。


154391310132205_zh-CN.png


相關文章