1.下载安装yii2
composer create-project yiisoft/yii2-app-advanced my-blog
2.配置数据库信息
新版yii2 增加了environments目录生产环境为prod目录 开发测试环境为dev目录
本地开发所以在
environments/dev/common/config/main-local.php
线上则
environments/prod/common/config/main-local.php
配置数据库信息
<?php
return [
'components' => [
'db' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=my-blog',
'username' => 'root',
'password' => '12345',
'charset' => 'utf8',
],
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'viewPath' => '@common/mail',
// send all mails to a file by default. You have to set
// 'useFileTransport' to false and configure a transport
// for the mailer to send real emails.
'useFileTransport' => true,
],
],
];
3.初始化yii2
php init
Which environment do you want the application to be initialized in?
[0] Development
[1] Production
//本地选择 0 然后一直yes
4.配置nginx
新建nginx配置文件
touch www.my-blog.com.conf
编辑配置内容
server {
listen 80;
server_name www.my-blog.com;
access_log /usr/local/log/nginx/access_nginx.log combined;
root /data/www/my-blog/backend/web;
index index.html index.htm index.php;
location /nginx_status {
stub_status on;
access_log off;
allow 127.0.0.1;
deny all;
}
location / {
# Redirect everything that isn't a real file to index.php
try_files $uri $uri/ /index.php?$args;
}
location ~ [^/]\.php(/|$) {
fastcgi_pass 127.0.0.1:9000;
#fastcgi_pass unix:/dev/shm/php-cgi.sock;
fastcgi_index index.php;
include fastcgi.conf;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico)$ {
expires 30d;
access_log off;
}
}
重启nginx
nginx -s reload
5.配置 /etc/hosts
vim /etc/hosts
添加 127.0.0.1 www.my-blog.com
backend/config/main.php 修改一下路由
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
],
],
后台环境配置完成
浏览器访问结果
网友评论