搭建LNMP+CI環境

 

首先搭建 LNMP 的服務器環境

 

  • 安裝 Nginx, MySQL 和 PHP 軟件包,執行如下命令

         yum install -y nginx mariadb-server mariadb php php-fpm php-mysqlphp

 

啓動並檢查 Nginx 和 PHP 的安裝狀況html

修改  /etc/nginx/nginx.conf,可參考下面的配置示例:
nginx.conf 
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

include /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;

    server {
        listen       80 default_server;
        #listen       [::]:80 default_server;
        server_name  _;
        root /var/www/html;

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

        location / {

        }

        location ~ .php$ {
            fastcgi_pass 127.0.0.1:9000;  
            fastcgi_index index.php;  
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;  
            include fastcgi_params;  
        }


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

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }
}

啓動nginx服務器node

 

檢查php環境搭建,phpinfo();mysql

 

啓動 PHP-FPM 進程:nginx

service php-fpm start


啓動以後,能夠使用下面的命令查看 PHP-FPM 進程監聽哪一個端口
netstat -nlpt | grep php-fpm
chkconfig php-fpm on

啓動並配置 MySQL
systemctl start mariadb
配置密碼, 這裏默認使用密碼 QcloudLabPASSWORD
mysqladmin -u root password 'QcloudLabPASSWORD'
登陸 MySQL
mysql -u root -pQcloudLabPASSWORD
建立數據庫 CI
create database CI;
退出 MySQL, 回到 Bash shell

下載 CI 框架
     執行如下命令, 將 CI 框架下載到 家目錄 下
      wget https://mc.qcloudimg.com/static/archive/282f387cae30259401a8800e8d17e60b/CodeIgniter-3.1.4.zip -O ~/CodeIgniter.zip
 
安裝 CI 框架
將CodeIgniter.zip 解壓到 /var/www/html 目錄下
unzip ~/CodeIgniter.zip && mv ~/CodeIgniter-3.1.4/* /var/www/html
 

實踐 CI 框架

  • 知識準備
    這裏將會演示如何經過 CI 框架, 使得訪問  http://123.207.8.59/index.php/firstrun/hello 返回 "Hello, World"
    在 CI 的路由規則中, 路由的匹配規則:
    1. 用戶訪問的 URL 爲 http://123.207.8.59/index.php/firstrun/hello
    2. 此時 CI 會查找 application/controller 目錄下名爲 Firstrun.php 的 PHP 文件 
       
    3. 該 PHP 文件有個叫 Firstrun 的 class
    4. 該 class 有一個叫 hello 的方法, 該方法處理對此 URL 地址的請求並做出響應

編寫調用代碼sql

在  /var/www/html/application/controllers 目錄下新建一個叫 Firstrun.php 的文件, 代碼以下:
Firstrun.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Firstrun extends CI_Controller {

    public function hello() {
        echo 'Hello World';
    }
}

  修改nginx配置並重啓shell

修改  /etc/nginx/nginx.conf,可參考下面的配置示例:
nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

include /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;

    server {
        listen       80 default_server;
        #listen       [::]:80 default_server;
        server_name  _;
        root /var/www/html;

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

        location / {
                # 這裏使用try_files進行url重寫,不用rewrite了。
                try_files $uri $uri/ /index.php?$query_string;
        }

        location ~ .php($|/) {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_split_path_info ^(.+.php)(.*)$;
            fastcgi_param   PATH_INFO $fastcgi_path_info;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }

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

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }
}

  重啓 Nginx數據庫

 

 

 

大功告成bash

相關文章
相關標籤/搜索