CentOS 6.6 安裝 NginX、MariaDB、php ( LNMP )

Nginx 是一套輕量級的 Web Server,和 Apache Web Server 比起來,由俄羅斯程式設計師 Igor Sysoev 所開發輕量級的網頁伺服器,軟體以 BSD-like 授權,可以在 UNIX、GNU/Linux、BSD、Mac OS X、Solaris,以及Microsoft Windows 等作業系統中執行。

Nginx 官方網站:http://nginx.org/



目前有人在 Linux 使用它來取代 Apache Web Server,並把這個組合稱為 LNMP(Linux + Nginx + MySQL + PHP)

安裝方式:
因為 Nginx 並不是 CentOS 官方套件,所以必須先新增 Nginx 官方所提供的第三方套件庫
# vim /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1

匯入憑證
# rpm --import http://nginx.org/keys/nginx_signing.key


# wget http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm
# rpm -ivh http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm

更新套件庫
# yum update

安裝 Nginx Web Server
# yum install nginx

執行 Nginx Web Server
# service nginx start
Starting nginx:                                            [  OK  ]

設定開機預設啟動 Nginx 和關閉 Apache Web Server
# chkconfig --level 3 nginx on
# chkconfig --level 3 httpd off

開啟瀏覽器,連線到 Nginx Web Server



安裝MariaDB前,必須先新增MariaDB的知識庫,讓系統使用。

vi /etc/yum.repos.d/mariadb.repo
32位元系統

[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/5.5/centos6-x86
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1

64位元系統

[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/5.5/centos6-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1

然後更新CentOS。

yum update -y


開始安裝 MariaDB

開始安裝MariaDB伺服器及用戶端(登入、備份用)。

yum install MariaDB-devel MariaDB-client MariaDB-server -y
安裝好後啟動MariaDB,MariaDB叫做【mysql】跟MySQL的【mysqld】不一樣!

service mysql start
設定開機啟動MariaDB。

chkconfig mysql on

初始化 MariaDB

執行這個初始化程式,過程跟MySQL一模一樣。

/usr/bin/mysql_secure_installation
第一個問題,Enter current password for root (enter for none):。

請直接按下Enter,因為預設MariaDB沒有密碼。


第二個問題,Change the root password? [Y/n]。

是否更改root密碼,極度建議您設定root密碼,請輸入Y。

第三個問題,Remove anonymous users? [Y/n]。

是否移除匿名帳號,請務必移除匿名帳號,否則別人隨便就可以進入您的資料庫了!

第四個問題,Disallow root login remotely? [Y/n]。

是否移除遠端root登入權限,視需求設定,若要允許root遠端登入,還需要使用另一個指令新增權限,後面會說明!

第五個問題,Remove test database and access to it? [Y/n]。

是否移除測試資料庫跟使用者,留著也沒用!移除請輸入Y。

第六個問題,Reload privilege tables now? [Y/n]。

是否刷新權限表,輸入Y完成所有初始化設定!

讓遠端可以存取root帳號

如果您要使用管理工具從自己的電腦管理MariaDB,必須開放遠端存取權限,所以來建立一個可以遠端存取的root帳號!

一、使用root帳號登入MariaDB,注意:以下步驟只能在MariaDB那台主機上操作。

mysql -u root -p
二、執行下面那段指令後,所有遠端電腦都可以登入您的root帳號,若您是固定IP,也可以將指令中的【%】改成您的IP。

GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '設定密碼' WITH GRANT OPTION;






使用mysql-workbench查看
 使用navaicat查看


安裝php

yum -y php php-fpm

vi /etc/nginx/conf.d/default.conf
將設定檔修改成下列,讓nginx可以執行php

server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/log/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.php index.html index.htm;
    }

    #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 {
        root   /usr/share/nginx/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$ {
        root           html;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /usr/share/nginx/html$fastcgi_script_name;
        include        fastcgi_params;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}


設定php.ini
vi /etc/php.ini

cgi.fix_pathinfo=1


切換nginx網頁目錄增加phpinfo文件

cd /usr/share/nginx/html

vi phpinfo.php
<?php
    phpinfo();
?>






同場加映:安裝phpmyadmin於mariadb版

留言

這個網誌中的熱門文章

c語言-關於#define用法

CMD常用網管指令

PHP 與 JavaScript 之間傳值利用 json