homestead下的站点管理详解

/ 0评 / 1

我的本机环境是Mac,所以这里以Mac为例,Linux也是一样的。

新增站点一

官方给出了2种方式来新增站点,第一种是在 ~/.homestead/Homestead.yaml 配置文件中直接修改配置。

---
ip: "192.168.10.10"
memory: 2048
cpus: 1
provider: virtualbox

authorize: ~/.ssh/id_rsa.pub

keys:
    - ~/.ssh/id_rsa

# 这里是本机路径和虚拟机路径的映射
folders:
    - map: ~/phpstudy
      to: /home/vagrant/Code

# 这里是域名和虚拟机路径的映射,相当nginx配置站点里的域名和site path
sites:
    - map: www.study.com
      to: /home/vagrant/Code/Laravel/public
    - map: www.test.com
      to: /home/vagrant/Code/Test

databases:
    - homestead

# blackfire:
#     - id: foo
#       token: bar
#       client-id: foo
#       client-token: bar

# ports:
#     - send: 50000
#       to: 5000
#     - send: 7777
#       to: 777
#       protocol: udp

向上面的配置文件,我本机的家目录下的 phpstudy目录和虚拟机里的 /home/vagrant/Code 目录做一个映射,修改了本地的文件也相当于修改了虚拟机里对于路径的文件。www.test.com站点,映射到本机的~/phpstudy/Test目录,这个目录就是www.test.com的根目录了。

修改完成后,我们需要在homestead目录执行以下命令来让配置立即生效,或者你可以重启虚拟机。

vagrant provision

不过官方文档里面提到,这个操作是具有破坏性的,当执行 provision 命令,现有的数据库会被摧毁并重新创建。

新增站点二

直接进入homestead环境,并执行以下命令新增站点,domainpath请修改为自己的。

serve www.test.com /home/vagrant/Code/Test

然后重启 nginx ,即可生效。

sudo service nginx restart

上面2种方式新增的站点,都别忘记把domain添加到本机的/etc/hosts文件中。

修改/删除站点一

使用第一种方式新增的站点,如果需要修改,则直接修改Homestead.yaml配置文件,并在homestead目录下执行 vagrant provision 命令即可。

修改/删除站点二

如果是使用第二种方式新增的站点,我们在Homestead.yaml配置文件中是找不到新增的站点的。首先进入homestead环境,然后执行下面命令。

cd /etc/nginx/sites-available
ls

我们可以看到新增的站点都在这里了,相当于站点的nginx配置文件一样。

vagrant@homestead:~$ cd /etc/nginx/sites-available/
vagrant@homestead:/etc/nginx/sites-available$ ls
www.study.com  www.test.com
vagrant@homestead:/etc/nginx/sites-available$

我们可以删除和修改配置文件,从而做到修改站点和删除站点。

删除站点

rm www.test.com

修改站点

vi www.test.com

配置文件

server {
    listen 80;
    listen 443 ssl;
    server_name www.test.com;
    root "/home/vagrant/Code/Test";

    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; }

    access_log off;
    error_log  /var/log/nginx/www.test.com-error.log error;

    sendfile off;

    client_max_body_size 100m;

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

        fastcgi_intercept_errors off;
        fastcgi_buffer_size 16k;
        fastcgi_buffers 4 16k;
        fastcgi_connect_timeout 300;
        fastcgi_send_timeout 300;
        fastcgi_read_timeout 300;
    }

    location ~ /\.ht {
        deny all;
    }

    ssl_certificate     /etc/nginx/ssl/www.test.com.crt;
    ssl_certificate_key /etc/nginx/ssl/www.test.com.key;
}

修改了配置文件别忘了重启nginx哦,否则不重启虚拟机是不会生效的。

sudo service nginx restart

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注