centos下安装ngnix+php+mysql服务
一、nginx 安装
1.查看yum下nginx版本信息
[root@localhost ~]# yum list | grep nginx
2.手动添加nginx的yum仓库
[root@localhost ~]# vi /etc/yum.repos.d/nginx.repo
添加的内容为:
[nginx] name=nginx repo baseurl=http://nginx.org/packages/centos/$releasever/$basearch/ gpgcheck=0 enabled=1
3.编辑保存之后再查看nginx版本:
[root@localhost ~]# yum list | grep nginx
4.安装nginx服务:
[root@localhost ~]# yum install -y nginx
5.启动nginx服务:
[root@localhost ~]# service nginx start
注意:/etc/nginx/nginx.conf # Nginx配置文件位置
6.设置开机启动
[root@localhost ~]# chkconfig nginx on
7.nginx服务重启:
[root@localhost ~]# /etc/init.d/nginx restart
8.测试是否正常:
[root@localhost ~]# Curl http://127.0.0.1
9.删除默认的测试页面:
root@localhost ~]# rm -rf /usr/share/nginx/html/*
二、安装mysql
1.安装mysql
[root@localhost ~]# yum install mysql mysql-server
2.启动mysql
[root@localhost ~]# /etc/init.d/mysqld start
3.设置开机启动
[root@localhost ~]# chkconfig mysqld on
4.为root设置密码:
[root@localhost ~]# mysql_secure_installation
5.重启启动mysql服务:
[root@localhost ~]# /etc/init.d/mysqld stop #停止 [root@localhost ~]# /etc/init.d/mysqld start #启动 [root@localhost ~]# service mysqld restart #重启
6.设置允许远程连接mysql
[root@localhost ~]# mysql -u root -p Mysql>use mysql; Mysql>grant all privileges on *.* to 'root'@'%' identified by '123456' with grant option; [root@localhost ~]# service mysqld restart #重启
三、安装PHP
1.安装PHP
[root@localhost ~]# yum install php -y
2.安装PHP组件,使PHP支持 MySQL、PHP支持FastCGI模式
[root@localhost ~]# yum install php-mysql php-gd libjpeg* php-imap php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-mcrypt php-bcmath php-mhash libmcrypt libmcrypt-devel php-fpm
3.重启MySql
/etc/init.d/mysqld restart
4.重启nginx
/etc/init.d/nginx restart
5.启动php-fpm
/etc/rc.d/init.d/php-fpm start
6.设置开机启动php-fpm
chkconfig php-fpm on
四、配置nginx支持php
1.备份原有配置文件
cp /etc/nginx/nginx.conf /etc/nginx/nginx.confbak
2.编辑配置文件
vim /etc/nginx/nginx.conf user nginx nginx; #修改nginx运行账号为:nginx组的nginx用户 :wq! #保存退出
3.备份原有默认配置文件
cp /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.confbak
4.编辑默认配置
vim /etc/nginx/conf.d/default.conf
修复内容为:
index index.php index.html index.htm; #增加index.php
修改为以下内容:
# pass the PHPscripts 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; } #取消FastCGI server部分location的注释,并要注意fastcgi_param行的参数,改为$document_root$fastcgi_script_name,或者使用绝对路径
五、配置php
1.配置文件
vim /etc/php.ini #disable_functions=passthru,exec,system,chroot,scandir,chgrp,chown,shell_exec #在386行 列出PHP可以禁用危险的函数 #expose_php = Off #在432行 禁止显示php版本的信息 #magic_quotes_gpc = On #在745行 打开magic_quotes_gpc来防止SQL注入
六、配置php-fpm
1.备份原有配置文件
cp /etc/php-fpm.d/www.conf /etc/php-fpm.d/www.confbak
2.编辑配置文件
vim /etc/php-fpm.d/www.conf
修改为以下:
user = nginx #修改用户为nginx
group = nginx #修改组为nginx
3.重启MySql
/etc/init.d/mysqld restart
4.重启nginx
/etc/init.d/nginx restart
5.重启php-fpm
/etc/rc.d/init.d/php-fpm restart
总结:到处已成功搭建起在centos下的ngnix+php+mysql应用!