搭建 LNMP + CodeIgniter 開發環境

仇諾伊發表於2017-12-13

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

相關文章