centos7+PHP 7.1.7+mariadb 10.2.7+nginx 1.10.2 编译安装

    作为一个phper,一直想学习一下在Linux环境下面环境的搭建,终于腾下来时间,可以好好梳理一下。对自己的知识做一个巩固,在以后的工作生活中,可以用到。

    首先,需要买一台服务器、一个域名;后面配置需要用到,当然也可以在本机上面装一个虚拟机,在虚拟机里面学习搭建。我呢,这里就先演示下在虚拟机里面搭建 Lnmp 的开发环境吧!先说一下我打的环境的各个版本,基于学就用新的原则,我的各个版本如下:

Lnmp环境:

linux:   CentOS Linux release 7.3.1611 (Core)  【 CentOS-7-x86_64-DVD-1611.iso】

nginx:    nginx/1.10.2

mysql:   10.2.7-MariaDB

php:      PHP 7.1.7


准备工作

系统环境: 

(适用于所有的linux,包括Redhat、SuSE、Debian等发行版,但是在debian下要安装lsb)

[root@localhost ~]# lsb_release -a

bash: lsb_release: 未找到命令...

[root@localhost ~]# yum install -y redhat-lsb   #使用yum安装lsb

[root@localhost ~]# lsb_release -a

LSB Version:    :core-4.1-amd64:core-4.1-noarch:cxx-4.1-amd64:cxx-4.1-noarch:desktop-4.1-amd64:desktop-4.1-noarch:languages-4.1-amd64:languages-4.1-noarch:printing-4.1-amd64:printing-4.1-noarch

Distributor ID:    CentOS

Description:    CentOS Linux release 7.3.1611 (Core)

Release:    7.3.1611

Codename:    Core


系统版本说明

[root@localhost ~]# cat /etc/redhat-release

CentOS Linux release 7.3.1611 (Core)

检查可更新的所有软件包:

[root@localhost ~]# yum check-update

下载更新系统已经安装的软件包:

[root@localhost ~]# yum update


一、使用centos7默认防火墙

[root@localhost ~]# systemctl status firewalld.service       #查看当前firewall状态

[root@localhost ~]# systemctl start firewalld.service         #开始运行firewall

[root@localhost ~]# systemctl enable firewalld.service     #设置firewall开机启动

[root@localhost ~]# systemctl stop firewalld.service         #停止firewall

[root@localhost ~]# systemctl restart firewalld.service     #重启firewall

[root@localhost ~]# systemctl disable firewalld.service    #禁止firewall开机启动


二、关闭SELINUX

[root@localhost ~]# vi /etc/selinux/config


#SELINUX=enforcing         #注释掉

#SELINUXTYPE=targeted  #注释掉

SELINUX=disabled            #增加

:wq                                    #保存,退出

[root@localhost ~]# setenforce 0


三、系统约定

软件源代码包存放位置: /usr/local/src

源码包编译安装位置:    /usr/local/软件名字


四、编译安装nginx、mariadb、php7

1、下载mariadb  编译 安装

附:

mariadb下载地址:https://downloads.mariadb.org/

首先查询下是否安装了mysql或者旧版本mariadb

[root@localhost ~]# rpm -qa | grep mysql

[root@localhost ~]# rm -rf /etc/my.cnf               #删除mysql配置文件,不删除后面也回去覆盖


安装开发环境

[root@localhost ~]# yum groupinstall 'Development Tools'


安装依赖包

[root@localhost ~]# yum install -y ncurses-devel libxml2-devel openssl-devel libaio-devel libcurl-devel libarchive-devel boost boost-devel jemalloc


下载cmake

[root@localhost ~]# wget -O cmake-3.9.0.tar.gz https://cmake.org/files/v3.9/cmake-3.9.0.tar.gz

下载mariadb 10.2.7

[root@localhost ~]# wget -O mariadb-10.2.7.tar.gz  http://mirrors.tuna.tsinghua.edu.cn/mariadb//mariadb-10.2.7/source/mariadb-10.2.7.tar.gz


移动软件到安装目录

[root@localhost ~]# mv mariadb-10.2.7.tar.gz /usr/local/src/

[root@localhost ~]# mv cmake-3.9.0.tar.gz  /usr/local/src/


切换到安装目录、解压缩

[root@localhost ~]# cd /usr/local/src/

[root@localhost src]# ls

cmake-3.9.0.tar.gz  mariadb-10.2.7.tar.gz

[root@localhost src]# tar -zxvf cmake-3.9.0.tar.gz

[root@localhost src]# tar -zxvf mariadb-10.2.7.tar.gz  


配置、编译、安装cmake依赖

[root@localhost src]# cd cmake-3.9.0/

[root@localhost cmake-3.9.0]# ./bootstrap

[root@localhost cmake-3.9.0]# make &&  make install


创建MySQL用户和组:

[root@localhost cmake-3.9.0]#  groupadd mysql

[root@localhost cmake-3.9.0]#  useradd -g mysql -s /usr/sbin/nologin mysql

创建数据库数据存放目录及赋予权限

[root@localhost cmake-3.9.0]# mkdir -pv /data/mysql/

[root@localhost cmake-3.9.0]# chown -R mysql:mysql /data/mysql

[root@localhost cmake-3.9.0]# mkdir -pv /usr/local/mysql/logs


配置、编译、安装mariadb

[root@localhost cmake-3.9.0]# cd ..

[root@localhost src]# cd mariadb-10.2.7/

[root@localhost mariadb-10.2.7]# cmake . \

-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \       #安装目录

-DMYSQL_DATADIR=/data/mysql \                   #数据库存放目录

-DSYSCONFDIR=/etc \

-DMYSQL_USER=mysql \

-DWITHOUT_TOKUDB=1 \

-DWITH_INNOBASE_STORAGE_ENGINE=1 \              #支持数据库innobase引擎

-DWITH_ARCHIVE_STORAGE_ENGINE=1 \               #支持数据库archive引擎

-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \             #支持数据库blackhole存储引擎

-DWITH_PARTITION_STORAGE_ENGINE=1  \ 

-DWITHOUT_MROONGA_STORAGE_ENGINE=1  \

-DWITH_READLINE=1 \

-DWITH_SSL=system \

-DWITH_ZLIB=system \

-DWITH_LIBWRAP=0 \

-DMYSQL_UNIX_ADDR=/tmp/mysql.sock \

-DDEFAULT_CHARSET=utf8 \                         #字符集utf8

-DDEFAULT_COLLATION=utf8_general_ci \            #校验字符

-DENABLED_LOCAL_INFILE=1                         #允许本地导入数据

-- Configuring done

-- Generating done

-- Build files have been written to: /usr/local/src/mariadb-10.2.7

[root@localhost mariadb-10.2.7]# make && make install


可能遇到的问题:

1、cmake的时候如果报    -bash: /usr/bin/cmake: 没有那个文件或目录

是因为直接使用cmake系统回到默认的/usr/bin中去寻找,但是src中安装的cmake是在/usr/local/bin中,所以当然不会找到这个东东啦。解决方法:

1、做一个链接即可:

[root@localhost cmake-3.9.0]# ln -s /usr/local/bin/cmake /usr/bin

2、直接使用/usr/local/bin/cmake进行编译。

2、注意:如果万一执行中有了错误,可以执行: rm -f CMakeCache.txt 删除编译缓存,让指令重新执行,否则每次读取这个文件,命令修改正确也是报错

[root@localhost mariadb-10.2.7]# rm -rf CMakeCache.txt


安装完环境准备

[root@localhost mariadb-10.2.7]# chmod +w /usr/local/mysql/

[root@localhost mariadb-10.2.7]# chown -R mysql:mysql /usr/local/mysql/


修改配置文件

[root@localhost mariadb-10.2.7]# cp ./support-files/my-large.cnf /etc/my.cnf

[root@localhost mariadb-10.2.7]# vim /etc/my.cnf

在[mysqld]模块添加下面的几行:

datadir=/data/mysql

basedir = /etc/local/mysql

user=mysql

symbolic-links=0

新增加mysqld_safe块

[mysqld_safe]

log-error=/usr/local/mysql/logs/mysqld.log

pid-file=/usr/local/mysql/mysqld.pid

user=mysql

tmpdir=/tmp


初始化mysql

[root@localhost mariadb-10.2.7]# ./scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/data/mysql/ --defaults-file=/etc/my.cnf


配置MySQL开机自启动、启动服务

[root@localhost mariadb-10.2.7]# cp ./support-files/mysql.server /etc/init.d/mysqld

[root@localhost mariadb-10.2.7]# chmod +x /etc/init.d/mysqld

[root@localhost mariadb-10.2.7]# chkconfig --add mysqld                         # 添加至系统服务

[root@localhost mariadb-10.2.7]# chkconfig mysqld on                              # 设置开机自启动

[root@localhost mariadb-10.2.7]# /etc/init.d/mysqld start 或者 systemctl start mysqld.service 都可以启动mysql服务  


添加MySQL的软链接以适应init脚本

[root@localhost mariadb-10.2.7]# ln -sv /usr/local/mysql/bin/mysql  /usr/sbin/mysql

[root@localhost mariadb-10.2.7]# ln -sv /usr/local/mysql/bin/mysqladmin  /usr/sbin/mysqladmin

[root@localhost mariadb-10.2.7]# ln -sv /usr/local/mysql/bin/mysqldump  /usr/sbin/mysqldump


添加环境变量

[root@localhost mariadb-10.2.7]# vim /etc/profile

export PATH=$PATH:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/mysql/bin


重读环境变量

[root@localhost mariadb-10.2.7]# source /etc/profile


初始化数据库,此方式可以禁用空密码登陆,按照提示输入你需要的Y或者N

[root@localhost mariadb-10.2.7]# mysql_secure_installation                  #默认回车就行,第二个回车输入y,两次输入root密码

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB

      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current

password for the root user.  If you've just installed MariaDB, and

you haven't set the root password yet, the password will be blank,

so you should just press enter here.

Enter current password for root (enter for none):                                     #输入当前root用户密码?默认为空,直接回车

OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB

root user without the proper authorisation.

Set root password? [Y/n] y                                                                         #要设置root密码吗?输入y表示愿意。

New password:

Re-enter new password:

Password updated successfully!

Reloading privilege tables..

 ... Success!

By default, a MariaDB installation has an anonymous user, allowing anyone

to log into MariaDB without having to have a user account created for

them.  This is intended only for testing, and to make the installation

go a bit smoother.  You should remove them before moving into a

production environment.

Remove anonymous users? [Y/n]                                                             #要移除匿名用户吗?输入y表示愿意。

 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This

ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n]                                                          #不想root远程登录吗?输入y表示愿意。

 ... Success!

By default, MariaDB comes with a database named 'test' that anyone can

access.  This is also intended only for testing, and should be removed

before moving into a production environment.

Remove test database and access to it? [Y/n]                                          #要去掉test数据库吗?输入y表示愿意。

 - Dropping test database...

 ... Success!

 - Removing privileges on test database...

 ... Success!

Reloading the privilege tables will ensure that all changes made so far

will take effect immediately.

Reload privilege tables now? [Y/n]                                                           #想要重新加载权限吗?输入y表示愿意。

 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB

installation should now be secure.

Thanks for using MariaDB!

[root@localhost mariadb-10.2.7]# mysql

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

[root@localhost mariadb-10.2.7]# mysql -uroot -p123456

Welcome to the MariaDB monitor.  Commands end with ; or \g.

Your MariaDB connection id is 18

Server version: 10.2.7-MariaDB Source distribution

Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> show databases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| mysql              |

| performance_schema |

+--------------------+

3 rows in set (0.02 sec)

MariaDB [(none)]> exit;

Bye


设置mysql本地方式免密码登陆

# egrep -v "^$|#" /etc/my.cnf  (增加了红色部分

[client]

port= 3306

socket= /tmp/mysql.sock

[mysqld]

port= 3306

socket= /tmp/mysql.sock

skip-external-locking

key_buffer_size = 256M

max_allowed_packet = 1M

table_open_cache = 256

sort_buffer_size = 1M

read_buffer_size = 1M

read_rnd_buffer_size = 4M

myisam_sort_buffer_size = 64M

thread_cache_size = 8

query_cache_size= 16M

thread_concurrency = 8

log-bin=mysql-bin

binlog_format=mixed

server-id= 1

[mysqldump]

quick

max_allowed_packet = 16M

[mysql]

no-auto-rehash

user = root

password = 123456

host = 127.0.0.1

[myisamchk]

key_buffer_size = 128M

sort_buffer_size = 128M

read_buffer = 2M

write_buffer = 2M

[mysqlhotcopy]

interactive-timeout

[root@localhost mariadb-10.2.7]# vim /etc/my.cnf

[root@localhost mariadb-10.2.7]# /etc/init.d/mysqld restart

MariaDB [(none)]> exit;

Bye

[root@localhost mariadb-10.2.7]# mysql

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

[root@localhost mariadb-10.2.7]# mysql                  #执行完上述设置后,重新执行mysql命令就可以直接登录了

Welcome to the MariaDB monitor.  Commands end with ; or \g.

Your MariaDB connection id is 9

Server version: 10.2.7-MariaDB Source distribution

Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]>


2、nginx 安装下载、 配置、编译、安装

编译安装依赖,直接yum安装:(如果执行yum groupinstall 'Development Tools' 会自动安装相关依赖) 

[root@localhost ~]# yum install gcc gcc-c++ make

Nginx的安装: Nginx需要依赖下面3个包     官网地址:http://nginx.org/en/download.html  

1. gzip 模块需要 zlib 库 ( 下载: http://www.zlib.net/  )

2. rewrite 模块需要 pcre 库 ( 下载: http://www.pcre.org/    支持伪静态 )

3. ssl 功能需要 openssl 库 ( 下载: https://www.openssl.org/source/ )


附:

nginx下载安装地址:

[root@localhost ~]# wget -O nginx-1.10.2.tar.gz http://nginx.org/download/nginx-1.10.2.tar.gz


下载完nginx源码包后pcer库,pcer的功能是为nginx提供重写(rewrite),伪静态之类的功能。

[root@localhost ~]# wget -O pcre-8.41.tar.gz ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.41.tar.gz


然后再下载zlib库,zlib的作用是提供gzip压缩功能

[root@localhost ~]# wget -O zlib-1.2.11.tar.gz  http://www.zlib.net/zlib-1.2.11.tar.gz


然后再下载openssl,给nginx提供ssl功能的支持,也就是https

[root@localhost ~]# wget -O openssl-1.1.0f.tar.gz https://www.openssl.org/source/openssl-1.1.0f.tar.gz


安装软件移动到指定目录,并解压缩

[root@localhost ~]# ls

anaconda-ks.cfg  initial-setup-ks.cfg  nginx-1.10.2.tar.gz  openssl-1.1.0f.tar.gz  pcre-8.41.tar.gz  zlib-1.2.11.tar.gz  公共  模板  视频  图片  文档  下载  音乐  桌面

[root@localhost ~]# mv nginx-1.10.2.tar.gz /usr/local/src/

[root@localhost ~]# mv openssl-1.1.0f.tar.gz  /usr/local/src/

[root@localhost ~]# mv pcre-8.41.tar.gz /usr/local/src/

[root@localhost ~]# mv zlib-1.2.11.tar.gz /usr/local/src/

[root@localhost ~]# cd /usr/local/src/

[root@localhost src]# tar -zxvf nginx-1.10.2.tar.gz

[root@localhost src]# tar -zxvf openssl-1.1.0f.tar.gz

[root@localhost src]# tar -zxvf pcre-8.41.tar.gz

[root@localhost src]# tar -zxvf zlib-1.2.11.tar.gz

[root@localhost src]# ls

cmake-3.9.0         mariadb-10.2.7         nginx-1.10.2         openssl-1.1.0f         pcre-8.41         zlib-1.2.11

cmake-3.9.0.tar.gz  mariadb-10.2.7.tar.gz  nginx-1.10.2.tar.gz  openssl-1.1.0f.tar.gz  pcre-8.41.tar.gz  zlib-1.2.11.tar.gz

 

进入nginx解压目录

[root@localhost src]# cd nginx-1.10.2/


#给nginx创建一个专属的用户组和用户

[root@localhost nginx-1.10.2]# groupadd nginx                         #创建一个名为nginx的用户组


#给nginx创建一个主目录,/usr/local/nginx这个目录也是nginx默认安装的目录

[root@localhost nginx-1.10.2]# mkdir /usr/local/nginx       

        

#创建一个名为nginx的用户,-d参数设置它的主目录 -g参数设置所属的用户组,-s设置它的登陆shell,-r说明是创建一个系统用户

[root@localhost nginx-1.10.2]# useradd -d /usr/local/nginx/ -g nginx -s /sbin/nologin -r nginx


开始正式编译安装nginx,进入nginx源码包解压的目录

cd nginx-1.10.2

编译nginx

[root@localhost nginx-1.10.2]# ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx \

> --with-http_ssl_module \

> --with-pcre=/usr/local/src/pcre-8.41 \

> --with-zlib=/usr/local/src/zlib-1.2.11 \

> --with-openssl=/usr/local/src/openssl-1.1.0f

编译参数解释:

#指定运行权限的用户

--user=www

#指定运行的权限用户组

--group=www

#指定安装路径

--prefix=/usr/local/nginx

#支持nginx状态查询

--with-http_stub_status_module

#开启ssl支持

--with-http_ssl_module

#开启GZIP功能

--with-http_gzip_static_module


编译、安装nginx

[root@localhost nginx-1.10.2]# make && make install


输入/usr/local/nginx/sbin/nginx启动下nginx

[root@localhost nginx-1.10.2]# /usr/local/nginx/sbin/nginx


Nginx启动和停止:

[root@localhost nginx-1.10.2]# /usr/local/nginx/sbin/nginx -s reload   #重新载入配置文件

[root@localhost nginx-1.10.2]# /usr/local/nginx/sbin/nginx -s reopen   #重启 Nginx

[root@localhost nginx-1.10.2]# /usr/local/nginx/sbin/nginx -s stop     #停止 Nginx

[root@localhost nginx-1.10.2]# kill -QUIT [Nginx主进程号]               #停止 Nginx【安全停止】

[root@localhost nginx-1.10.2]# kill -TERM [Nginx主进程号]               #停止 Nginx【快速停止】


查看当前编译安装的nginx的版本:

[root@localhost nginx-1.10.2 ]# cd /usr/local/nginx/

[root@localhost nginx]# ./sbin/nginx -v

nginx version: nginx/1.10.2

白俊遥博客

在虚拟机  浏览器里面访问ip,即可看到nginx安装成功!如果在虚拟机外面,访问不了的话,执行以下代码

[root@localhost nginx-1.10.2]# firewall-cmd --permanent --zone=public --add-service=http

success

[root@localhost nginx-1.10.2]# firewall-cmd --permanent --zone=public --add-service=https

success

[root@localhost nginx-1.10.2]# firewall-cmd --reload

success

也可以关闭防火墙

[root@localhost nginx-1.10.2]# firewall-cmd --state                    #查看默认防火墙启动状态

running

[root@localhost nginx-1.10.2]# systemctl stop firewalld.service        #停止firewall

[root@localhost nginx-1.10.2]# systemctl disable firewalld.service     #禁止firewall开机启动


在 centos7 中为nginx的启动、重启、重载配置添加脚本

#nginx直接启动的方法:

/usr/local/nginx/sbin/nginx

但是不是很方便,因此使用下面的脚本来控制nginx的启动关闭重载更加合理一些。

注意:请先执行 /usr/local/nginx/sbin/nginx -s stop  停止Nginx

编辑文件:vim /usr/lib/systemd/system/nginx.service 添加下面的脚本,注意路径 !

[Unit]

Description=nginx - high performance web server

Documentation=http://nginx.org/en/docs/

After=network.target remote-fs.target nss-lookup.target

[Service]

Type=forking

PIDFile=/usr/local/nginx/logs/nginx.pid

ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf

ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

ExecReload=/bin/kill -s HUP $MAINPID

ExecStop=/bin/kill -s QUIT $MAINPID

PrivateTmp=true

[Install]

WantedBy=multi-user.target

systemctl的一些使用方法:

systemctl is-enabled servicename.service #查询服务是否开机启动

systemctl enable xxx.service #开机运行服务

systemctl disable xxx.service #取消开机运行

systemctl start xxx.service #启动服务

systemctl stop xxx.service #停止服务

systemctl restart xxx.service #重启服务

systemctl reload xxx.service #重新加载服务配置文件

systemctl status xxx.service #查询服务运行状态

systemctl --failed #显示启动失败的服务


因此,添加上面脚本后,centos7 中操作nginx的方法有

systemctl is-enabled nginx.service #查询nginx是否开机启动

systemctl enable nginx.service #开机运行nginx

systemctl disable nginx.service #取消开机运行nginx

systemctl start nginx.service #启动nginx

systemctl stop nginx.service #停止nginx

systemctl restart nginx.service #重启nginx

systemctl reload nginx.service #重新加载nginx配置文件

systemctl status nginx.service #查询nginx运行状态

systemctl --failed #显示启动失败的服务


添加完后,查看nginx启动状态

[root@localhost nginx]# systemctl status  nginx.service

● nginx.service - nginx - high performance web server

   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)

   Active: inactive (dead)

     Docs: http://nginx.org/en/docs/

[root@localhost nginx]# systemctl restart nginx.service     #重新启动nginx服务后就又可以访问了

[root@localhost nginx]# systemctl enable nginx.service     #设置开机自动启动

Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.

运行 注:nginx默认使用80端口,若是80端口被占用,修改/usr/local/nginx/conf/nginx.conf

/usr/local/nginx/sbin/nginx

#查看nginx是否正常

[root@localhost nginx-1.12.1]# /usr/local/nginx/sbin/nginx -t

nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok

nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful



3、下载、配置、编译、安装PHP7

首先下载php7(官网地址:http://php.net/downloads.php)

[root@localhost ~]# wget -O php-7.1.7.tar.gz  http://cn2.php.net/get/php-7.1.7.tar.gz/from/this/mirror


移动安装软件包到指定安装目录、并解压缩

[root@localhost ~]# mv php-7.1.7.tar.gz /usr/local/src/

[root@localhost ~]# cd /usr/local/src/

[root@localhost src]# ls

cmake-3.9.0         mariadb-10.2.7         nginx-1.10.2         openssl-1.1.0f         pcre-8.41         php-7.1.7.tar.gz  zlib-1.2.11.tar.gz

cmake-3.9.0.tar.gz  mariadb-10.2.7.tar.gz  nginx-1.10.2.tar.gz  openssl-1.1.0f.tar.gz  pcre-8.41.tar.gz  zlib-1.2.11

[root@localhost src]# tar -zxvf php-7.1.7.tar.gz


安装依赖

[root@localhost src]# yum install libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel


配置

[root@localhost src]# cd php-7.1.7/

[root@localhost php-7.1.7]# ./configure \

--prefix=/usr/local/php7 \

--with-config-file-path=/usr/local/php7/etc \

--enable-fpm \

--with-fpm-user=nginx  \

--with-fpm-group=nginx \

--enable-inline-optimization \

--disable-debug \

--disable-rpath \

--enable-shared  \

--enable-soap \

--with-libxml-dir \

--with-xmlrpc \

--with-openssl \

--with-mcrypt \

--with-mhash \

--with-pcre-regex \

--with-sqlite3 \

--with-zlib \

--enable-bcmath \

--with-iconv \

--with-bz2 \

--enable-calendar \

--with-curl \

--with-cdb \

--enable-dom \

--enable-exif \

--enable-fileinfo \

--enable-filter \

--with-pcre-dir \

--enable-ftp \

--with-gd \

--with-openssl-dir \

--with-jpeg-dir \

--with-png-dir \

--with-zlib-dir  \

--with-freetype-dir \

--enable-gd-native-ttf \

--enable-gd-jis-conv \

--with-gettext \

--with-gmp \

--with-mhash \

--enable-json \

--enable-mbstring \

--enable-mbregex \

--enable-mbregex-backtrack \

--with-libmbfl \

--with-onig \

--enable-pdo \

--with-mysqli=mysqlnd \

--with-pdo-mysql=mysqlnd \

--with-zlib-dir \

--with-pdo-sqlite \

--with-readline \

--enable-session \

--enable-shmop \

--enable-simplexml \

--enable-sockets  \

--enable-sysvmsg \

--enable-sysvsem \

--enable-sysvshm \

--enable-wddx \

--with-libxml-dir \

--with-xsl \

--enable-zip \

--enable-mysqlnd-compression-support \

--with-pear 

configure: error: mcrypt.h not found. Please reinstall libmcrypt.

解决办法:                    http://mcrypt.hellug.gr/lib/

[root@localhost ~]# wget -O libmcrypt-2.5.7.tar.gz  ftp://mcrypt.hellug.gr/pub/crypto/mcrypt/libmcrypt/libmcrypt-2.5.7.tar.gz

[root@localhost ~] # mv libmcrypt-2.5.7.tar.gz  /usr/local/src/

[root@localhost ~] #  cd /usr/local/src  

[root@localhost src]# tar -zxvf libmcrypt-2.5.7.tar.gz

[root@localhost src]# cd libmcrypt-2.5.7/

[root@localhost libmcrypt-2.5.7]# ./configure

[root@localhost libmcrypt-2.5.7]# make && make install


 --with-libxml-dir=/usr/lib64  --with-png-dir=/usr/lib64 --with-jpeg-dir=/usr/lib64

PHP编译时错误:Don’t know how to define struct flock on this system, set –enable-opcache=no

解决方法是:

sudo ln -s /usr/local/mysql/lib/libmysqlclient.so /usr/lib/

sudo ln -s /usr/local/mysql/lib/libmysqlclient.so.20 /usr/lib/

(注意,如果是64位的系统,应该软链接到/usr/lib64/目录下。并且,第二个有可能不是libmysqlclient.so.20,最后两位数有可能是其他,根据mysql的版本而变化)

还有一种可能是/usr/local/lib下的库文件没有加载,可如下操作:

vim /etc/ld.so.conf.d/local.conf     # 编辑库文件(该文件可能不存在,不存在则创建一个新的)

/usr/local/lib                        # 添加该行

/usr/local/lib64                     # 64位系统的除了添加上一行,还需要添加此行

:wq                                     # 保存退出

ldconfig -v                           # 使之生效


编译、测试、安装php

[root@localhost php-7.1.7]# make

[root@localhost php-7.1.7]# make test

[root@localhost php-7.1.7]# make install


添加 PHP 命令到环境变量

[root@localhost php-7.1.7]# vim /etc/profile

在末尾加入

PATH=$PATH:/usr/local/php7/bin

export PATH

要使改动立即生效执行

[root@localhost php-7.1.7]# source /etc/profile

查看环境变量

[root@localhost php-7.1.7]# echo $PATH

/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/php7/bin


查看PHP版本

[root@localhost php-7.1.7]#  /usr/local/php7/bin/php -v

PHP 7.1.7 (cli) (built: Jul 19 2017 15:58:42) ( NTS )

Copyright (c) 1997-2017 The PHP Group

Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies

[root@localhost php-7.1.7]# php -v

PHP 7.1.7 (cli) (built: Jul 19 2017 15:58:42) ( NTS )

Copyright (c) 1997-2017 The PHP Group

Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies


配置php-fpm

[root@localhost php-7.1.7]# cp php.ini-production /etc/php.ini

[root@localhost php-7.1.7]# cp /usr/local/php7/etc/php-fpm.conf.default /usr/local/php7/etc/php-fpm.conf

[root@localhost php-7.1.7]# cp /usr/local/php7/etc/php-fpm.d/www.conf.default /usr/local/php7/etc/php-fpm.d/www.conf

[root@localhost php-7.1.7]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm

[root@localhost php-7.1.7]# chmod +x /etc/init.d/php-fpm


启动php-fpm

[root@localhost php-7.1.7]# /etc/init.d/php-fpm start

Starting php-fpm  done


设置php-fpm开机自动启动

[root@localhost php7]# vim /usr/lib/systemd/system/php-fpm.service

[Unit]

Description=php-fpm

After=network.target

[Service]

Type=forking

ExecStart=/usr/local/php7/sbin/php-fpm

PrivateTmp=true

[Install]

WantedBy=multi-user.target


[root@localhost php7]# /etc/init.d/php-fpm restart

[root@localhost php7]# systemctl enable php-fpm.service

Created symlink from /etc/systemd/system/multi-user.target.wants/php-fpm.service to /usr/lib/systemd/system/php-fpm.service.


配置nginx虚拟机,绑定域名:

[root@localhost nginx]# mkdir vhosts

[root@localhost vhosts]# cd /usr/local/nginx/vhosts

[root@localhost nginx]# vim /usr/local/nginx/vhosts/www.laravel.conf

server {

     listen 80;

     server_name laravel.com www.laravel.com;

     root /usr/local/nginx/html/www.laravel.com;

     #access_log /www/access_ abc.log main;

     location / {

         index index.php index.html index.htm;

     }

    error_page 500 502 503 504 /50x.html;

    location = /50x.html {

        root /usr/share/nginx/html;

    }

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

    location ~ \.php$ {

        fastcgi_pass 127.0.0.1:9000;

        fastcgi_index index.php;

        fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html/www.laravel.com$fastcgi_script_name;

        include fastcgi_params;

    }

    location ~ /\.ht {

        deny all;

    }

}


/usr/local/nginx/html目录新建文件夹 http://www.laravel.com/

[root@localhost html]# cd www.laravel.com/              #进入www.laravel.com目录

[root@localhost www.cuikai.com]# vim index.php      #编辑 index.php文件

<?php

echo date('Y-m-d H:i:s',time());

phpinfo();


查看nginx配置文件路径

[root@localhost conf]# /usr/local/nginx/sbin/nginx -t

nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok

nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful


打开 nginix 配置文件,在相应位置加入 include 把以上2个文件包含进来

[root@localhost conf]# vim /usr/local/nginx/conf/nginx.conf


# 包含所有的虚拟主机的配置文件(http大括号内,server同级)

include /usr/local/nginx/vhosts/*;


重启nginx

[root@localhost conf]# systemctl restart  nginx.service


查看nginx pid

[root@localhost conf]# ps -ef | grep nginx


最后,做一个测试文件,测试即可。。。。

冷暖自知一抹茶ck
请先登录后发表评论
  • 最新评论
  • 总共0条评论