WIN 下配置 PHP+Nginx+Mysql 开发环境
WIN 下配置 PHP+Nginx+Mysql 开发环境
由于新职位需要用到Nginx,也是第一次正式接触,仅作为笔记当做参考,如有不对之处,望指正。
准备工作(系统 win10 64位)
1、Nginx官网下载:http://nginx.org/en/download.html
此处可以根据自己需要选择版本
2、PHP下载地址:https://windows.php.net/download/
此处的要根据电脑系统的版本(64位)来选择PHP对应的版本,以及Nginx需要选择非线程安全,所以要选择 Non Thread Safe 这类的版本
3、Mysql下载地址:http://www.mysql.com/downloads/mysql/
此处选择的是最新版,可以根据自己需求选其他版本
把下载下来的文件(PHP、nginx、MySQL)都解压并放到你的C/D/E/F…盘里(随便一个都可以)建一个新文件夹命名为“wnmp”,来用做我们的环境部署
然后在wnmp文件夹下新建一个文件夹“www”作为站点目录,并在www 文件夹里面新建一个index.php的文件,内容如下:
<?php
phpinfo();
安装nginx
1.打开E:\wnmp\nginx目录,运行该文件夹下的nginx.exe
2.测试是否启动nginx。打开浏览器访问http://localhost 或 http://127.0.0.1,看看是否出现“Welcome to nginx!”,出现的证明已经启动成功了。没有启动的话,大部分都是端口占用的问题(打开E:\wnmp\nginx\conf\nginx.conf,把里面的 listen 后的端口改一下即可)。
注意:该网站的默认目录在“E:\wnmp\nginx\htm”下
安装PHP
1、E:\wnmp\php\ext下修改php.ini-development文件,将文件名修改为php.ini,打开php配置文件(php.ini);
2、搜索“extension_dir”,找到: e;xtension_dir = "ext" 先去前面的分号再改为 extension_dir = "E:\wnmp\php\ext";
3、搜索“date.timezone”,找到:;date.timezone = 先去前面的分号再改为 date.timezone = Asia/Shanghai 设置时区;
4、搜索“enable_dl”,找到:enable_dl = Off 改为 enable_dl = On;
5、搜索“cgi.force_redirect” ;cgi.force_redirect = 1 先去前面的分号再改为 cgi.force_redirect = 0;
6、搜索“fastcgi.impersonate”,找到: ;fastcgi.impersonate = 1 去掉前面的分号;
7、搜索“cgi.rfc2616_headers”,找到:;cgi.rfc2616_headers = 0 先去前面的分号再改为 cgi.rfc2616_headers = 1;
8、搜索“php_mysql”,找到:”extension=php_mysql.dll和extension=php_mysqli.dll 去掉前面的“;”extension=php_mysql.dll和extension=php_mysqli.dll (支持MYSQL数据库)
9、启动php服务:在E:\wnmp\php 文件下以管理员身份打开cmd命令行,运行:php-cgi.exe -b 127.0.0.1:9000 -c E:/wnmp/php/php.ini(根据自己配置的端口和路径更改哦)
检测是否安装成功,管理员身份打开cmd下在E:\wnmp\php 下面执行 php -v 看到版本号即代表安装成功,如下图:
nginx支持PHP(解析PHP文件)
1、打开文件 E:\wnmp\nginx\conf\nginx.conf(最好先做一下备份)然后修改nginx 配置文件
location / {
root html;
index index.html index.htm;
}
改为:(修改网站根目录路径,以及添加index.php的默认页)
location / {
root E:/wnmp/www;
index index.html index.htm index.php;
}
2、支持php的设置
#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;
#}
改为:(把 ‘/scripts’ 改为’$document_root’ ,这里的 ‘$document_root’ 就是指前面“root”所指的站点路径)
location ~ \.php$ {
root E:/wnmp/www;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
注意:改完配置文件nginx.conf后记得重启nginx哦
管理员打开cmd ,然后cd到nginx目录,输入nginx重启命令,如下图:
如无意外,在浏览器中输入 localhost/index.php ,看到下面内容即代表配置成功
安装MySQL
1、解压我们下载好的MySQL安装包到E:\wnmp\mysql里面
2、在这里新建一个文件my.ini,再新建一个文件夹data(如果有就不需要新建了),如下图:
3、编辑my.ini,文件内容如下:
[mysqld]
# 设置3306端口
port=3306
# 设置mysql的安装目录
basedir=E:\wnmp\mysql
# 设置mysql数据库的数据的存放目录
datadir=E:\wnmp\mysql\data
# 允许最大连接数
max_connections=200
# 允许连接失败的次数。这是为了防止有人从该主机试图攻击数据库系统
max_connect_errors=10
# 服务端使用的字符集默认为UTF8
character-set-server=utf8
# 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB
[mysql]
# 设置mysql客户端默认字符集
default-character-set=utf8
[client]
# 设置mysql客户端连接服务端时默认使用的端口
port=3306
default-character-set=utf8
4、以管理员身份打开cmd,转到E:\wnmp\mysql\bin,初始化命令(会生成一个临时密码,注意把临时密码记住)
移除mysql(如果之前安装过或安装失败)
mysqld -remove
mysqld --initialize --console
5、输入 mysqld -install 进行服务的添加
6、输入net start mysql启动服务,如下图:
7、输入mysql -u root -p进行登录数据库,用你上面的密码登录
8、重新设置密码:ALTER USER ‘root’@‘localhost’ IDENTIFIED WITH mysql_native_password BY ‘你的密码’;
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456';
9、退出:exit;
10、再管理员身份打开一个cmd窗口,重新登录一下,成功!大功告成了。
完整代码
mysql\my.ini
[mysqld]
# 设置3306端口
port=3306 # 设置mysql的安装目录
basedir=E:\wnmp\mysql\
# 设置mysql数据库的数据的存放目录
datadir=E:\wnmp\mysql\data
# 允许最大连接数
max_connections=200 # 允许连接失败的次数。这是为了防止有人从该主机试图攻击数据库系统
max_connect_errors=10 # 服务端使用的字符集默认为UTF8
character-set-server=utf8
# 创建新表时将使用的默认存储引擎 default-storage-engine=INNODB
# 默认使用“mysql_native_password”插件认证
default_authentication_plugin=mysql_native_password
[mysql]
# 设置mysql客户端默认字符集 default-character-set=utf8
[client]
# 设置mysql客户端连接服务端时默认使用的端口
port=3306
default-character-set=utf8
nginx\conf\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 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root E:/wnmp/www;
index index.html index.htm index.php;
}
#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 E:/wnmp/www;
fastcgi_pass 127.0.0.1:9000;
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;
#}
}
# 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;
# }
#}
}