首先搭建 LNMP 的服务器环境

 

  • 安装 Nginx, MySQL 和 PHP 软件包,执行以下命令

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

 

启动并检查 Nginx 和 PHP 的安装情况

修改 /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服务器

 

检查php环境搭建,phpinfo();

 

启动 PHP-FPM 进程:

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 地址的请求并作出响应
     

编写调用代码

在 /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配置并重启

修改 /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

 

 

 

大功告成