资讯专栏INFORMATION COLUMN

php7.0 + mysql5.7.10 + nginx7.0 web开发环境搭建(CentOS7)

CollinPeng / 2598人阅读

摘要:一搭建开发环境参考官方文档设置官当提供了利用来安装升级的方法在目录下创建文件,输入以下代码将替换为,或者,根据当前的系统版本替换即可。设置好文件之后,开始用安装安装提示安装完成,则表明安装成功到此安装完成了。关闭停止禁止开机启动

一、搭建nginx开发环境

参考官方文档:http://nginx.org/en/linux_pac...

1、设置yum

官当提供了利用yum来安装、升级nginx的方法
在/etc/yum.repos.d/目录下创建nginx.repo文件,输入以下代码:

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/OSRELEASE/$basearch/
gpgcheck=0
enabled=1

将OSRELEASE替换为"5"、"6","7"或者"5.x"、"6.x"、"7.x",根据当前centos的系统版本替换即可。
设置好nginx.repo文件之后,开始用yum安装nginx

2、安装nginx
# sudo yum install -y nginx 

yum提示安装完成,则表明安装成功
到此nginx安装完成了。等我们安装完php,再对nginx进行设置!

二、安装php7 1、下载
# wget http://cn2.php.net/get/php-7.0.1.tar.gz/from/this/mirror

建议安装之前先看看安装帮助文件INSTALL

2、解压安装
# tar zxvf php-7.0.0.tar.gz
# cd php-7.0.0

首先查看安装帮助

# ./configure   --help

开始预编译

# ./configure --prefix=/usr/local/php 
 --with-curl 
 --with-freetype-dir 
 --with-gd 
 --with-gettext 
 --with-iconv-dir 
 --with-kerberos 
 --with-libdir=lib64 
 --with-libxml-dir 
 --with-mysqli 
 --with-openssl 
 --with-pcre-regex 
 --with-pdo-mysql 
 --with-pdo-sqlite 
 --with-pear 
 --with-png-dir 
 --with-xmlrpc 
 --with-xsl 
 --with-zlib 
 --enable-fpm 
 --enable-bcmath 
 --enable-libxml 
 --enable-inline-optimization 
 --enable-gd-native-ttf 
 --enable-mbregex 
 --enable-mbstring 
 --enable-opcache 
 --enable-pcntl 
 --enable-shmop 
 --enable-soap 
 --enable-sockets 
 --enable-sysvsem 
 --enable-xml 
 --enable-zip

如果配置错误,需要安装需要的模块,直接yum一并安装依赖库

# yum -y install libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel mysql pcre-devel
3、编译安装
# make &&  make install

4、配置文件
# cp php.ini-development /usr/local/php/lib/php.ini
# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
# cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
# cp -R ./sapi/fpm/php-fpm /etc/init.d/php-fpm

需要注意的是php7中www.conf这个配置文件配置phpfpm的端口号等信息,如果你修改默认的9000端口号需在这里改,再改nginx的配置

5、启动
#  /etc/init.d/php-fpm

查看phpinfo()

6、设置php-fpm按systemctl方式启动

注意:这一点是额外的,可以根据自身需求而定,我个人觉得每次用/etc/init.d/php-fpm 启动很不方便!

编辑php-fpm.conf

# vim /usr/local/php/etc/php-fpm.conf

把pid 改成 /run/php-fpm.pid

pid = /run/php-fpm.pid

加php-fpm管理器到systemctl中

# vim /usr/lib/systemd/system/php-fpm.service

按i写入以下内容

[Unit]
Description=The PHP FastCGI Process Manager
After=syslog.target network.target

[Service]
Type=simple
PIDFile=/run/php-fpm.pid
ExecStart=/usr/local/php/sbin/php-fpm --nodaemonize --fpm-config /usr/local/php/etc/php-fpm.conf
ExecReload=/bin/kill -USR2 $MAINPID
ExecStop=/bin/kill -SIGINT $MAINPID

[Install]
WantedBy=multi-user.target

启动php-fpm
systemctl start php-fpm.service
添加到开机启动
systemctl enable php-fpm.service
如果设置成功,可以将之前添加到/etc/init.d/php-fpm删除

三、根据php参数配置nginx
# vim /usr/local/php/etc/php-fpm.d/www.conf

查看listen的值,默认是 listen=127.0.0.1:9000

# cd /etc/nginx/conf.d
# vim default.conf

看到类似内容

server {
  2     listen       80;
  3     server_name  localhost;
  4 
  5     #charset koi8-r;
  6     #access_log  /var/log/nginx/log/host.access.log  main;
  7 
  8     location / {
  9         root   /usr/share/nginx/html;
 10         index  index.html index.htm;
 11     }
 12 
 13     #error_page  404              /404.html;
 14 
 15     # redirect server error pages to the static page /50x.html
 16     #
 17     error_page   500 502 503 504  /50x.html;
 18     location = /50x.html {
 19         root   /usr/share/nginx/html;
 20     }
 21 
 22     # proxy the PHP scripts to Apache listening on 127.0.0.1:80
 23     #
 24     #location ~ .php$ {
 25     #    proxy_pass   http://127.0.0.1;
 26     #}
 27 
 28     # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
 29     #
 30     location ~ .php$ {
 31         root           /usr/share/nginx/html;
 32         fastcgi_pass   127.0.0.1:9000;
 33         fastcgi_index  index.php;
 34     #  fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
 35         fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
 36         include        fastcgi_params;
 37     }
 38 
 39     # deny access to .htaccess files, if Apache"s document root
 40     # concurs with nginx"s one
 41     #
 42     #location ~ /.ht {
 43     #    deny  all;
 44     #}
 45 }

去掉30行至37行的注释,将root修改为19行的路径(默认localhost域名访问的文件路径)

三、rpm安装mysql

考虑到国内的网速较慢,mysql文件包较大,使用yum安装不太现实,源码编译耗时较久,在此我们采用rpm包安装!
可以参考官方文档:
http://dev.mysql.com/doc/refm...

1、下载rpm包

按照官方文档,我们需要逐一下载各个rpm包,然而官方文档也说了有打包好的rar供我们下载,我们需要做的只是按照自己的系统版本,下载对应的rar包

下表列出了各个系统对应mysql的rar包名,当前我的系统个版本是centOS7.1 64位,故我选择下载mysql-5.7.10-1.el7.x86_64.rpm-bundle.tar,带-bundle.tar的代表是所有rpm包的集合包!

注意:考虑到国内下载mysql官网mysql包可能较慢,特此提供国内的一个镜像:http://mirrors.sohu.com/mysql...
找到mysql-5.7.10-1.el7.x86_64.rpm-bundle.tar这个包,下载下来

2、安装

下载的rar包可以在windows下用rar软件解压,然后搬到centos中,由于本人使用的是vagrant,所以直接将解压好的文件夹放到了共享目录,供虚拟机访问

进入解压好的mysql包文件夹

# cd mysql-5.7.10-1.el7.x86_64.rpm-bundle

使用yum进行安装(也可以使用rpm命令安装,不过可能会遇到依赖性的错误,使用yum安装,最大好处就是不用解决软件包之间的依赖性!yum使用本地下载好的软件包安装起来也比较方便)

# sudo yum install mysql-community-{server,client,common,libs}-* mysql-5.* 

看到类似以下输出

Dependencies Resolved

=========================================================================================================================
 Package                         Arch       Version           Repository                                            Size
=========================================================================================================================
Installing:
 mysql-community-client          x86_64     5.7.10-1.el7      /mysql-community-client-5.7.10-1.el7.x86_64          109 M
 mysql-community-common          x86_64     5.7.10-1.el7      /mysql-community-common-5.7.10-1.el7.x86_64          2.5 M
 mysql-community-libs            x86_64     5.7.10-1.el7      /mysql-community-libs-5.7.10-1.el7.x86_64            9.8 M
     replacing  mariadb-libs.x86_64 1:5.5.41-2.el7_0
 mysql-community-libs-compat     x86_64     5.7.10-1.el7      /mysql-community-libs-compat-5.7.10-1.el7.x86_64     9.2 M
     replacing  mariadb-libs.x86_64 1:5.5.41-2.el7_0

Transaction Summary
=========================================================================================================================
Install  4 Packages

Total size: 130 M
Is this ok [y/d/N]:

这里输入y。看到类似以下输出:

Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Warning: RPMDB altered outside of yum.
  Installing : mysql-community-common-5.7.10-1.el7.x86_64                                                            1/5 
  Installing : mysql-community-libs-5.7.10-1.el7.x86_64                                                              2/5 
  Installing : mysql-community-client-5.7.10-1.el7.x86_64                                                            3/5 
  Installing : mysql-community-libs-compat-5.7.10-1.el7.x86_64                                                       4/5 
  Erasing    : 1:mariadb-libs-5.5.41-2.el7_0.x86_64                                                                  5/5 
warning: file /etc/my.cnf: remove failed: No such file or directory
  Verifying  : mysql-community-common-5.7.10-1.el7.x86_64                                                            1/5 
  Verifying  : mysql-community-libs-5.7.10-1.el7.x86_64                                                              2/5 
  Verifying  : mysql-community-client-5.7.10-1.el7.x86_64                                                            3/5 
  Verifying  : mysql-community-libs-compat-5.7.10-1.el7.x86_64                                                       4/5 
  Verifying  : 1:mariadb-libs-5.5.41-2.el7_0.x86_64                                                                  5/5 

Installed:
  mysql-community-client.x86_64 0:5.7.10-1.el7             mysql-community-common.x86_64 0:5.7.10-1.el7                 
  mysql-community-libs.x86_64 0:5.7.10-1.el7               mysql-community-libs-compat.x86_64 0:5.7.10-1.el7 

本人测试过程中发现没装上mysql-community-server.x86_64 0:5.7.10-1.el7

于是再次安装

# sudo yum install mysql-community-server-5.7.10-1.el7.x86_64.rpm

到此一共安装了六个软件包

启动mysql:

# sudo service mysqld start

验证是否启动成功:

# ps -aux | grep mysql

如果看到mysql进程则说明启动成功!

3、修改root用户密码

注意:新版mysql考虑到安全问题,root默认密码不再是root,有些说法认为,新版mysql会在root家目录下生成一个".mysql_secret"文件,本人并未查看到该文件。
首先修改mysql配置文件

# sudo vim /etc/my.cnf

在[mysqld]下面添加"skip-grant-tables",表示禁用授权,这样用户可以直接登录mysql!

重启mysql

# sudo service mysqld restart

登录mysql

# mysq

进入到mysql客户端界面

mysql >use mysql;
mysql >UPDATE user SET authentication_string=PASSWORD("NEWPASSWORD") WHERE User="root";
mysql >FLUSH PRIVILEGES;
mysql >quit;

注意:authentication_string=PASSWORD("NEWPASSWORD")中的PASSWORD是mysql的加密函数,用于生成加密字符串,所以不要漏掉这个函数!另外列名是User,而不是user。
注释配置文件中的免授权登录代码

# sudo vim /etc/my.cnf


重启mysql:

# sudo service mysqld restart

重启登录mysql客户端:

# mysql -uroot -p

输入密码,即可进行操作,如果还是报1820的错,则说明之前你设置的密码不符合密码规则,为了安全 ,新版mysql密码规则有所变化,设置的密码需包含大写、小写字母、数字、特殊符号。
到此mysql安装、设置基本完成,为了方便后期开发,我选择新建一个超级用户,如果你也有该需求,可以参考

4、新建可以远程连接mysql的管理员用户

使用mysql自带客户端添加可远程登陆的用户,首先使用root用户登陆本地的mysql,执行以下命令:

mysq> GRANT ALL PRIVILEGES ON *.* TO admin@localhost IDENTIFIED BY "你的密码" WITH GRANT OPTION;

上句代码新增了一个admin用户可以通过密码访问本机mysql数据库。

执行以下代码

mysq> GRANT ALL PRIVILEGES ON *.* TO admin@"%" IDENTIFIED BY "123456" WITH GRANT OPTION;

这一句代码则是授予用户admin用户通过任意主机访问mysql数据库

如果进行以上设置,依然无法远程连接,可以考虑有可能是iptables的问题
退出mysql客户端

mysql> exit;

CentOS 7.0默认使用的是firewall作为防火墙,这里改为iptables防火墙。
关闭firewall:

# systemctl stop firewalld.service #停止firewall
# systemctl disable firewalld.service #禁止firewall开机启动

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/26050.html

相关文章

  • ubuntu 16.04 下配置lnmp环境

    摘要:系统升级安装一条指令就搞定了安装默认路径在访问本地出现即是安装成功安装安装我之前写过环境配置内容大同小异故此处略下的开发环境搭建安装配置打开设置虚拟主机配置目录下新建文件写下如下内容通过指令快捷方式到目 系统升级 sudo atp-get update 安装nginx sudo apt-get install nginx一条指令就搞定了安装 默认web路径在/var/www/html...

    Karuru 评论0 收藏0
  • ubuntu 16.04 下配置lnmp环境

    摘要:系统升级安装一条指令就搞定了安装默认路径在访问本地出现即是安装成功安装安装我之前写过环境配置内容大同小异故此处略下的开发环境搭建安装配置打开设置虚拟主机配置目录下新建文件写下如下内容通过指令快捷方式到目 系统升级 sudo atp-get update 安装nginx sudo apt-get install nginx一条指令就搞定了安装 默认web路径在/var/www/html...

    zsy888 评论0 收藏0
  • 虚拟机上Centos7开发环境搭建(编译安装LNMP,多版本PHP共存,PHP扩展安装)

    摘要:安装系统本次安装的是的最小化版本,以下操作在用户下进行。在搭建完该环境后,笔者使用作为开发,配置使其可以直接调试虚拟机上的代码即开发环境下代码是运行在虚拟机上的。通过该方法,可以最大程度地确保开发环境与生产环境的一致性。 安装系统 本次安装的是Centos7.3的最小化版本,以下操作在root用户下进行。在搭建完该环境后,笔者使用phpstorm作为开发ide,配置phpstorm使其...

    zhangke3016 评论0 收藏0

发表评论

0条评论

CollinPeng

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<