您现在的位置是:首页 > 技术学习 > PHP 网站首页 技术学习 PHP

centos7.4安装laravel开发环境

PHP   Laravel   服务器  
简介 当我们在本地开发laravel好项目后,需要部署到服务器。那远端服务器的laravel环境要如何搭建呢。我们需要安装git、nginx、mysql、php、composer等。

当我们在本地开发laravel好项目后,需要部署到服务器。那远端服务器的laravel环境要如何搭建呢。我们需要安装git、nginx、mysql、php、composer等。

参考文档:

https://www.aliyun.com/jiaocheng/128853.html

https://jingyan.baidu.com/article/215817f7a10bfb1eda14238b.html

个人csdn博客
 

1. 安装前准备

  1. 安装screen

    yum install screen
    
  2. 安装wget

    yum install wget
    
  3. 更新yum

    yum update
    
  4. 安装额外资源库

    yum install epel-release
    
  5. 下载最新ius

    wget https://dl.iuscommunity.org/pub/ius/stable/CentOS/7/x86_64/ius-release-1.0-15.ius.centos7.noarch.rpm
    
  6. 安装ius

    rpm -ivh ius-release-1.0-15.ius.centos7.noarch.rpm
    
  7. 新建www用户

    adduser www
    
  8. 设置密码(可不设置)

    passwd www
    
  9. 添加到组

    usermod -aG wheel www
    

2. 安装nginx

  1. 安装

    sudo yum install nginx
    
  2. 启动nginx

    sudo systemctl start nginx
    
  3. 设置为开机启动

    sudo systemctl enable nginx.service
    
  4. 检查开机自动是否设置成功

    systemctl list-dependencies | grep nginx
    

注:
项目目录默认在 /var/www/ 下
配置文件在 /etc/nginx/ 下

3. 安装mysql

由于laravel5.4以后,数据库默认使用utf8mb64,mysql版本太低时,在运行php artisan migrate 会报错,原因就是utf8和utf8mb4造成的,mysql5.7以后的版本没有 utf8 和 utf8mb64 的问题,

  1. 安装5.7.x的mysql源

    yum -y localinstall  http://dev.mysql.com/get/mysql57-community-release-el7-7.noarch.rpm
    
  2. 安装mysql

    yum -y install mysql-community-server install mysql-community-devel
    
  3. 启动mysql

    service mysqld start
    
  4. 设置mysqld服务开机自启动

    systemctl enable mysqld.service
    
  5. 检查mysqld开机自启动是否设置成功

    systemctl list-dependencies | grep mysqld
    

注:使用yum安装,启动会系统会自动生成一个随机的密码.
查看mysql的随机密码
grep 'temporary password' /var/log/mysqld.log

使用查询得到的随机密码在终端登录
mysql -u root -p 更改密码(mysql文档规定,密码必须包括大小写字母数字加特殊符号>8位)
ALTER USER 'root'@'localhost' IDENTIFIED BY '你要设置的密码';
如果报错提示不符合策略,那么可以先修改设定:
set global validate_password_policy=0;
这样就不再对密码大小写之类的有要求,这样只要长度符合就行了。
再次修改密码即可:ALTER USER 'root'@'localhost' IDENTIFIED BY '你要设置的密码';
退出mysql客户端,用刚才修改的密码登录确保密码修改成功
mysql -uroot -pxxxxx

4. 安装PHP7(7.1)

  1. 安装php71的源

    rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
    rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
    
  2. 安装php71

    yum -y install php71w php71w-fpm
    
  3. 安装常用拓展

    yum -y install php71w-mbstring php71w-common php71w-gd php71w-mcrypt
    yum -y install php71w-mysql php71w-xml php71w-cli php71w-devel
    yum -y install php71w-pecl-memcached php71w-pecl-redis php71w-opcache
    
  4. 重新加载php

    systemctl reload php-fpm
    
  5. 验证php是否安装成功

    php -v
    
  6. 验证对应的扩展是否安装成功

    php -m
    
  7. 启动php-fpm

    service php-fpm start
    
  8. 设置开机自启动

    systemctl enable php-fpm.service
    
  9. 检查开机自启动是否设置成功

    systemctl list-dependencies | grep php-fpm
    ps -ef | grep php-fpm
    

5. 安装Composer

# 依次执行
    php -r "copy('https://install.phpcomposer.com/installer', 'composer-setup.php');"

    php composer-setup.php

    php -r "unlink('composer-setup.php');"

# 全局安装
    sudo mv composer.phar /usr/local/bin/composer

6. 安装iptables防火墙,开放3306端口

  1. 关闭默认的firewall

    systemctl stop firewalld.service
    systemctl disable firewalld.service
    systemctl mask firewalld.service
    
  2. 安装iptables防火墙

    yum install iptables-services -y
    
  3. 启动设置防火墙

    systemctl enable iptables
    systemctl start iptables
    
  4. 查看防火墙状态

    systemctl status iptables
    
  5. 编辑防火墙,增加端口

    vi /etc/sysconfig/iptables #编辑防火墙配置文件(配置要开放的端口)
    
    # 端口开放的配置代码如下:
    -A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
    
    -A INPUT -m state --state NEW -m tcp -p tcp --dport 21 -j ACCEPT
    -A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
    -A INPUT -m state --state NEW -m tcp -p tcp --dport 443 -j ACCEPT
    
    -A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
    
    :wq! #保存退出
    

    注:
    iptables默认配置文件里有两行设置:
    -A INPUT -j REJECT --reject-with icmp-host-prohibited
    -A FORWARD -j REJECT --reject-with icmp-host-prohibited
    这两行设置一定要写在其他 端口开放 配置代码的 下面,不然会导致端口开放失败

  6. 重启配置,重启系统

    systemctl restart iptables.service   #重启防火墙使配置生效
    systemctl enable iptables.service   #设置防火墙开机启动
    
  7. 进入阿里云,添加安全组规则,把3306端口开放

    由于阿里云有双层保障,安全组的功能就是在请求到达服务器前的一个保障,因此,阿里云安全组需要先开放3306端口的入方向

6. 安装git

  1. 下载安装git

    yum -y install git
    
  2. 检查是否安装成功

    git --version    
    

7. 下载或克隆laravel项目到 /var/www/ 目录下

  1. 进入 /var/www/ 下 git clone 项目地址
  2. 复制 .env.example 为 .env
  3. 修改 .env 相关配置
  4. 安装依赖
    composer install
  1. 生存appkey
    php artisan key:generate
  1. 给文件权限 以上操作针对的都是root用户,需要开放网站访问用户权限
    1. 给 /var/www/项目名 设置权限
      sudo chown -R :www /var/www/项目名
    2. 给项目下的storage目录写的权限
      sudo chmod -R 775 /var/www/项目名/storage
    3. 如果给完775权限扔不能访问,那么给整个项目777权限
      sudo chmod -R 777 /var/www/项目名/

8. 设置nginx配置文件

  1. 进入nginx目录下的conf.d文件夹

    cd /etc/nginx/conf.d/
    
  2. 新建一个自己网站的配置文件

    vim yourwebsite.com.conf
    // 文件以.conf结尾,名字一般为自己网站的名,这样方便辨识
    
  3. 在新建的配置文件中写入如下内容:

    server {
        listen  80;
        server_name yourwebsite.com;
        set $root_path 'your project path/public';
        root $root_path;
        index index.php index.html index.htm;
        try_files $uri $uri/ @rewrite;
        location @rewrite {
            rewrite ^/(.*)$ /index.php?_url=/$1;
        }
        location ~ \.php {
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index /index.php;
            fastcgi_split_path_info       ^(.+\.php)(/.+)$;
            fastcgi_param PATH_INFO       $fastcgi_path_info;
            fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include                       fastcgi_params;
        }
        location ~* ^/(css|img|js|flv|swf|download)/(.+)$ {
            root $root_path;
        }
        location ~ /\.ht {
            deny all;
        }
    }
    
  4. 配置完毕,重启nginx服务

    systemctl restart nginx
    

注:

  1. 常用命令:
    systemctl 命令 程序
    或:
    service 程序 命令

相关命令:

  1. nginx相关命令(sudo看情况加)

    # 启动nginx
    sudo systemctl start nginx
    # 或者:
    service nginx start
    
    # 重启ngnix
    sudo systemctl restart nginx
    # 或者:
    service nginx restart
    
    # 关闭nginx
    sudo systemctl stop nginx
    # 或者:
    service nginx stop
    
    # 查看nginx状态
    sudo sysemctl status nginx
    # 或者:
    service nginx status
    
  2. php服务相关命令

    # 启动php-fpm
    systemctl start php-fpm
    # 或者:
    service php-fpm start
    
    # 关闭php-fpm
    systemctl stop php-fpm
    # 或者:
    service php-fpm stop
    
    # 重启php-fpm
    systemctl restart php-fpm
    # 或者:
    service php-fpm restart
    
    # 查看php-fpm状态
    systemctl status php-fpm
    # 或者:
    service php-fpm status
    
  3. 使用yum安装的软件一般在 /etc/ 目录下

  4. nginx 项目目录默认在 /var/www/ 目录下

Top