一、nginx相关搭建
1、nginx(web server) 安装
1.1 什么是WebServer
Webserver能够解析HTTP协议。当Webserver接收到一个HTTP请求,会返回一个HTTP响应,比如送回一个HTML页面。为了处理一个请求Webserver能够响应一个静态页面或图片,进行页面跳转或者把动态响应的产生托付给一些其他的程序比如CGI脚本,JSP脚本,servlets,ASP脚本,server端JavaScript,或者一些其他的server端技术。不管它们的目的怎样,这些server端的程序通常产生一个HTML的响应来让浏览器能够浏览。
1.2 往yum内添加nginx资源库:
vim /etc/yum.repos.d/nginx.repo
使用 vim 命令去打开 nginx.repo ,如果 nginx.repo 不存在,就会去创建一个这样的文件,
打开以后按一下小 i 键,进入编辑模式,然后复制粘贴下面这几行代码,完成以后按 esc 键退出,再输入 :wq (保存并退出)
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1
1.3 完成后,使用 yum 命令安装 nginx :
yum install nginx
1.4 安装好以后查看nginx 当前状态:
systemctl status nginx
应该会返回:nginx is stopped (nginx 已停止)
1.5 查看nginx 的配置文件:
nginx -t
应该会返回:nginx: the configuration file /etc/nginx/nginx.conf syntax is ok.... test is successful
说明配置文件没问题,同时这个结果里你可以找到 nginx 的配置文件 nginx.conf 所在的位置。
2、nginx 相关操作
查看nginx配置文件路径:ps aux | grep nginx
相关操作:启动(start),重启(restart),或停止服务(stop),比如启动 nginx 服务:
systemctl start nginx
服务启动以后,你就可以在浏览器上使用服务器的 IP 地址,或者指向这个地址的域名访问服务器指定的目录了。你会看到类似下面的这些文字。
Welcome to nginx! If you see this page, the nginx web server is successfully installed and working. ...Thank you for using nginx.
3、nginx 虚拟机配置
安装完 nginx 以后,第一件想到的事应该就是去创建虚拟主机,虚拟主机允许我们在同一台服务器上运行多个网站,我们可以为不同的域名绑定不同的目录,访问这个域名的时候,会打开对应目录里面的东西。
3.1 进入到 nginx 配置文件目录:
cd /etc/nginx/conf.d
复制目录里的 default.conf ,复制以后的名字可以使用你的虚拟主机名字。比如创建一个 nginx.virtual_01.net 的虚拟主机。
cp default.conf nginx.virtual_01.net.conf
3.2 编辑 nginx.virtual_01.net.conf 配置文件
vim nginx.virtual_01.net.conf
你会看到像这样的代码
server {
listen 80;
server_name localhost; // 主机名(跟这个虚拟主机绑定在一块儿的域名)
location / {
root /usr/share/nginx/html; // 虚拟主机的根目录(网站所在的目录)
index index.html index.htm; // 默认打开的文件
}
修改为:
server {
listen 80;
server_name nginx.virtual_01.net
location / {
root /home/www/nginx.virtual_01.net;
index index.php index.html index.htm;
}
现在虚拟主机应该就可以使用了。主机的域名是 nginx.virtual_01.net,访问它的时候,打开是/home/www/nginx.virtual_01.net; 这个目录里面的东西,你可以在这个目录下放点东西。重启 nginx 或者重新加载 nginx 可以让配置文件生效。
systemctl restart nginx
3.3 配置客户端 hosts 文件: 阿里云公网Ip nginx.virtual_01.net
浏览器访问:nginx.virtual_01.net/xxx.html
二、php-fpm 相关搭建
1、php-fpm 的安装
1.1 要让 nginx 能够执行 php 文件,需要安装一下 php-fpm,它直接包含在了 CentOS 资源库里:
yum install php-fpm
1.2 php-fpm 相关操作命令:
启动: systemctl start php-fpm
重启: systemctl restart php-fpm
查看状态:systemctl status php-fpm
2、修改nginx配置,让 nginx 可以执行 php
2.1 修改 nginx.virtual_01.net.conf 文件
vim nginx.virtual_01.net.conf
打开以后,找到下面这段字样的代码:
# 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;
#}
这是 nginx 默认给我们的用来执行 php 的配置,从 location 开始取消注释,会让这个配置生效,然后我们还得简单去修改一下:
# 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 /home/www/nginx.virtual_01.net$fastcgi_script_name;
include fastcgi_params;
}
注意 root 仍然是被注释掉的,还有 SCRIPT_FILENAME 后面修改了一下,把 /scripts 换成了 /home/www/nginx.virtual_01.net 。保存并退出。然后重新启动 nginx:
systemctl restart nginx
2.2 测试是否可以执行 php
在nginx.ninghao.net目录下新建一个测试的 index.php 文件,访问:http://nginx.ninghao.net/index.php