centos配置PHP+nginx+MySQL环境

一、配置nginx

       1、下载对应当前系统版本的nginx包(package)

              wget http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

       2、建立nginx的yum仓库

             rpm-ivh nginx-release-centos-7-0.el7.ngx.noarch.rpm

       3、使用yum安装Nginx:

             sudo yum install nginx

       4、按照提示,输入yes后开始安装。安装完毕后,Nginx的配置文件在/etc/nginx目录下。使用以下命令启动Nginx:

             sudo systemctl start nginx

       5、检查系统中firewalld防火墙服务是否开启,如果已开启,我们需要修改防火墙配置,开启Nginx外网端口访问。

             sudo systemctl status firewalld

       6、如果显示active (running),则需要调整防火墙规则的配置。

            修改/etc/firewalld/zones/public.xml文件,在zone一节中增加:

           

                   ...

                   

           

        7、保存后重新加载firewalld服务:

              sudo systemctl reload firewalld

        8、您可以通过浏览器访问http://<外网IP地址>来确定Nginx是否已经启动。

        9、最后将Nginx设置为开机启动:

              sudo systemctl enable nginx.service

二、mariadb安装

       1、安装mariadb:

             sudo yum install mariadb-server

       2、启动MariaDB服务

             sudo systemctl start mariadb

       3、MariaDB默认root密码为空,我们需要设置一下,执行脚本:

             sudo /usr/bin/mysql_secure_installation

            ①首先提示输入当前的root密码:

                Enter current password for root (enter for none):初始root密码为空,我们直接敲回车进行下一步。

                 Set root password? [Y/n]设置root密码,默认选项为Yes,我们直接回车,提示输入密码,在这里设置您的MariaDB的root账户密码。

                 Remove anonymous users? [Y/n]是否移除匿名用户,默认选项为Yes,建议按默认设置,回车继续。

                 Disallow root login remotely? [Y/n]是否禁止root用户远程登录?如果您只在本机内访问MariaDB,建议按默认设置,回车继续。如果您还有其他云主机需要使用root账号访问该数据库,则需要选择n。

                 Remove test database and access to it? [Y/n]是否删除测试用的数据库和权限?建议按照默认设置,回车继续。

                 Reload privilege tables now? [Y/n]是否重新加载权限表?因为我们上面更新了root的密码,这里需要重新加载,回车。完成后你会看到Success!的提示,MariaDB的安全设置已经完成。

             ②以权限用户root登录MariaDB:

                 mysql -uroot -p

                 按提示输入root密码,就会进入MariaDB的交互界面,说明已经安装成功。

             ③选择mysql库

                 use mysql;

             ④查看mysql库中的user表的host值(即可进行连接访问的主机/IP名称)

                 select 'host' from user whereuser='root';

             ⑤修改host值(以通配符%的内容增加主机/IP地址),当然也可以直接增加IP地址,如果这步出错"ERROR 1062(23000): Duplicate entry '%-root' for key 'PRIMARY'"由说明该记录有了,跳过这步

                 update user set host = '%' where user='root';

             ⑥刷新MySQL的系统权限相关表

                 flush privileges;

             ⑦再重新查看user表时,有修改,然后重起mariadb服务即可完成

       4、最后我们将MariaDB设置为开机启动。

             sudo systemctl enable mariadb

三、mysql安装

1.先下载mysql5.7的repo源;相关命令:

wgethttp://repo.mysql.com/mysql57-community-release-el7-8.noarch.rpm

2.安装mysql57-community-release-el7-8.noarch.rpm:(安装这个包后,会获得两个mysql的yum repo源:/etc/yum.repos.d/mysql-community.repo,/etc/yum.repos.d/mysql-community-source.repo):

rpm-ivh mysql57-community-release-el7-8.noarch.rpm

3.安装MySQL:

yuminstall mysql-server

一路Y下去即可;

4.安装完毕后,重启服务器命令:

servicemysqld restart

注:在/var/log/mysqld.log文件中会自动生成一个随机的密码,我们需要先取得这个随机密码,以用于登录MySQL服务端:

grep"password" /var/log/mysqld.log

会打印出如下内容:

Atemporary password is generated for root@localhost: hilX0U!9i3_6

我们复制root@localhost:后面的随机字符串,这个字符串就是MySQL在安装完成后为我们随机生成的密码;

密码就在这个日志的文件里

/var/log/mysqld.log

例如:标注的就是密码

2017-03-26T21:53:36.234194Z1 [Note] A temporary password is generated for root@localhost: >b&uAW1D>n7k

5.登录到MySQL服务端并更新用户root的密码:

mysql-u root -philX0U!9i3_6

打印出MySQL的版本即表明已登录;

由于修改密码:

setpassword for 'fred'@'localhost'=password('passwd');

可能会报错

由以下两个命令解决:

setglobal validate_password_policy=0;

setglobal validate_password_length=4;

开始设置自己的密码:

setpassword for root@localhost=password('passwd');

刷新权限使之生效:

flushprivileges;

设置用户root可以在任意IP下被访问:

grant

all privileges on *.* to root@"%" identified by "新密码";

设置用户root可以在本地被访问:

grant

all privileges on *.* to root@"localhost" identified by "新密码";

刷新权限使之生效:

flushprivileges;

更新MySQL的用户root的密码:

set

password = password('新密码');

注意:由于MySQL5.7采用了密码强度验证插件validate_password,故此我们需要设置一个有一定强度的密码;

输入exit后用新密码再次登录看看吧!

5.将mysql设置为开机启动:

sudosystemctl enable mysqld

安装PHP7.0

1.删除旧版本

如果已经安装过php就先删除之前的版本。检查方法如下:

yumlist installed | grep php

然后将安装的包进行删除

比如yum remove php.x86_64 php-cli.x86_64 php-common.x86_64php-gd.x86_64 php-ldap.x86_64 php-mbstring.x86_64 php-mcrypt.x86_64php-mysql.x86_64 php-pdo.x86_64

具体根据显示的安装列表的名称进行相应的删除

2.rpm安装Php7相应的yum源:

rpm-Uvh https://mirror.webtatic.com/yum/el7/epel-release.rpm

​rpm -Uvhhttps://mirror.webtatic.com/yum/el7/webtatic-release.rpm

3.安装php7.0

yuminstall php70w

4.安装php扩展

​php70w-mysql.x86_64mysql扩展(作为依赖同时安装php70w-pdo.x8664)

​php70w-gd.x86_64GD库,是php处理图形的扩展库,GD库提供了一系列用来处理图片的API,使用GD库可以处理图片,或者生成图片。

​php70w-ldap.x86_64 "轻量级目录访问协议",是一个用于访问"目录服务器"(Directory Servers)的协议;

php70w-mbstring.x86_64

mbstring扩展库用于处理多字节字符串,PHP内置的字符串长度函数strlen无法正确处理中文字符串,它得到的只是字符串所占的字节数。对于GB2312的中文编码,strlen得到的值是汉字个数的2倍,而对于UTF-8编码的中文,就是1~3倍的差异了。采用mb_strlen函数可以较好地解决这个问题。

php70w-mcrypt.x86_64

Mcrypt扩展库可以实现加密解密功能,就是既能将明文加密,也可以密文还原。

5.安装PHP FPM

​yum install php70w-fpm

关于php-fpm

​PHP-FPM其实是PHP源代码的一个补丁,旨在将FastCGI进程管理整合进PHP包中。必须将它patch到你的PHP源代码中,在编译安装PHP后才可以使用。

​现在我们可以在最新的PHP 5.3.2的源码树里下载得到直接整合了PHP-FPM的分支,据说下个版本会融合进PHP的主分支去。相对Spawn-FCGI,PHP-FPM在CPU和内存方面的控制都更胜一筹,而且前者很容易崩溃,必须用crontab进行监控,而PHP-FPM则没有这种烦恼。

​PHP5.3.3已经集成php-fpm了,不再是第三方的包了。PHP-FPM提供了更好的PHP进程管理方式,可以有效控制内存和进程、可以平滑重载PHP配置,比spawn-fcgi具有更多优点,所以被PHP官方收录了。在./configure的时候带–enable-fpm参数即可开启PHP-FPM。

使用PHP-FPM来控制PHP-CGI的FastCGI进程

配置nginx

修改配置文件之前记得备份

1.nginx配置文件位置:(/etc/nginx/nginx.conf)

​# For more information on configuration,see:

#* Official English Documentation: http://nginx.org/en/docs/

#* Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;

worker_processes auto;

error_log

/var/log/nginx/error.log; #错误日志记录的位置

pid /run/nginx.pid; #nginx.pid为记录nginx主进程pid文件;切勿修改、移动

# Load dynamic modules. See/usr/share/nginx/README.dynamic.

include/usr/share/nginx/modules/*.conf;

#引入/usr/share/nginx/modules/目录下的所有以.conf结尾的文件

events {

worker_connections 1024;

}

http {

log_formatmain'$remote_addr - $remote_user [$time_local]"$request" '

'$status $body_bytes_sent"$http_referer" '

'"$http_user_agent" "$http_x_forwarded_for"';

access_log/var/log/nginx/access.logmain;

sendfileon;

tcp_nopushon;

tcp_nodelayon;

keepalive_timeout65;

types_hash_max_size 2048;

include/etc/nginx/mime.types;

default_typeapplication/octet-stream;

# Load modular configuration files from the /etc/nginx/conf.d directory.

# See http://nginx.org/en/docs/ngx_core_module.html#include

# for more information.

include /etc/nginx/conf.d/*.conf;

#这句很重要,引入所有etc/nginx/conf.d/目录下的.conf文件

#***etc/nginx/conf.d/目录存放的就是分站点的文件(下面会给出实例代码)***

server {

#由于我们的nginx需要配置多站点,所以在此就需要注释一些东西

listen80 default_server;

listen[::]:80 default_server;

#保留监听的端口

# server_name_;

# root/usr/share/nginx/php;

# Load configuration files for thedefault server block.

# include /etc/nginx/default.d/*.conf;

# location / {

# }

# error_page 404 /404.html;

location = /40x.html {

}

error_page 500 502 503 504 /50x.html;

location = /50x.html {

}

# location ~ \.php$ {

# root/usr/share/php;

# fastcgi_pass127.0.0.1:9000;

# fastcgi_indexindex.php;

# fastcgi_paramSCRIPT_FILENAME$document_root$fastcgi_script_name;

# includefastcgi_params;

# }

}

# Settings for a TLS enabledserver.

#

#server {

#listen443 ssl http2 default_server;

#listen[::]:443 ssl http2 default_server;

#server_name_;

#root/usr/share/nginx/html;

#

#ssl_certificate"/etc/pki/nginx/server.crt";

#ssl_certificate_key"/etc/pki/nginx/private/server.key";

#ssl_session_cache shared:SSL:1m;

#ssl_session_timeout10m;

#ssl_ciphers HIGH:!aNULL:!MD5;

#ssl_prefer_server_ciphers on;

#

## Load configuration files for thedefault server block.

#include /etc/nginx/default.d/*.conf;

#

#location / {

#}

#

#error_page 404 /404.html;

#location = /40x.html {

#}

#

#error_page 500 502 503 504 /50x.html;

#location = /50x.html {

#}

#}

}

2.php-fpm配置文件位置:(/etc/php-fpm.d/www.conf)

​修改

user = nginx

​ group= nginx

3.启动nginx服务:

systemctl start nginx.service

​如需设置开机自启使用以下命令:

sudo systemctl enable nginx.service

​查看启动状态:

systemctl status nginx

看到以下字眼说明启动成功!

​Active:

active (running) since六2016-11-19 13:40:04 CST; 50min ago

4.启动PHP-FPM:

systemctl start php-fpm.service

如需设置开机自启试用以下命令:

sudo systemctl enable php-fpm.service

​查看启动状态:

systemctl status php-fpm.service

看到以下字眼说明启动成功!

​Active:

active (running) since六2016-11-19 14:14:33 CST; 18min ago

你可能感兴趣的:(centos配置PHP+nginx+MySQL环境)