知識中心

搭建LNMP環境(CentOS 6)-阿裏雲ECS

2018-12-08 21:57:44 mimukeji

搭建LNMP環境(CentOS 6)-阿裏雲ECS



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

項目配置

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

  • 操作系統:CentOS 6.8 64位
  • Nginx版本:Nginx 1.10.2
  • MySQL版本:MySQL 5.6.24
  • PHP版本:PHP 5.6.23

適用對象

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

基本流程

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

  1. 准備編譯環境。
  2. 安裝Nginx。
  3. 安裝MySQL。
  4. 安裝PHP-FPM。
  5. 測試訪問。
步驟一:准備編譯環境。

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

  1. 使用向導創建實例。
    說明 本篇教程創建的ECS實例選用了CentOS 6.8 64位的操作系統,專有網絡和公網IP。
  2. 使用管理終端連接ECS實例。
  3. 輸入命令cat /etc/redhat-release查看系統版本。
    阿裏雲使用小妙招
  4. 關閉SELinux。
    1. 輸入getenforce命令查看當前SELinux的狀態。
      阿裏雲使用小妙招
    2. 如果SELinux狀態參數是Enforcing,則SELinux為開啟狀態。如果SELinux狀態參數是Disabled, 則SELinux為關閉狀態。如上圖所示,此處SELinux為開啟狀態,需要運行如下命令關閉SELinux:
      • 如果您想臨時關閉SELinux,輸入命令setenforce 0
        說明 這只是暫時關閉SELinux,下次重啟Linux後,SELinux還會開啟。
      • 如果您想永久關閉SELinux,輸入命令vi /etc/selinux/config編輯SELinux配置文件。回車後,把光標移動到SELINUX=enforcing這一行,按下i鍵進入編輯模式,修改為SELINUX=disabled, 按下Esc鍵,然後輸入:wq並回車以保存並關閉SELinux配置文件。
        說明 您可參考redhat關於SELinux的官方文檔來決定何時開啟SELinux。
    3. 重啟系統使設置生效。
  5. 參考添加安全組規則,放行所需端口入方向規則。
步驟二:安裝Nginx。
  1. 添加運行Nginx服務進程的用戶。
    groupadd -r nginx useradd -r -g nginx  nginx
  2. 下載源碼包解壓編譯。
    wget http://nginx.org/download/nginx-1.10.2.tar.gz
    tar xvf nginx-1.10.2.tar.gz -C /usr/local/src
    yum groupinstall "Development tools"
    yum -y install gcc wget gcc-c++ automake autoconf libtool libxml2-devel libxslt-devel perl-devel perl-ExtUtils-Embed pcre-devel openssl-devel
    cd /usr/local/src/nginx-1.10.2
    ./configure \
    --prefix=/usr/local/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_module make && make install
    mkdir -p /var/tmp/nginx/client
  3. 添加SysV啟動腳本。

    輸入命令vi /etc/init.d/nginx打開SysV啟動腳本文件,按下i鍵,然後在腳本文件中寫下如下內容:

     #!/bin/sh  #  # nginx - this script starts and stops the nginx daemon  #  # chkconfig:   - 85 15  # description: Nginx is an HTTP(S) server, HTTP(S) reverse \  #               proxy and IMAP/POP3 proxy server  # processname: nginx  # config:      /etc/nginx/nginx.conf  # config:      /etc/sysconfig/nginx  # pidfile:     /var/run/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/sbin/nginx" prog=$(basename $nginx)
    NGINX_CONF_FILE="/etc/nginx/nginx.conf" [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
    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 killall -9 nginx
    } restart() {
        configtest || return $?
        stop
        sleep 1
        start
    } reload() {
        configtest || return $? echo -n $"Reloading $prog: " killproc $nginx -HUP
    RETVAL=$? echo } force_reload() {
        restart
    } configtest() { $nginx -t -c $NGINX_CONF_FILE } rh_status() {
        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

    按下Esc鍵,然後輸入:wq並回車以保存並關閉SysV啟動腳本文件。

  4. 賦予腳本執行權限。
    chmod +x /etc/init.d/nginx
  5. 添加至服務管理列表,設置開機自啟。
    chkconfig --add nginx chkconfig  nginx on
  6. 啟動服務。
    service nginx start
  7. 登錄ECS管理控制台,單擊左側導航欄中的實例,在實例列表中找到正在部署環境的實例,從這個實例的IP地址項中複制它的公網IP,用瀏覽器訪問這個IP地址可看到默認歡迎頁面。
    阿裏雲使用小妙招
步驟叁:安裝MySQL。
  1. 准備編譯環境。
    yum groupinstall "Server Platform Development" "Development tools" -y yum install cmake -y
  2. 准備MySQL數據存放目錄。
    mkdir /mnt/data groupadd -r mysql useradd -r -g mysql -s /sbin/nologin mysql id mysql
  3. 更改數據目錄屬主和屬組。
    chown -R mysql:mysql /mnt/data
  4. 下載穩定版源碼包解壓編譯。
    wget https://downloads.mysql.com/archives/get/file/mysql-5.6.24.tar.gz tar xvf mysql-5.6.24.tar.gz -C  /usr/local/src cd /usr/local/src/mysql-5.6.24 cmake . -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 \
    -DMYSQL_UNIX_ADDR=/tmp/mysql.sock \
    -DDEFAULT_CHARSET=utf8 \
    -DDEFAULT_COLLATION=utf8_general_ci 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/
    說明 在CentOS 6.8版操作系統的最小安裝完成後,在/etc目錄下會存在一個my.cnf,需要將此文件更名為其他的名字,如/etc/my.cnf.bak,否則,該文件會幹擾源碼安裝的MySQL的正確配置,造成無法啟動。
  7. 複制配置文件和啟動腳本。
    cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
    chmod +x /etc/init.d/mysqld
    cp /usr/local/mysql/support-files/my-default.cnf /etc/my.cnf
  8. 設置開機自動啟動。
    cd
    chkconfig mysqld on chkconfig --add mysqld
  9. 修改配置文件中的安裝路徑及數據目錄存放路徑。
    echo -e "basedir = /usr/local/mysql\ndatadir = /mnt/data\n" >> /etc/my.cnf
  10. 設置PATH環境變量。
    echo "export PATH=$PATH:/usr/local/mysql/bin" > /etc/profile.d/mysql.sh source /etc/profile.d/mysql.sh
  11. 啟動服務。
    service mysqld start 
    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.23.tar.bz2/from/this/mirror
    cp mirror php-5.6.23.tar.bz2
    tar xvf php-5.6.23.tar.bz2 -C /usr/local/src
    cd /usr/local/src/php-5.6.23
    ./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-bz2 make && make install
  3. 添加PHP和PHP-FPM配置文件。
    cp /usr/local/src/php-5.6.23/php.ini-production /etc/php.ini cd /usr/local/php/etc/
    cp php-fpm.conf.default php-fpm.conf sed -i 's@;pid = run/php-fpm.pid@pid = /usr/local/php/var/run/php-fpm.pid@' php-fpm.conf
  4. 添加PHP-FPM啟動腳本。
    cp /usr/local/src/php-5.6.23/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
    chmod +x /etc/init.d/php-fpm
  5. 添加PHP-FPM至服務列表並設置開機自啟。
    chkconfig --add php-fpm  chkconfig --list php-fpm  chkconfig php-fpm on
  6. 啟動服務。
    service php-fpm start
  7. 添加Nginx對FastCGI的支持。
    1. 備份默認的Nginx配置文件。
      cp /etc/nginx/nginx.conf /etc/nginx/nginx.confbak cp /etc/nginx/nginx.conf.default /etc/nginx/nginx.conf
    2. 輸入命令vi /etc/nginx/nginx.conf打開Nginx的配置文件,按下i鍵,在所支持的主頁面格式中添加php格式的主頁,類似如下:
      location / {
        root   /usr/local/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 /usr/local/nginx/html;
    5. fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;改成fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html/$fastcgi_script_name;
    6. 按下Esc鍵,然後輸入:wq並回車以保存並關閉Nginx配置文件。
  8. 輸入命令service nginx reload重新載入Nginx的配置文件。
  9. 輸入命令vi /usr/local/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平台構建完成。


阿裏雲使用小妙招



如果遇到問題可以隨時聯系米姆,我們將免費為您提供阿裏雲基礎服務。

本文轉載自網絡,如有侵權,請聯系我們刪除。