搭建 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 , 說明配置成功了