1. 程式人生 > >記錄:CentOS7.2配置LNMP環境記錄

記錄:CentOS7.2配置LNMP環境記錄

記錄:CentOS7.2配置LNMP環境記錄

CentOS7.2配置LNMP環境記錄

php 5.6+ nginx 1.10+ mysql 5.5+

LNMP是Linux、Nginx、MySQL(MariaDB)和PHP的縮寫,這個組合是最常見的WEB伺服器的執行環境之一。本文將帶領大家在CentOS 7作業系統上搭建一套LNMP環境。

本教程適用於CentOS 7.x版本。

準備工作

更新 yum 源,自帶的源沒有 PHP5.6 :

rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.
noarch.rpm
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm

安裝 epel:

yum install epel-release

升級系統

yum update

準備工作完成,開始安裝!

安裝Nginx

CentOS系統模板中配置了內網源,下載速度較快,推薦使用yum安裝Nginx:

sudo yum install nginx

按照提示,輸入yes後開始安裝。安裝完畢後,Nginx的配置檔案在/etc/nginx目錄下。使用以下命令啟動Nginx:

sudo systemctl start nginx

檢查系統中firewalld防火牆服務是否開啟,如果已開啟,我們需要修改防火牆配置,開啟Nginx外網埠訪問。

sudo systemctl status firewalld

如果顯示active (running),則需要調整防火牆規則的配置。

修改/etc/firewalld/zones/public.xml檔案,在zone一節中增加:

<zone>
    ...
    <service name="nginx"/>
<zone>

儲存後重新載入firewalld服務:

sudo systemctl reload firewalld

您可以通過瀏覽器訪問 http://外網IP地址 來確定Nginx是否已經啟動。

注意:很重要!!!

部分童鞋發現按照教程操作最後無法訪問,這是雲伺服器預設關閉了80埠。這個請移步雲服務裡控制檯開啟80埠的外網訪問,如需步驟,請自行百度!

最後將Nginx設定為開機啟動:

sudo systemctl enable nginx.service

這麼Nginx就安裝成功了!

安裝MySQL(MariaDB)

MariaDB是MySQL的一個分支,主要由開源社群進行維護和升級,而MySQL被Oracle收購以後,發展較慢。在CentOS 7的軟體倉庫中,將MySQL更替為了MariaDB。

我們可以使用yum直接安裝MariaDB:

sudo yum install mariadb-server

安裝完成之後,執行以下命令重啟MariaDB服務:

sudo systemctl start mariadb

MariaDB預設root密碼為空,我們需要設定一下,執行指令碼:

sudo /usr/bin/mysql_secure_installation

這個指令碼會經過一些列的互動問答來進行MariaDB的安全設定。

首先提示輸入當前的root密碼:

  • Enter current password for root (enter for none):
    初始root密碼為空,我們直接敲回車進行下一步。
  • Set root password? [Y/n]
    設定root密碼,預設選項為Yes,我們直接回車,提示輸入密碼,在這裡設定您的MariaDB的root賬戶密碼。
  • Remove anonymous users? [Y/n] 是否移除匿名使用者,預設選項為Yes,建議按預設設定,回車繼續。
  • Disallow root login remotely? [Y/n]
    是否禁止root使用者遠端登入?如果您只在本機內訪問MariaDB,建議按預設設定,回車繼續。 如果您還有其他雲主機需要使用root賬號訪問該資料庫,則需要選擇n。
  • Remove test database and access to it? [Y/n] **是否刪除測試用的資料庫和許可權?
    建議按照預設設定,回車繼續。**
  • Reload privilege tables now? [Y/n]
    是否重新載入許可權表?因為我們上面更新了root的密碼,這裡需要重新載入,回車。

完成後你會看到Success!的提示,MariaDB的安全設定已經完成。我們可以使用以下命令登入MariaDB:

mysql -uroot -p

按提示輸入root密碼,就會進入MariaDB的互動介面,說明已經安裝成功。

最後我們將MariaDB設定為開機啟動。

sudo systemctl enable mariadb

安裝PHP

我們可以直接使用yum安裝PHP:

sudo yum install php56w-fpm php56w-mysql php56w-mysqli php56w php56w-opcache php56w-gd php56w-intl php56w-mbstring php56w-exif php56w-mcrypt php56w-openssl

//把該安裝的一次性裝到位

安裝完成後我們將php-fpm啟動:

sudo systemctl start php-fpm

將php-fpm設定為開機啟動:

sudo systemctl enable php-fpm

接下來需要注意了!配置Nginx–多個站點

我給大家提供一個範本作為參考:

nginx.conf
//裡面我會詳細的給予中文註釋
vi /etc/nginx/nginx.conf
//編輯nginx.conf的命令

以下為conf檔案內容:

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log; #錯誤日誌記錄的位置
pid /run/nginx.pid; #nginx.pid為記錄nginx主程序pid檔案;切勿修改、移動
# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
#引入/usr/share/nginx/modules/ 目錄下的所有以.conf結尾的檔案

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;
    #這句很重要,引入所有etc/nginx/conf.d/目錄下的.conf檔案

    #***etc/nginx/conf.d/目錄存放的就是分站點的檔案(下面會給出例項程式碼)***

    server {
        #由於我們的nginx需要配置多站點,所以在此就需要註釋一些東西

         listen       80 default_server;
         listen       [::]:80 default_server;
        #保留監聽的埠  
        # server_name  _;
        # root         /usr/share/nginx/php;

        # Load configuration files for the default server block.
        # include /etc/nginx/default.d/*.conf;

        # location / {
        # }

        # error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
        # location ~ \.php$ {
        # root           /usr/share/php;
        # fastcgi_pass   127.0.0.1:9000;
        # fastcgi_index  index.php;
        # fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        # include        fastcgi_params;
        # }
    }

# Settings for a TLS enabled server.
#
#    server {
#        listen       443 ssl http2 default_server;
#        listen       [::]:443 ssl http2 default_server;
#        server_name  _;
#        root         /usr/share/nginx/html;
#
#        ssl_certificate "/etc/pki/nginx/server.crt";
#        ssl_certificate_key "/etc/pki/nginx/private/server.key";
#        ssl_session_cache shared:SSL:1m;
#        ssl_session_timeout  10m;
#        ssl_ciphers HIGH:!aNULL:!MD5;
#        ssl_prefer_server_ciphers on;
#
#        # Load configuration files for the default server block.
#        include /etc/nginx/default.d/*.conf;
#
#        location / {
#        }
#
#        error_page 404 /404.html;
#            location = /40x.html {
#        }
#
#        error_page 500 502 503 504 /50x.html;
#            location = /50x.html {
#        }
#    }

}

#注意:此份nginx.conf可以直接複製了去使用!~好用了就給博主打個賞錢!謝謝!

配置完nginx之後我們該幹啥、?當然是重啟nginx唄

service nginx start      #啟動nginx

service nginx stop       #停止nginx

service nginx restart    #重啟nginx

sudo systemctl reload nginx     #或者執行這條

重啟完畢,繼續開啟 http://外網IP地址 來確定Nginx是否已經啟動。

此時,伺服器啟動的是nginx和apache。

而且php-fpm預設傳送到apache。

所以咱們還得繼續修改一下php-fpm。

配置 php-fpm

vi /etc/php-fpm.d/www.conf    

#編輯php-fpm配置檔案

修改user和group (原始碼為:user = apache group = apache)

user = nginx    
group = nginx

修改完了之後,還是老樣子,重啟php-fpm服務

service php-fpm start      #啟動php-fpm

service php-fpm stop       #停止php-fpm

service php-fpm restart    #重啟php-fpm

最後,咱們需要為nginx新增站點了

新增站點這我先給大家一個截圖,以幫助大家迅速的瞭解是怎麼回事

image1.png

大家應該看的很清楚了,猜都可以猜到,博主這一共配置了三個站點,這三個站點是怎麼被nginx引入的呢?

我給大家貼出nginx的配置檔案的裡面應該有這麼一句(注意圖中的紅框,上面的是地址)
include /etc/nginx/conf.d/*.conf;
#這句很重要,引入所有etc/nginx/conf.d/目錄下的.conf檔案
#***etc/nginx/conf.d/目錄存放的就是分站點的檔案(下面會給出例項程式碼)***

好的,大家應該能準確理解了,如果還是理解不了的話只能缺你回去喝點三鹿了!

下面我給大家貼出nginx站點配置檔案的程式碼,修改修改就可以用

看程式碼的時候請注意看裡面的路徑,當然我也還是會給一定的中文註釋

#這個檔案是上面的qopmall.com.conf
server {

    server_name  qopmall.com www.qopmall.com;#這裡就是你要繫結的域名了,空格分開
    location / {
            root   /usr/share/php/weixin; #這裡是你站點存放的資料夾名稱(也就是說,你當前這個站點的檔案全部都丟在這個路徑的weixin資料夾裡面)
            index  index.php index.html index.htm; #這裡照抄即可
        }
    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        root           /usr/share/php/weixin; #這裡的配置等同於上面的那個root配置
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /usr/share/php/weixin/$fastcgi_script_name; #這裡的配置也是和上面的root配置一樣
        include        fastcgi_params;
    }
}

程式碼非常簡單,我沒註釋到的不用修改就行。

上面的路徑,比如/usr/share/php/weixin 這就是你站點的根目錄,我給大家截圖參考:
image2.png

各位童鞋,建立好站點了,先寫個簡單的php程式測試一下是否正常,比如info。