新手 在linux 中安装 lnmp + yii2 趟河记录

我服务器用阿里云,这样的话,在公司在家、在任何地方、 只要一个电脑、就能不停的趟linux 这条河。虽说很多教程都在使用虚拟机做练习,但我个人不太推荐。俩者本来就有区别、作为新手的我们,还要分部分精力去考究出现的问题是不是虚拟机的问题、所以我直接阿里云买了个linux。一月费用一百来块钱,方便。

用xshell进入系统。


第一步,装php。因为yii2 要求php5.4以上,爬贴之后,发现php5.4对yii2支持有问题,果断选择php5.6。打算所有的软件都源包安装,所以第一步 更新yum

命令 yum update

600+个包更新,目测90%以上是没用的,但是不知道是干嘛的,先不管。

Complete!之后

安装php5.6

命令 yum install php56w

提示要确认的话 输入y 回车

Complete!之后

命令 php -v 查看安装结果

出现 PHP 5.6.25 (cli) (built: Aug 20 2016 10:06:25)表示安装成功 版本号为5.6.25

做一步打一个节点,使用 阿里云 自定义磁盘镜像、目测跟快照功能一样,要等处理结果为100%后继续操作,5-6分钟的样子。


做好备份,感觉心里踏实不少,接着 继续趟河吧


第二步 安装nginx 

参考Linux+nginx+搭配YII2环境


1、wget http://nginx.org/download/nginx-1.4.5.tar.gz  
2、tar zxvf nginx-1.4.5.tar.gz  
3、cd nginx-1.4.5  
4、./configure --with-http_ssl_module  
5、make && make install


进入第四步时候报错  解决方法 参考原文: 点击打开链接

ngingx安装错误 ./configure: error: the HTTP rewrite module requires the PCRE library.

有时候,我们需要单独安装nginx,来处理大量的下载请求。单独在Centos5安装nginx遇到的rewrite和HTTP  cache错误解决办法:

wget http://nginx.org/download/nginx-0.8.33.tar.gz
tar -zxvf nginx-0.8.33.tar.gz 
cd nginx-0.8.33
./configure --prefix=/usr/local/nginx

安装Nginx时报错

./configure:  error: the HTTP rewrite module requires the PCRE library.

安装pcre-devel解决问题
yum -y install pcre-devel

 

错误提示:./configure: error: the HTTP cache module requires md5 functions
from OpenSSL library.   You can either disable the module by using
--without-http-cache option, or install the OpenSSL library into the system,
or build the OpenSSL library statically from the source with nginx by using
--with-http_ssl_module --with-openssl=<path> options.

解决办法:

yum  -y install openssl openssl-devel

 

总结:

yum -y install pcre-devel openssl openssl-devel

./configure --prefix=/usr/local/nginx

make

make install

这些命令是编译 nginx配置文件用的 一定要全部执行

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

错误解决完后,再次执行第四步

解决完错误后,输出操作结果:nginx目录结构

Configuration summary
  + using system PCRE library
  + using system OpenSSL library
  + md5: using OpenSSL library
  + sha1: using OpenSSL library
  + using system zlib library

  nginx path prefix: "/usr/local/nginx"
  nginx binary file: "/usr/local/nginx/sbin/nginx"
  nginx configuration prefix: "/usr/local/nginx/conf"
  nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
  nginx pid file: "/usr/local/nginx/logs/nginx.pid"
  nginx error log file: "/usr/local/nginx/logs/error.log"
  nginx http access log file: "/usr/local/nginx/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"

至此 nginx 安装结束,在window上i浏览器中输入阿里云的ip 默认能访问到nginx 欢迎界面

走一步打一个节点,阿里云快照中、、、

查看nginx 运行状态、启动、停止、重启

命令 service nginx status

命令 service nginx start

命令 service nginx stop

命令 service nginx restart

至于nginx与yii2 配置相关,继续趟河中、、、


第三步、安装Composer

Composer 跟Compser.phar 作为新手的我理解的是这两文件 是一个文件,命名不同而已。

安装命令参照 Yii 2.0 权威指南

俩行命令分开执行

curl-sS https://getcomposer.org/installer | php

mv composer.phar /usr/local/bin/composer

注意,这里面下载到的文件名字为 composer.phar 通过命令改名为composer

所以我觉得这俩文件是一样的,网上的教程有的命令 直接是 composer.pharxxxxx 的问题是 博主没把命名做修改,也是困扰了我半天时间

下载composer文件,移动更名到 全局目录中

切换目录

命令 cd /usr/local/bin/

新建Composer.json文件并编辑

命令vim Composer.json

粘贴配置文件到Composer.json


{
    "require": {
        "monolog/monolog": "1.0.*"
    }
}

然后退出编辑

更新Composer 

命令composer update

至此Composer 安装结束,因为我们把Compser 文件放在 全局目录/usr/local/bin/ 中,所以在任何地方都可以使用Composer命令

Composer 安装后,切换到一个可通过 Web 访问的目录,执行如下命令即可安装 Yii :

我切换到nginx 根目录下 html(目前还不是清楚nginx根目录如何配置,所以之后的yii2项目文件我直接放在html中,学习阶段中嘛,无伤大雅)

(这一步的时候 我通过locate 查询 nginx 发现有俩个目录下面都有html 文件夹 而且里面都有 50x.html index.html 通过修改index.html内容 发现, ip地址访问的是 /usr/local/nginx/html/index.html,所以我认为 /root/nginx-1.4.5/html/index.html 这个文件所在的目录文件为安装包解压后的文件,这是安装文件,可以删除,事实可能也是如此的吧 哈哈)

命令 cd /usr/local/nginx/html/

命令 composer global require "fxp/composer-asset-plugin:~1.1.1"(第一条命令)

命令 composer create-project --prefer-dist yiisoft/yii2-app-basic basic(第二条命令)

第一条命令安装 Composer asset plugin, 它是通过 Composer 管理 bower 和 npm 包所必须的,此命令全局生效,一劳永逸。 第二条命令会将 Yii 安装在名为 basic 的目录中,你也可以随便选择其他名称。

第二条命令的时候会提示输入Token 

Token (hidden):  长这个样子 ,解决方案 度娘吧。 输入Token码后提示

Token stored successfully. 成功,然后就是等待。等一会会报错吧,如下


  Problem 1
    - yiisoft/yii2 2.0.9 requires ext-mbstring * -> the requested PHP extension mbstring is missing from your system.
    - yiisoft/yii2 2.0.8 requires ext-mbstring * -> the requested PHP extension mbstring is missing from your system.
    - yiisoft/yii2 2.0.7 requires ext-mbstring * -> the requested PHP extension mbstring is missing from your system.
    - yiisoft/yii2 2.0.6 requires ext-mbstring * -> the requested PHP extension mbstring is missing from your system.
    - yiisoft/yii2 2.0.5 requires ext-mbstring * -> the requested PHP extension mbstring is missing from your system.
    - Installation request for yiisoft/yii2 >=2.0.5 -> satisfiable by yiisoft/yii2[2.0.5, 2.0.6, 2.0.7, 2.0.8, 2.0.9].


  To enable extensions, verify that they are enabled in those .ini files:
    - /etc/php.ini
    - /etc/php.d/bz2.ini
    - /etc/php.d/calendar.ini
    - /etc/php.d/ctype.ini
    - /etc/php.d/curl.ini
    - /etc/php.d/exif.ini
    - /etc/php.d/fileinfo.ini
    - /etc/php.d/ftp.ini
    - /etc/php.d/gettext.ini
    - /etc/php.d/gmp.ini
    - /etc/php.d/iconv.ini
    - /etc/php.d/json.ini
    - /etc/php.d/phar.ini
    - /etc/php.d/shmop.ini
    - /etc/php.d/simplexml.ini
    - /etc/php.d/sockets.ini
    - /etc/php.d/tokenizer.ini
    - /etc/php.d/xml.ini
    - /etc/php.d/zip.ini
  You can also run `php --ini` inside terminal to see which files are used by PHP in CLI mode.

提示为缺少 mbstring 扩展,解决如下

命令 yum install php55w-soap(试了很多东西包括这个命令 但是仍然不行,最后试了下面的命令,安装通过)

命令yum install -y php56w php56w-opcache php56w-xml php56w-mcrypt php56w-gd php56w-devel php56w-mysql php56w-intl php56w-mbstring php56w-bcmath

(也许是php相关的模块没有安装齐全导致的,这行命令应该是吧php大部分的扩展都配置全了吧 ,总之通过了,脑子一轻,通过阿里云备份回滚,重新执行此命令,完美通过)

趟河不容易,且趟且珍惜,感谢 国外的friends 其博客为Steve Johnson有机会去膜拜大神

执行过程中 要求确认。

命令cd /usr/local/nginx/html/  (切换回html目录 重新安装yii2)

命令rm -rf basic(删除basic,不然会提示basic非空,执行不下去了)

命令composer create-project --prefer-dist yiisoft/yii2-app-basic basic

然后重复上述步骤 Token 什么的、、、

命令 cd /usr/local/nginx/html/basic/

命令 php requirements.php

至此,lnmp 已装完nginx +php 还有mysql 这个只有在yii2开发的时候再弄吧,简单的一逼。


第四步、nginx+yii2的关联配置 继续趟吧、、、

用外网访问,发现NGinx解析不了php文件,在error.log中查看报错信息如下


问度娘,原来是php没启动,php启动要 php-fpm,本地貌似都没装,那就装一下吧,

命令 yum install php56w-fpm

然后启动

命令 service php-fpm start

命令 service php-fpm status  查看启动状态

上面说了,我的yii 框架在/usr/local/nginx/html/下 所以配置nginx.conf 如下,红色为主要配置 其他为默认

http {
    include       mime.types;
    default_type  application/octet-stream;

    #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  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ .php$ {
            root           html/basic/web/;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param SCRIPT_FILENAME   $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }
    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
    # HTTPS server
    #
    #server {
    #    listen       443;
    #    server_name  localhost;

    #    ssl                  on;
    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_timeout  5m;

    #    ssl_protocols  SSLv2 SSLv3 TLSv1;
    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers   on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
}


在保证所有服务器启动的情况下,ip访问

命令 service php-fpm start

命令 service nginx start

这是我的目录路劲



这样配置

        location ~ .php$ {
            root           html/basic/web/;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param SCRIPT_FILENAME   $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }

可以访问yii2框架中的web

http://139.224.2.66/index.php


如果要访问info.php

修改配置为

        location ~ .php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param SCRIPT_FILENAME   $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }

http://139.224.2.66/info.php

至此,nginx + yii2 交互配置结束。

还要有配置,譬如 

给ip139.224.2.66绑定域名 www.xxx.com 

访问路径中不能出现index 字眼这些细碎配置,待续、、、

接下来就是吭yii2 这块大石头了。




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值