搭建 LNMP + CodeIgniter 开发环境

3 篇文章 0 订阅

搭建 LNMP + CodeIgniter 开发环境
搭建 LNMP 环境
首先搭建 LNMP 的服务器环境
安装 Nginx, MySQL 和 PHP 软件包
执行以下命令:
yum install -y nginx mariadb-server mariadb php php-fpm php-mysql

启动并检查 Nginx 和 PHP 的安装情况
修改 /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 / { } 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
nginx

/var/www/html 目录下新建一个 info.php 文件来检查 php 是否安装成功了,文件内容参考如下:

示例代码:/var/www/html/info.php
<?php phpinfo(); ?>

启动 PHP-FPM 进程:
service php-fpm start

启动之后,可以使用下面的命令查看 PHP-FPM 进程监听哪个端口
netstat -nlpt | grep php-fpm

把 PHP-FPM 也设置成开机自动启动:
chkconfig php-fpm on

此时,访问 http://<您的 CVM IP 地址>/info.php 可浏览到我们刚刚创建的 info.php 页面了, 该页面展示了 PHP 的配置情况
启动并配置 MySQL
启动 MySQL
systemctl start mariadb

配置密码, 这里默认使用密码 QcloudLabPASSWORD

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
, 说明配置成功了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值