搭建 LNMP + CodeIgniter 开发环境 搭建 LNMP 环境 首先搭建 LNMP 的服务器环境 安装 Nginx, MySQL 和 PHP 软件包 执行如下命令: yum install -y nginx mariadb-server mariadb php php-fpm php-mysql
php
启动并检查 Nginx 和 PHP 的安装状况 修改 /etc/nginx/nginx.conf,可参考下面的配置示例:html
示例代码:
/etc/nginx/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 nginxnode
在*/var/www/html* 目录下新建一个 info.php 文件来检查 php 是否安装成功了,文件内容参考以下:mysql
示例代码:/var/www/html/info.php
<?php phpinfo(); ?>
复制代码
启动 PHP-FPM 进程: service php-fpm start
nginx
启动以后,能够使用下面的命令查看 PHP-FPM 进程监听哪一个端口 netstat -nlpt | grep php-fpm
sql
把 PHP-FPM 也设置成开机自动启动: chkconfig php-fpm on
shell
此时,访问 http://<您的 CVM IP 地址>/info.php 可浏览到咱们刚刚建立的 info.php 页面了, 该页面展现了 PHP 的配置状况 启动并配置 MySQL 启动 MySQL systemctl start mariadb
数据库
配置密码, 这里默认使用密码 QcloudLabPASSWORDbash
mysqladmin -u root password 'QcloudLabPASSWORD'
服务器
登陆 MySQL mysql -u root -pQcloudLabPASSWORD
建立数据库 CI create database CI;
退出 MySQL, 回到 Bash shell exit
至此, LAMP 环境已经搭建好了 下载安装 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
此时访问 http://<您的 CVM IP 地址>/index.php , 便可看到返回了CI的欢迎页面 实践 CI 框架
知识准备 这里将会演示如何经过 CI 框架, 使得访问 http://<您的 CVM IP 地址>/index.php/firstrun/hello 返回 "Hello, World" 在 CI 的路由规则中, 路由的匹配规则: 用户访问的 URL 为 http://<您的 CVM IP 地址>/index.php/firstrun/hello 此时 CI 会查找 application/controller 目录下名为 Firstrun.php 的 PHP 文件 [?]
该 PHP 文件有个叫 Firstrun 的 class 该 class 有一个叫 hello 的方法, 该方法处理对此 URL 地址的请求并做出响应
CI 会自动将此处作大小写的转换
编写调用代码 在 /var/www/html/application/controllers 目录下新建一个叫 Firstrun.php 的文件, 代码以下: 示例代码:/var/www/html/application/controllers/Firstrun.php <?phpdefined('BASEPATH') OR exit('No direct script access allowed'); class Firstrun extends CI_Controller { public function hello() { echo 'Hello World'; }}
修改nginx配置并重启 修改 /etc/nginx/nginx.conf,可参考下面的配置示例:
示例代码:/etc/nginx/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($|/) { f
astcgi_pass 127.0.0.1:9000; f
astcgi_index index.php;
fastcgi_split_path_info ^(.+.php)(.*)$; f
astcgi_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 nginx -s reload
访问不带 index.php 的 URL 地址 http://<您的 CVM IP 地址>/firstrun/hello , 看到返回了 Hello, World , 说明配置成功了