前几天想学thinkphp,但每次访问的路径都要到thinkphp的public下,感觉路径很长,不是很方便,而且不同的项目都要配置不同的端口号,不想记那么多。于是,捣鼓起了给nginx配置虚拟机。
1.Homebrew简介
由于我的电脑是Mac ,所以安装环境服务用Homebrew 是最方便的,在这简单介绍一下,安装自行百度。
Homebrew是一款Mac OS平台下的软件包管理工具,拥有安装、卸载、更新、查看、搜索等很多实用的功能。简单的一条指令,就可以实现包管理,而不用你关心各种依赖和文件路径的情况,十分方便快捷。
- 安装包
brew install <packageName>
- 卸载包
brew uninstall <packageName>
- 查询包
brew search <packageName>
- 查看已安装包列表
brew list
- 查看包信息
brew info <packageName>
- 更新homebrew
brew update
- 查看版本
brew -v
- 查看帮助信息
brew -h
2.安装nginx
1.卸载nginx
因为我的电脑原本就安装好了,所以现在卸载一遍
。
打开终端,可以先关闭nginx
~ % nginx -s stop
然后执行卸载命令
~ % brew uninstall nginx
Uninstalling /usr/local/Cellar/nginx/1.19.0... (25 files, 2.1MB)
Warning: The following nginx configuration files have not been removed!
If desired, remove them manually with `rm -rf`:
/usr/local/etc/nginx
/usr/local/etc/nginx/fastcgi.conf
/usr/local/etc/nginx/fastcgi.conf.default
/usr/local/etc/nginx/fastcgi_params
/usr/local/etc/nginx/fastcgi_params.default
/usr/local/etc/nginx/koi-utf
/usr/local/etc/nginx/koi-win
/usr/local/etc/nginx/mime.types
/usr/local/etc/nginx/mime.types.default
/usr/local/etc/nginx/nginx.conf
/usr/local/etc/nginx/nginx.conf.default
/usr/local/etc/nginx/scgi_params
/usr/local/etc/nginx/scgi_params.default
/usr/local/etc/nginx/servers
/usr/local/etc/nginx/uwsgi_params
/usr/local/etc/nginx/uwsgi_params.default
/usr/local/etc/nginx/win-utf
发现它告诉我们 /usr/local/Cellar/nginx/1.19.0...下面的nginx 已经卸载好了,但是还有一个Warning 警告。安装nginx的时候它会在/usr/locl/etc目录下生成一个nginx 目录,里面有我们需要配置的nginx配置文件。还有在/usr/local/var下生成一个www目录,这就是nginx的根目录了,我们写的项目都是放在里面的。现在把这些按提示先删除。
~ % rm -rf /usr/local/etc/nginx
~ % rm -rf /usr/local/var/www
再看看/usr/locl/etc下是都含有nginx了
~ % cd /usr/local/etc
etc % ls
bash_completion.d locales.conf openssl@1.1
fonts odbc.ini php
freetds.conf odbcinst.ini pkcs11
gitconfig openldap pool.conf
httpd openssl unbound
发现没了,那么卸载就算成功了。
2.安装nginx
直接运行安装命令
~ % brew install nginx
==> Downloading https://mirrors.ustc.edu.cn/homebrew-bottles/bottles/nginx-1.19.
Already downloaded: /Users/lijiwei/Library/Caches/Homebrew/downloads/41a508b3d42ad55e04fcf83c39d56c6c80f6d21187de7599e769db50ba424bc0--nginx-1.19.0.catalina.bottle.tar.gz
==> Pouring nginx-1.19.0.catalina.bottle.tar.gz
==> Caveats
Docroot is: /usr/local/var/www
The default port has been set in /usr/local/etc/nginx/nginx.conf to 8080 so that
nginx can run without sudo.
nginx will load all files in /usr/local/etc/nginx/servers/.
To have launchd start nginx now and restart at login:
brew services start nginx
Or, if you don't want/need a background service you can just run:
nginx
==> Summary
🍺 /usr/local/Cellar/nginx/1.19.0: 25 files, 2.1MB
这就下载好了,告诉你根目录所在地/usr/local/var/www和配置文件/usr/local/etc/nginx/nginx.conf,并且端口号是8080 。若你现在就想访问那肯定还是不行的。先把nginx开启一下。
~ % sudo nginx
之后访问localhost:8080
看到这个界面就算安装好了。
3.配置PHP环境
首先看看 配置文件/usr/local/etc/nginx/nginx.conf长啥样子
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
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 8080;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#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;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$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 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
include servers/*;
}
文件太多,我们先把注释的都删除一些,重要的的还留着,你可以先拷贝一份。注意,还是得保留并打开这段代码
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi_params;
}
之后就剩这些了,仔细看一下这里的注释。
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
# 监听的端口,多个端口之间使用空格进行间隔
listen 8080;
# 用来定义访问的ip地址或者域名,多个域名之间使用空格分开
server_name localhost;
# 用于指定站点网页的默认编码格式
# charset koi8-r;
# 用来指定此站点的访问日志存放路径,后面的main用于设定日志的格式
# access_log logs/host.access.log main;
# 用于指定站点的网页根目录,可以是相对路径(相对于nginx安装目录),也可以是绝对路径
# root /www/examples.com
# 用于指定访问的默认首页地址
# index index.html index.php
# 用来指定站点默认访问的设置,里面的root和index用法和效果与上面是一样的
# 两种方式使用任何一种都是可以的,这里采用 location / {} 的方式
location / {
root html;
index index.html index.htm;
}
# 使用error_page指令设置各种错误信息的返回页面
# 错误信息的返回页面大小如果低于512k则会被ie浏览器替换为ie默认的错误页面
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# 下面是列出了两种解析php的方式:
# 第一种是简单的将所有以php结尾的请求都交给本机的127.0.0.1处理,你可以后面添加上8080
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# 第二种是将php的请求交给FastCGI进程监听的ip地址及端口,这里转发给PHP_FPM
location ~ \.php$ {
# 设定用于解析php的根目录,通常为网站根目录
root html;
# 地址和端口与php_fpm中设置的一致
fastcgi_pass 127.0.0.1:9000;
# 默认首页
fastcgi_index index.php;
# 指定防止php动态程序的主目录,也就是$fastcgi_script_name前面指定的路径,建议与网站根目录一致或直接使用$document_root
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi_params;
}
# 防止直接访问 .htaccess 文件,建议开启
#location ~ /\.ht {
# deny all;
#}
}
include servers/*;
}
开始修改文件,主要改两个地方。
- 指定访问的默认首页为index.php,修改location /{}下的index
location / {
root html;
index index.php index.html index.htm;
}
- 修改 location ~ .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;
}
然后在/usr/local/www 下建一个index.php,里面的内容为
<?php
phpinfo();
重启一下nginx(修改了配置文件都需要重启一下)
~ % sudo nginx -s reload
开启下php-fpm
~ % sudo php-fpm start
Usage: php [-n] [-e] [-h] [-i] [-m] [-v] [-t] [-p <prefix>] [-g <pid>] [-c <file>] [-d foo[=bar]] [-y <file>] [-D] [-F [-O]]
-c <path>|<file> Look for php.ini file in this directory
-n No php.ini file will be used
-d foo[=bar] Define INI entry foo with value 'bar'
-e Generate extended information for debugger/profiler
-h This help
-i PHP information
-m Show compiled in modules
-v Version number
-p, --prefix <dir>
Specify alternative prefix path to FastCGI process manager (default: /usr/local/Cellar/php/7.4.6_1).
-g, --pid <file>
Specify the PID file location.
-y, --fpm-config <file>
Specify alternative path to FastCGI process manager config file.
-t, --test Test FPM configuration and exit
-D, --daemonize force to run in background, and ignore daemonize option from config file
-F, --nodaemonize
force to stay in foreground, and ignore daemonize option from config file
-O, --force-stderr
force output to stderr in nodaemonize even if stderr is not a TTY
-R, --allow-to-run-as-root
Allow pool to run as root (disabled by default)
在浏览器上再访问一下 localhost:8080,如果结果为下图所示,就完美了。
4.配置虚拟主机
首先我在/usr/local/var/www下下载了三个版本的thinkphp
然后在/etc/hosts文件里配置好所需的三个域名,填好后可在终端ping 一下看是否有效。
127.0.0.1 www.ljwtp.cn //v5.0.0
127.0.0.1 www.ljwtp51.com //v5.1.39
127.0.0.1 www.ljwtp6.com //v6.0.3
在配置文件中加3个server,并填写好参数
server {
listen 80;
server_name www.ljwtp.cn;
#server_name somename alias another.alias;
root /usr/local/var/www/thinkPHP/public;
location / {
index index.php index.html index.htm;
}
location ~ \.php$ {
root /usr/local/var/www/thinkPHP/public;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
server {
listen 80;
server_name www.ljwtp51.com;
#server_name somename alias another.alias;
root /usr/local/var/www/tp5/public;
location / {
index index.php index.html index.htm;
}
location ~ \.php$ {
root /usr/local/var/www/tp5/public;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
server {
listen 80;
server_name www.ljwtp6.com;
#server_name somename alias another.alias;
location / {
root /usr/local/var/www/tp/public;
index index.php index.html index.htm;
}
location ~ \.php$ {
root /usr/local/var/www/tp/public;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
然后重启nginx
~ % sudo nginx -s reload
最后,用配置好的域名在浏览器访问
整个环节就到此结束了,不懂的请留言,若有大佬指教也虚心接受。