阿里云ECS服务安装 nginx+php+MariaDB完整版

安装 Nginx
想在 CentOS 系统上安装 Nginx ,你得先去添加一个资源库,像这样:

vim /etc/yum.repos.d/nginx.repo
使用 vim 命令去打开 /etc/yum.repos.d/nginx.repo ,如果 nginx.repo 不存在,就会去创建一个这样的文件,打开以后按一下小 i 键,进入编辑模式,然后复制粘贴下面这几行代码,完成以后按 esc 键退出,再输入 :wq (保存并退出)

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1
完成以后,我们就可以使用 yum 命令去安装 nginx 了,像这样:

yum install nginx
安装好以后测试一下 nginx 服务:

service nginx status
应该会返回:

nginx is stopped (nginx 已停止)
再测试一下 nginx 的配置文件:

nginx -t
应该会返回:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
... syntax is ok,... test is successful,说明配置文件没问题,同时这个结果里你可以找到 nginx 的配置文件 nginx.conf 所在的位置。

操纵 nginx 服务

操纵服务,可以使用使用 service 命令,它可以启动(start),重启(restart),或停止服务(stop),比如要启动 nginx 服务:

service nginx start
服务启动以后,你就可以在浏览器上使用服务器的 IP 地址,或者指向这个地址的域名访问服务器指定的目录了。你会看到类似下面的这些文字。

Welcome to nginx! If you see this page, the nginx web server is successfully installed and working. Further configuration is required. For online documentation and support please refer to nginx.org. Commercial support is available at nginx.com. Thank you for using nginx.

配置 php-fpm
要让 nginx 能够执行 php 文件,需要去安装一下 php-fpm,它直接包含在了 CentOS 资源库里,所以直接使用 yum 命令可以安装它:

yum install php-fpm
完成以后,可以检查一下 php-fpm 的运行状态,使用 service 命令:

service php-fpm status
返回:

php-fpm is stopped(php-fpm 已停止)
启动 php-fpm 同样可以使用 service 命令:

service php-fpm start

让 nginx 可以执行 php
现在我们应该就可以让 nginx 去执行 php 了。不过你需要修改一下 nginx 的配置文件,之前我们在配置虚拟主机的时候,创建了一个 nginx.ninghao.net.conf 的配置文件,需要去修改下 nginx 的这个配置文件,才能去执行 php 。使用 vim 命令去编辑它:

vim /etc/nginx/conf.d/www.96net.com.cn.conf
注意你的配置文件不一定叫 nginx.www.96net.com.cn,应该是你自己命名的配置文件。打开以后,找到下面这段字样的代码:

# 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 $document_root$fastcgi_script_name;
include fastcgi_params;
}
注意 root 那里仍然是被注释掉的,还有 SCRIPT_FILENAME 后面修改了一下,把 /scripts 换成了 $document_root 。保存并退出。然后重新启动 nginx:

service nginx restart

安装 php 扩展

现在,我们有了可以提供 web 服务的 nginx ,并且安装了 php-fpm ,配置了 nginx 可以让它去执行 php ,也安装了数据库管理系统。不过在运行真正的网站的时候,我们还需要为 php 安装一些额外的扩展,比如 处理 mysql 数据库的 mysql 扩展,缓存功能的 apc 扩展,处理图像的 gd 扩展等等。安装它们同样可以使用 yum 命令。

yum install php-pecl-apc php-mysql php-gd php-mcrypt php-pear php-mbstring php-xmlrpc php-dom

上面安装了一些 php 的扩展,如果你发现在安装网站的时候提示需要安装其它的扩展,同样可以使用 yum 命令去安装。安装完成以后,需要重启一下 php-fpm :

service php-fpm restart
上一篇:剑指offer——python【第43题】左旋转字符串


下一篇:华为/华三交换机snmp配置