美文网首页我爱编程
Ubuntu安装配置Nginx、PHP、MySQL

Ubuntu安装配置Nginx、PHP、MySQL

作者: VitaAin | 来源:发表于2018-02-11 15:07 被阅读189次

    参考:一步一步教你部署自己的 Laravel 应用&程序到服务器

    1. 安装PHP 7.1

      • apt-get update

      • apt-get install -y language-pack-en-base,安装 Php7.1 之前,要先安装language-pack-en-base这个包

        这个包是为了解决系统不同语言之间可能发生的冲突,安装之后可以减少许多因语言编码带来的问题。其中-y参数表明直接安装,无需确认。

      • locale-gen en_US.UTF-8,设定语言编码为UTF-8

      • apt-get install git vim,安装Git、Vim

      • 下面采用ppa方式安装php7.1:

      • apt-get install software-properties-common

        software-properties-commonadd-apt-repository所依赖的包

      • LC_ALL=en_US.UTF-8 add-apt-repository ppa:ondrej/php,添加php7的ppa,注意LC_ALL=en_US.UTF-8参数告诉我们系统语言为UTF-8,如果没有,可能会出现错误,如阿里云的服务器。

      • apt-get update,更新安装包,把刚才添加的包拉取下来

      • apt-cache search php7.1,搜索php7.1开头的包检验是否安装成功,输出如下:

        root@demo:~# apt-cache search php7.1
        php-yaml - YAML-1.1 parser and emitter for PHP
        php-apcu - APC User Cache for PHP
        php-ssh2 - Bindings for the libssh2 library
        php-igbinary - igbinary PHP serializer
        php-mailparse - Email message manipulation for PHP
        php-libsodium - PHP wrapper for the Sodium cryptographic library
        php-propro - propro module for PHP
        php-raphf - raphf module for PHP
        
        ....................省略...........................
        
      • apt-get -y install php7.1 ,安装php

      • php -v,查看是否安装成功,成功则输出如下:

        PHP 7.1.0beta2 (cli) ( NTS )
        Copyright (c) 1997-2016 The PHP Group
        Zend Engine v3.1.0-dev, Copyright (c) 1998-2016 Zend Technologies
            with Zend OPcache v7.1.0beta2, Copyright (c) 1999-2016, by Zend Technologies
        
      • apt-get -y install php7.1-mysql,安装php7.1-mysql,Php7.1 与 mysql 的通信模块

      • apt-get install php7.1-fpm,安装 fpm,这是Nginx 用来解析php文件的

      • apt-get install php7.1-curl php7.1-xml php7.1-mcrypt php7.1-json php7.1-gd php7.1-mbstring php7.1-zip,安装其他必备模块

        // 查看已安装模块
        php -m
        
    2. 安装MySQL

      • apt-get update

      • apt-get install mysql-server mysql-client,安装MySQL

      • mysql -V,查看版本,输出如下:

        root@demo:~# mysql -V
        mysql  Ver 14.14 Distrib 5.7.15, for Linux (x86_64) using  EditLine wrapper
        
      • 启动与停止

        service mysql start
        service mysql stop 
        
      • 是否在运行

        netstat -tap | grep mysql
        
    3. 安装Nginx

      • apt update
      • apt-get -y install nginx,安装Nginx
      • 访问IP可查看到Nginx引导页
    4. 配置PHP

      • vim /etc/php/7.1/fpm/php.ini,输入/fix_pathinfo搜索,将cgi.fix_pathinfo=1改为`cgi.fix_pathinfo=0``
    5. 编辑fpm的配置文件

      • vim /etc/php/7.1/fpm/pool.d/www.conf,找到listen = /run/php/php7.1-fpm.sock修改为listen = /var/run/php/php7.1-fpm.sock。当然,你也可以不修改,但必须前后一致,后面会用到这个配置
    6. 配置Nginx

      • vim /etc/nginx/sites-available/default,主要是配置server这一部分,最终配置如下:

        server {
                listen 80 default_server;
                listen [::]:80 default_server ipv6only=on;
        
                root /var/www/[project-name]/public;
                index index.php index.html index.htm;
        
                # Make site accessible from http://localhost/
                server_name xxx.xx;
        
                location / {
                        # First attempt to serve request as file, then
                        # as directory, then fall back to displaying a 404.
                        try_files $uri $uri/ /index.php?$query_string;
                        # Uncomment to enable naxsi on this location
                        # include /etc/nginx/naxsi.rules
                }
        
                location ~ \.php$ {
                        try_files $uri /index.php =404;
                        fastcgi_split_path_info ^(.+\.php)(/.+)$;
                        fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
                        fastcgi_index index.php;
                        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                        include fastcgi_params;
                }
        }
        

        解释:

        • root:是你的项目的public目录,也就是网站的入口
        • index:添加了,index.php,告诉Nginx先解析index.php文件
        • server_name:你的域名,没有的话填写localhost
        • location / try_files修改为了try_files $uri $uri/ /index.php?$query_string;
        • location ~ \.php$部分告诉Nginx怎么解析Php,原封不动复制即可,但注意:fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;的目录要和fpm的配置文件中的listen一致

    Vue工程部署到Ngnix

    • npm run build,编译Vue工程,工程根目录下生成dist目录
    • 查看nginx配置文件(/etc/nginx/sites-available/default),查看root路径(如:root /var/www/vitahome/public;),将dist目录下内容复制到该路径下
    • 浏览器中访问IP或域名即可

    Laravel工程部署到Nginx

    参考:搭建nginx服务器 配置laravel

    如何把laravel项目部署到服务器上

    在阿里云的ECS上部署Laravel项目

    • 备注:当前服务器Nginx上已部署了一个Vue工程

    • 创建Laravel项目

      • composer安装

        cd ~
        curl -sS https://getcomposer.org/installer | php
        
      • 设置composer全局使用

        mv composer.phar /usr/local/bin/composer
        
      • 创建Laravel工程:vitahomebackend,在/var/www目录下直接执行:

        composer create-project laravel/laravel vitahomebackend
        
      • /var/www/是属于root用户的,而访问网站的用户则需要正确的权限和访问限制:

        chmod -R :www-data /var/www/vitahomebackend
        
      • 根据Laravel的官方文档,/var/www/vitahomebackend/storage目录需要给网站的用户权限

        chmod -R 775 /var/www/vitahomebackend/storage
        
    • cp /etc/nginx/sites-available/default /etc/nginx/sites-available/vitahomebackend,复制default配置文件到其目录下,换个名字,用于要部署的Lavavel工程

    • vim /etc/nginx/sites-available/vitahomebackend,修改Lavarel工程配置文件(:set nu打开行号),修改后如下:

      ##
      # You should look at the following URL's in order to grasp a solid understanding
      # of Nginx configuration files in order to fully unleash the power of Nginx.
      # http://wiki.nginx.org/Pitfalls
      # http://wiki.nginx.org/QuickStart
      # http://wiki.nginx.org/Configuration
      #
      # Generally, you will want to move this file somewhere, and start with a clean
      # file but keep this around for reference. Or just disable in sites-enabled.
      #
      # Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
      ##
      
      # Default server configuration
      #
      server {
        // 注释掉了原来的配置,下面那条是自己手写的80端口
        # listen 80 default_server;
        # listen [::]:80 default_server;
        listen 80;
      
        # SSL configuration
        #
        # listen 443 ssl default_server;
        # listen [::]:443 ssl default_server;
        #
        # Note: You should disable gzip for SSL traffic.
        # See: https://bugs.debian.org/773332
        #
        # Read up on ssl_ciphers to ensure a secure configuration.
        # See: https://bugs.debian.org/765782
        #
        # Self signed certs generated by the ssl-cert package
        # Don't use them in a production server!
        #
        # include snippets/snakeoil.conf;
      
        // 配置本地的laravel工程目录
        root /var/www/vitahomebackend/public;
      
        # Add index.php to the list if you are using PHP
        // 添加了一个 index.php ,原来是没有的
        index index.php index.html index.htm index.nginx-debian.html;
      
        // 域名或IP,注意:域名需要在阿里云后台增加域名解析
        server_name xxx.xxxx.xxx;
      
        location / {
            # First attempt to serve request as file, then
            # as directory, then fall back to displaying a 404.
            # try_files $uri $uri/ =404;
            // 将下面的指令放到站点配置文件中就可以实现美化链接的功能
            try_files $uri $uri/ /index.php?$query_string;
        }
      
        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        
        location ~ \.php$ {
            fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
            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;
        #}
      }
      # Virtual Host configuration for example.com  
      # You can move that to a different file under sites-available/ and symlink that
      # to sites-enabled/ to enable it.  
      # server {
      # listen 80;
      # listen [::]:80; 
      #
      # server_name example.com; 
      #
      # root /var/www/example.com;
      # index index.html; 
      #
      # location / {
      #     try_files $uri $uri/ =404;
      # }
      # }
      

      注意:Lavarel工程的配置文件内容与Vue工程的不一样,必须要修改,否则在添加软连接后启动Nginx会报错:

       Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details.
      
    • Nginx启动与停止

      service nginx stop
      service nginx start
      
    • 配置站点,添加软连接

      cd /etc/nginx/sites-enabled
      ln -s /etc/nginx/sites-available/vitahomebackend ./
      

      执行后,/etc/nginx/sites-enabled目录下会生成一个文件vitahomebackend

    • 重启Nginx,成功后即可用域名访问对应的工程页面


    php artisan serve运行报错
    • 错误信息:

      PHP Warning:  require(/var/www/vitahomebackend/vendor/autoload.php): failed to open stream: No such file or directory in /var/www/vitahomebackend/artisan on line 18
      PHP Fatal error:  require(): Failed opening required '/var/www/vitahomebackend/vendor/autoload.php' (include_path='.:/usr/share/php') in /var/www/vitahomebackend/artisan on line 18
      
    • 错误原因:

      网站根目录下缺少vendor这个存放laraver依赖包的目录

    • 解决方法:

      • 没有安装过的情况下,使用composer install进行安装
      • 以前安装过的话使用:composer update
    • 参考:Laravel访问出错错误信息:`Warning: require(/vendor/autoload.php): failed to open stream: No such file or dire 解决方法

    相关文章

      网友评论

        本文标题:Ubuntu安装配置Nginx、PHP、MySQL

        本文链接:https://www.haomeiwen.com/subject/oynktftx.html