今天我的一位朋友遇到这样一种情况,她只有一台服务器,但是这台服务器上面已经有了JSP的网站,也就是说此服务器已经有了“apache+tomcat"的环境配置,这个搭配中的apache主要用于解析html静态网页,tomcat主要用于解析java的,也就是jsp。
但是:我的这位朋友是做PHP程序员的,公司要求这台服务器上面也需要放PHP的网站,因为接的有PHP的业务。我们又应该如何做呢?
我的小建议:既然别人用了“apache+tomcat”来做JSP的网站,我们就用“Nginx+PHP”来做PHP的网站。别人肯定已经用了apache占用了80端口,我们nginx就用888端口。具体步骤如下:
一、Nginx 部份:nginx-1.14.2版本
1、下载Nginx,我这里下载的是Nginx稳定版:nginx-1.14.2;
2、解压Nginx,我这里解压后的目录是:G:\wnmp\nginx-1.14.2;
3、配置Nginx,我这里就说说配置文件要改的几个主要的地方,一个是端口,另一个就是网站根目录,最后一个就是让Nginx支持php解析,也是最重要的一个。
nginx.conf配置文件位置:G:\wnmp\nginx-1.14.2\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 888;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
# root html;
root G:/wnmp/nginx-1.14.2/html;
index index.php 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 G:/wnmp/nginx-1.14.2/html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME G:/wnmp/nginx-1.14.2/html$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;
# }
#}
}
从上面nginx.conf可以看出:nginx的端口是888;我的网站根目录地址是:G:/wnmp/nginx-1.14.2/html,这个路径是可以修改的,如果你不想修改,用默认的 html 也是可以的。
4、创建nginx“启动、停止、重启、退出、强制关闭”bat文件;因为我们这是纯手工配置,不像那些集成环境安装包那样操作方便,因此我们就需要自己来做。当然,你也可以直接进入cmd去操作nginx命令,但是那样太麻烦了。因此:Windows系统下,bat就是最简单最快的方法,
bat文件中的磁盘以及路径请修改为你自己的;
启动nginx.bat 代码:
@echo off
echo Starting Nginx...
G:
cd G:\wnmp\nginx-1.14.2
start nginx
停止nginx.bat 代码:
@echo off
echo fast stop Nginx...
G:
cd G:\wnmp\nginx-1.14.2
nginx -s stop
退出nginx.bat 代码:
@echo off
echo Quit Nginx...
G:
cd G:\wnmp\nginx-1.14.2
nginx -s quit
重启nginx.bat 代码:
@echo off
echo changing configuration Nginx...
G:
cd G:\wnmp\nginx-1.14.2
nginx -s reload
强制关闭nginx.bat 代码:
@echo off
echo close Nginx...
taskkill /F /im nginx.exe > nul
exit
5、启动nginx;现在我们直接点击“启动nginx.bat"文件,再用浏览器直接访问“http://127.0.0.1:888”已经可以出来“Welcome to nginx!”界面了,说明nginx此部份已经成功了!
二、PHP 部份:php-7.3.0版本
1、下载PHP,我这下载的版本是:php-7.3.0-nts-Win32-VC15-x64,很多人可能会想,我不下载nts的php版本可以吗?的确也是可以的。只要你下载的php版本里面有“php-cgi.exe"即可!我之所以下载nts版本的php,是因为曾经有高手说windows下nginx用nts版本的php会更加的好。
2、解压PHP,我这解压后的位置是:G:\wnmp\php-7.3.0-nts-Win32-VC15-x64;
3、配置PHP,复制或重命名一份php.ini-development为php.ini;由于配置太长,我把最主要的配置显示出来吧!
; Directory in which the loadable extensions (modules) reside.
; http://php.net/extension-dir
;extension_dir = "./"
; On windows:
extension_dir = "G:/wnmp/php-7.3.0-nts-Win32-VC15-x64/ext"
; If you wish to have an extension loaded automatically, use the following
; syntax:
;
; extension=modulename
;
; For example:
;
extension=mysqli
;
; When the extension library to load is not located in the default extension
; directory, You may specify an absolute path to the library file:
;
; extension=/path/to/extension/mysqli.so
;
; Note : The syntax used in previous PHP versions ('extension=<ext>.so' and
; 'extension='php_<ext>.dll') is supported for legacy reasons and may be
; deprecated in a future PHP major version. So, when it is possible, please
; move to the new ('extension=<ext>) syntax.
;
; Notes for Windows environments :
;
; - Many DLL files are located in the extensions/ (PHP 4) or ext/ (PHP 5+)
; extension folders as well as the separate PECL DLL download (PHP 5+).
; Be sure to appropriately set the extension_dir directive.
;
;extension=bz2
extension=curl
;extension=fileinfo
;extension=gd2
; cgi.fix_pathinfo provides *real* PATH_INFO/PATH_TRANSLATED support for CGI. PHP's
; previous behaviour was to set PATH_TRANSLATED to SCRIPT_FILENAME, and to not grok
; what PATH_INFO is. For more information on PATH_INFO, see the cgi specs. Setting
; this to 1 will cause PHP CGI to fix its paths to conform to the spec. A setting
; of zero causes PHP to behave as before. Default is 1. You should fix your scripts
; to use SCRIPT_FILENAME rather than PATH_TRANSLATED.
; http://php.net/cgi.fix-pathinfo
cgi.fix_pathinfo=1
4、创建PHP“启动、关闭”bat文件;因为我们是纯手工配置,要想启动php就是进入cmd用命令去操作,而且cmd窗口还不会关闭,这样不是很好,也非常的麻烦。因此,我推荐大家用bat文件。
启动php.bat 代码:
@echo off
if "%1" == "h" goto begin
mshta vbscript:createobject("wscript.shell").run("%~nx0 h",0)(window.close)&&exit
:begin
echo Starting PHP FastCGI...
G:\wnmp\php-7.3.0-nts-Win32-VC15-x64\php-cgi.exe -b 127.0.0.1:9000 -c G:\wnmp\php-7.3.0-nts-Win32-VC15-x64\php.ini
关闭php.bat 代码:
@echo off
echo Stopping PHP...
taskkill /F /im php-cgi.exe > nul
exit
5、启动PHP;现在我们直接点击“启动php.bat"文件,再用浏览器直接访问“http://127.0.0.1:888/phpinfo.php”已经可以出来“PHP Version 7.3.0”界面了,通过查找"mysqli扩展"也已经有了,说明php此部份也已经成功了!
三、MySQL 部份:mysql-8.0.13版本
1、下载mysql,我这里下载的是最新版本:mysql-8.0.13-winx64;
下载地址:https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.13-winx64.zip
2、解压mysql,我解压后的目录是:G:\wnmp\mysql-8.0.13-winx64;
2、创建my.ini配置,在G:\wnmp\mysql-8.0.13-winx64目录下新建my.ini。因为现在mysql高版本没有my.ini,因此必须自己去新建,如果有就算了。my.ini内容如下:
[mysqld]
# 设置3306端口
port=3306
# 设置mysql的安装目录
basedir=G:\wnmp\mysql-8.0.13-winx64
# 设置mysql数据库的数据的存放目录
datadir=G:\wnmp\mysql-8.0.13-winx64\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
3、打开cmd窗口,这里必须要以“管理员身份运行”打开cmd窗口。否则后面会出一些错误!后面我会讲到。
4、在cmd窗口通过命令进入到mysql的bin目录下,具体命令如下:
C:\Users\fujie>g:
G:\>cd G:\wnmp\mysql-8.0.13-winx64\bin
G:\wnmp\mysql-8.0.13-winx64\bin>
5、安装mysql服务及名称,主要是方便到时候建立多个mysql服务时不冲突,我这里创建的mysql服务名称就是mysql8。
G:\wnmp\mysql-8.0.13-winx64\bin>mysqld --install mysql8
Service successfully installed.
注意:如果我们刚才没有以“管理员身份运行“cmd窗口,运行此步命令就会提示“Install/Remove of the Service Denied!(安装/删除服务被拒绝!)”,就不会出现“Service successfully installed.(服务已成功安装)”。
6、mysql初始化,为了安全,mysql高版本都需要初始化,初始化都会得到一个myql连接地址、用户名和临时密码。我得到的是:root@localhost: q?uOMgblM9ww
G:\wnmp\mysql-8.0.13-winx64\bin>mysqld --initialize --console
2018-12-29T09:47:16.797923Z 0 [System] [MY-013169] [Server] G:\wnmp\mysql-8.0.13-winx64\bin\mysqld.exe (mysqld 8.0.13) initializing of server in progress as process 4480
2018-12-29T09:47:16.799220Z 0 [Warning] [MY-013242] [Server] --character-set-server: 'utf8' is currently an alias for the character set UTF8MB3, but will be an alias for UTF8MB4 in a future release. Please consider using UTF8MB4 in order to be unambiguous.
2018-12-29T09:47:34.500587Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: q?uOMgblM9ww
2018-12-29T09:47:45.680145Z 0 [System] [MY-013170] [Server] G:\wnmp\mysql-8.0.13-winx64\bin\mysqld.exe (mysqld 8.0.13) initializing of server has completed
7、启动mysql服务;我们就直接在cmd窗口执行如下命令:
G:\wnmp\mysql-8.0.13-winx64\bin>net start mysql8
mysql8 服务正在启动 ..
mysql8 服务已经启动成功。
8、登录到mysql数据库;可以证明一下我们的mysql是否正常了?此时登陆的密码是前面所生成的临时密码。
G:\wnmp\mysql-8.0.13-winx64\bin>mysql -u root -p
Enter password: ************
9、更改mysql用户名root的密码(重置密码);高版本的mysql都需要先修改密码,如果不修改你进入mysql是什么都操作不了的,会提示“ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.(ERROR 1820(HY000):在执行此语句之前,必须使用ALTER USER语句重置密码。)”错误。
以下语句是将我的root用户名的密码修改成为root;如果你是其它版本的mysql,修改密码的语句或许会不一样的,请自我百度。
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'root';
Query OK, 0 rows affected (0.08 sec)
此时密码更改成功后,我们可以看下用户信息:
mysql> select user,host,authentication_string from mysql.user;
+------------------+-----------+------------------------------------------------------------------------+
| user | host | authentication_string |
+------------------+-----------+------------------------------------------------------------------------+
| mysql.infoschema | localhost | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| mysql.session | localhost | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| mysql.sys | localhost | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| root | localhost | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
+------------------+-----------+------------------------------------------------------------------------+
4 rows in set (0.00 sec)
10、测试mysql用户名root密码是否修改成功?我们先用“quit"退出一下,再重新登陆一下mysql试试,毋庸质疑,结果当然是成功的。
G:\wnmp\mysql-8.0.13-winx64\bin>mysql -uroot -p
Enter password: ****
11、测试php能否连接上mysql数据库?结果当然是连接上了。先新建一个mysqli.php文件,放在网站 代根目录下面,PHP代码如下:
<?php
/* Connect to a MySQL server 连接数据库服务器 */
$link = mysqli_connect(
'localhost', /* The host to connect to 连接MySQL地址 */
'root', /* The user to connect as 连接MySQL用户名 */
'root', /* The password to use 连接MySQL密码 */
'mysql'); /* The default database to query 连接数据库名称*/
if (!$link) {
printf("Can't connect to MySQL Server. Errorcode: %s ", mysqli_connect_error());
exit;
}else
echo '数据库连接上了!';
/* Close the connection 关闭连接*/
mysqli_close($link);
?>
12、创建mysql“启动、关闭“bat文件,前面讲了很多了,至于为什么,我这里就不多说了。有一点需要注意,mysql的bat文件,必须以“管理员身份”运行,否则是不生效的。也可以理解为你不以管理员身份运行,你就没有权限。
启动mysql.bat 代码:
@echo off
echo Starting mysql...
net start mysql8
停止mysql.bat 代码:
@echo off
echo Stoping mysql...
net stop mysql8
关闭mysql.bat 代码:
@echo off
echo Close mysql...
taskkill /F /im mysqld.exe > nul
exit
总结:此时就算真正大功告成了,上面的“配置文件”讲的可能不是很详细,具体你要哪些可以自己去配置文件再修改的,我主要是先让nginx+php+mysql能够先跑起来。
如果有不懂,请评论留言或直接联系我即可!