CentOS 7系統下的LEMP環境搭建配置詳解

來源:互聯網
上載者:User

最近由於項目需求,將伺服器從CentOS6升級到CentOS7,對應的PHP版本也升級到PHP5.6。我們熟悉的有LEMP環境一鍵安裝包,但是本文我們將單獨安裝各個組件模組,並搭建一個完整的PHP運行平台。
我們常說的LNMP環境是指Linux/nginx/MySQL/PHP組合,而LEMP是什麼呢?其實Nginx的發音是Engine-X = E,LEMP包是由Linux、nginx、MariaDB/MySQL和PHP組成的,那麼看來LEMP和LNMP是一樣的,而現在業內習慣性的稱作LEMP。MariaDB是一款社區支援驅動的MySQL資料庫的分支,其功能更多效能更佳,所以我們在CentOS7下安裝MariaDB。CentOS7我已經安裝好了,現在只需安裝Nginx,MariaDB和PHP。
1、安裝Nginx
我們從nginx官方的RPM源來安裝一個預構建的穩定版本的nginx包。
$ sudo rpm --import http://nginx.org/keys/nginx_signing.key
$ sudo rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
$ sudo yum install nginx 
這樣,nginx就安裝上了,安裝完成後,nginx不會自動啟動。現在需要做的是讓nginx自動啟動,另外還要做些配置讓其可以隨著作業系統啟動而啟動。我們也需要在防火牆裡開啟TCP/80連接埠,以使得可以遠端存取nginx的web服務。所有這些操作、設定都只需要輸入如下命令就可實現。
$ sudo systemctl start nginx
$ sudo systemctl enable nginx
$ sudo firewall-cmd --zone=public --add-port=80/tcp --permanent
$ sudo firewall-cmd --reload 
Nginx已經啟動了,現在來測試nginx。nginx在CentOS7下的預設文件要目錄是/usr/share/nginx/html。預設的 index.html 檔案一定已經在這目錄下了。讓我們檢測下是否可以訪問到這個測試 web 頁,輸入 http://nginx的ip地址/訪問。
nginx
2、安裝MariaDB/MySQL
CentOS/RHEL 7使用了MariaDB替代了預設的 MySQL。作為MySQL的簡單替代品,MariaDB保證了與MySQL的API和命令列用法方面最大的相容性。下面是關於怎麼在 CentOS7上安裝和配置MaraDB/MySQL的操作樣本。
$ sudo yum install mariadb-server
$ sudo systemctl start mariadb
$ sudo systemctl enable mariadb 
在成功啟動MariaDB/MySQL服務後,還要進行資料庫的安全配置,如設定(非空)的root密碼、刪除匿名使用者、鎖定遠端存取。執行如下代碼:
$ sudo mysql_secure_installation
根據提示設定root密碼,以及刪除匿名使用者等操作。
3、安裝PHP
PHP是LEMP包中一個重要的組件,它負責把儲存在MariaDB/MySQL伺服器的資料取出產生動態內容。為了LEMP 需要,您至少需要安裝上PHP-FPM和PHP-MySQL兩個模組。PHP-FPM(FastCGI 進程管理器)實現的是nginx伺服器和產生動態內容的PHP應用程式的提供者。PHP-MySQL模組使PHP程式能訪問 MariaDB/MySQL資料庫。
首先檢查當前安裝的PHP包。
yum list installed | grep php
如果有安裝的PHP包,先刪除他們。
給yum安裝添加源包。
rpm -Uvh https://mirror.webtatic.com/yum/el7/epel-release.rpm
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
運行yum install。
使用yum命令來定製PHP引擎,安裝一些PHP擴充模組包。
yum install php56w.x86_64 php56w-cli.x86_64 php56w-common.x86_64 php56w-gd.x86_64 php56w-ldap.x86_64 php56w-mbstring.x86_64 php56w-mcrypt.x86_64 php56w-mysql.x86_64 php56w-pdo.x86_64
然後安裝PHP FPM。
yum install php56w-fpm
最後,啟動 PHP-FPM
$ sudo systemctl start php-fpm
$ sudo systemctl enable php-fpm
4、配置LEMP,讓Nginx支援PHP
首先,禁用隨PHP包安裝的httpd服務。
$ sudo systemctl disable httpd
接下來,配置nginx虛擬機器主機,使得nginx可以通過PHP-FPM來處理PHP的任務。用文字編輯器開啟/etc/nginx/conf.d/default.conf,然後按如下所示修改。
server {
    listen       80;
    server_name  localhost;
    root /usr/share/nginx/html;
    index index.php index.html index.htm;
 
    #charset koi8-r;
    #access_log  /var/log/nginx/log/host.access.log  main;
 
    location / {
    }
 
    #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 {
    }
 
    # 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$ {
        try_files $uri =404;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

然後,配置PHP, 修改/etc/php.ini。

cgi.fix_pathinfo=1
date.timezone = PRC
最後,測試nginx是否能處理PHP頁面。在測試之前,請確保重啟nginx和PHP-FPM。
$ sudo systemctl restart nginx
$ sudo systemctl restart php-fpm
建立一個叫名叫test.php的檔案,然後寫入如下內容,並放入/usr/share/nginx/html/目錄。
<?php
phpinfo();
?>
開啟瀏覽器,輸入 http://nginx的IP地址/test.php 。看到以下介面則LEMP安裝完成。

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.