Laravel+Vue前后端分离项目(二)Nginx与Mysql配置

Nginx配置

我用的是阿里云Ubuntu16.04的轻量级服务器,安装了LNMP环境。

因为xshell要收费,我用的是免费的国产软件FinalShell,比xshell好用,推荐。

1、假设Laravel项目(api)和Vue项目(dist)放在/home/www文件夹下。

2、接着就要配置nginx。

vi /etc/nginx/sites-available/default.conf

我们希望

通过“http://ip”访问到Vue页面

通过“http://ip/api”访问到Laravel接口

于是我们需要两个端口号,vue是80端口,laravel是8080端口

因此,必须要用到反向代理

对于反向代理,我的理解就是:本来“http://ip”默认指到80端口的vue项目

现在要让它退出80端口,再进入8080端口的laravel项目

这就只需要在分别写好vue和laravel的两个server配置后,再在vue的serve里添加location /api{...}。具体如下:

server {
    listen 80;
    server_name _;
    root /home/www/dist;

    index index.html index.htm;

    location / {
        try_files $uri $uri/ /index.html;
    }

    location /api{
    	   rewrite ^/api/(.*) /$1 break;
    	   proxy_pass http://localhost:8080;
    }

}

server {
    listen 8080;
    server_name localhost;
    root /home/www/api/public;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff";

    index index.html index.htm index.php;

    charset utf-8;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    error_page 404 /index.php;

    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /\.(?!well-known).* {
        deny all;
    }

}

3、保存好配置后,重启nginx服务

sudo service nginx restart

这样就可以了。

前端vue:

后端laravel:

可能会遇到laravel框架的报错:The stream or file “/storage/logs/laravel.log” could not be opened: failed to open stream: Permission denied

用以下两行解决:

sudo chgrp -R www-data storage bootstrap/cache

sudo chmod -R ug+rwx storage bootstrap/cache

Mysql配置

我用的是Navicat Premium 12 for mac,破解方法参考下面链接:

https://www.cnblogs.com/lyfstorm/p/11123159.html

然后配置mysql,使之可以远程链接:

1、mysql -uroot -p你刚才设置好的密码              //登录mysql

2、CREATE USER 'username'@'host' IDENTIFIED BY 'password';                   

//添加用户,其中,username写你自己设置的名称,host写%,代表可从任意ip访问,password写你自己设置的密码

3、GRANT privileges ON databasename.tablename TO 'username'@'host'

//给用户授权,其中,privieges写ALL,代表该用户可对数据库操作所有命令(SELECT、CREATE等),databasename写该用户被允许操作的数据库,tablename写该用户被允许操作的表,写*,username和host就写上面写过的。

4、flush privileges;

5、exit;

6、vim /etc/mysql/mysql.conf.d/mysqld.cnf

注释掉这一行:bind-address        = 127.0.0.1

7、sudo service mysql restart

这样数据库也能在本地访问了 !

猜你喜欢

转载自blog.csdn.net/qq_33514421/article/details/103836297