在centOS7下安装Nginx PHP7 mysql5.6

编辑于 2017-04-07


systemctl start php-fpm.service1.准备工作


首先确保你的Linux可以联网,否则解决依赖可以让你砸电脑

软件所需要的所有依赖,别管有用没用,装了再说

yum install -y gcc gcc-c++ cmake openssl openssl-devel pcre pcre-devel libaio libaio-devel bison bison-devel ncurses ncurses-devel openldap openldap-devel bzip2 bzip2-devel bzip2-libs libxml2 libxml2-devel curl curl-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libtool libtool-ltdl libtool-ltdl-devel libevent libevent-devel ImageMagick ImageMagick-c++ ImageMagick-c++-devel unixODBC unixODBC-devel tsocks screen lrzsz ntpdate make wget sqlite sqlite-devel subversion intltool openssh-clients readline-devel gd-devel wget autoconf glibc glibc-devel e2fsprogs e2fsprogs-devel krb5-devel   nss_ldap  libXpm* git

成功之后你就成功了一半了,然后去官网下载nginx,php,mysql

nginx

PHP

mysql

下载完成后把他们用xftp传到linux上,我是在自己家目录新建一个package文件夹来放源码包

2.安装nginx


解压    

tar zxvf nginx-1.10.1.tar.gz

进入目录    

cd nginx-1.10.1

创建nginx使用的www用户和www组

groupadd  www 
useradd -g  www www

配置

./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module

--user=www : 表示该软件所属用户www

--group=www:表示该软件所属组www

编译&安装

make && make install

完了之后,检查是否安装成功

cd  /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

说明成功了

开机启动

创建nginx启动命令脚本

vi /lib/systemd/system/nginx.service

插入以下内容, 注意修改PATH和NAME字段, 匹配自己的安装路径

[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target

内容解释

[Unit]:服务的说明

Description:描述服务

After:描述服务类别

[Service]服务运行参数的设置

Type=forking是后台运行的形式

ExecStart为服务的具体运行命令

ExecReload为重启命令

ExecStop为停止命令

PrivateTmp=True表示给服务分配独立的临时空间

注意:[Service]的启动、重启、停止命令全部要求使用绝对路径

[Install]运行级别下服务安装的相关设置,可设置为多用户,即系统运行级别为3

对nginx服务执行停止/启动/重新读取配置文件操作

启动nginx服务
systemctl start nginx.service
停止nginx服务
systemctl stop nginx.service
重启nginx服务
systemctl restart nginx.service
重新读取nginx配置(这个最常用, 不用停止nginx服务就能使修改的配置生效)
systemctl reload nginx.service
查看状态
systemctl status nginx.service

防火墙开启80端口

firewall-cmd --permanent --zone=public --add-port=80/tcp

配置Nginx支持php

vi /usr/local/nginx/conf/nginx.conf

首行user去掉注释,修改Nginx运行组为www www;必须与/usr/local/php/etc/php-fpm.d/www.conf中的user,group配置相同,否则php运行出错

user  www  www

添加index.php

index  index.php  index.html index.htm 
location ~ \.php$ {    
  #root          html;#此处和server下面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; 
}

重启nginx

systemctl restart nginx.service

nginx下的thinkphp的URL重写规则

location /project/ {    #project是你项目文件夹的名称
	index  index.php;
		if (!-e $request_filename) {
		rewrite  ^/project/(.*)$  /project/index.php/$1  last;
		break;
	}
}

location ~ .+\.php($|/) {
	set $script    $uri;
	set $path_info  "/";
	if ($uri ~ "^(.+\.php)(/.+)") {
		set $script     $1;
		set $path_info  $2;
	}

	fastcgi_pass 127.0.0.1:9000;
	fastcgi_index  index.php?IF_REWRITE=1;
	include /usr/local/nginx/conf/fastcgi_params;
	fastcgi_param PATH_INFO $path_info;
	fastcgi_param SCRIPT_FILENAME  $document_root/$script;
	fastcgi_param SCRIPT_NAME $script;
}


3.安装PHP


准备工作,安装libiconv

加强系统对支持字符编码转换的功能

cd /usr/local/src
wget http://ftp.gnu.org/gnu/libiconv/libiconv-1.14.tar.gz
tar zvxf libiconv-1.14.tar.gz
cd libiconv-1.14
./configure 
cd srclib
sed -i -e '/gets is a security/d' ./stdio.in.h
cd ..
make && make install
ln -sf /usr/local/lib/libiconv.so.2 /usr/lib64/
ldconfig

安装libmcrypt (包含libltdl)

加密算法库,PHP扩展mcrypt功能对此库有依赖关系
cd /usr/local/src
wget http://sourceforge.net/projects/mcrypt/files/Libmcrypt/2.5.8/libmcrypt-2.5.8.tar.gz
tar zvxf libmcrypt-2.5.8.tar.gz
cd libmcrypt-2.5.8
./configure && make && make install
cd libltdl/
./configure --enable-ltdl-install
make && make install
ln -sf /usr/local/lib/libmcrypt.* /usr/lib64/
ln -sf /usr/local/bin/libmcrypt-config /usr/lib64/
ldconfig

安装mcrypt (依赖libmcrypt和mhash)

cd /usr/local/src/
wget http://sourceforge.net/projects/mcrypt/files/MCrypt/2.6.8/mcrypt-2.6.8.tar.gz
tar zvxf mcrypt-2.6.8.tar.gz
cd mcrypt-2.6.8
./configure && make && make install

安装re2c

cd /usr/local/src/
wget http://sourceforge.net/projects/re2c/files/0.16/re2c-0.16.tar.gz
tar zvxf re2c-0.16.tar.gz
cd re2c-0.16
./configure && make && make install

创建ldap软连接

ln -s /usr/lib64/libldap* /usr/lib
ln -s /usr/lib64/liblber* /usr/lib
ldconfig

解压缩php

tar zxvf php-7.1.2.tar.gz
cd php-7.1.2

配置注意(注意安装的路径和配置文件php.ini存放的路径)

./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-zlib --with-mysqli --with-pdo-mysql --enable-mbstring --with-gd --with-png-dir=/usr/lib64 --with-jpeg-dir=/usr/lib64 --with-freetype-dir=/usr/lib64 --enable-libxml --enable-xml --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --enable-opcache --enable-mbregex --enable-fpm --enable-ftp --enable-gd-native-ttf --with-openssl --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --without-pear --with-gettext --enable-session --with-curl --enable-ctype

编译,安装

make ZEND_EXTRA_LIBS='-liconv' && make install

添加PHP配置文件

cp php.ini-development /usr/local/php/etc/php.ini

修改php配置文件,支持ZendOpcache

vim /usr/local/php/etc/php.ini

找到extension_dir 修改为:

extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20160303/"

加载扩展

zend_extension="opcache.so"

配置opcache参考php手册,在php.ini里去掉注释填上你认为合适的值即可

:wq    #保存退出

生成php-fpm配置文件

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

配置www.conf

vim /usr/local/php/etc/php-fpm.d/www.conf

修改内容,并且让其它生效.[user和group根据实际情况修改]

user = nobody
group = nobody
pm.max_children = 35
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35
:wq     #保存退出

设置开机自启动

在系统服务目录里创建php-fpm.service文件

vim /etc/systemd/system/php-fpm.service
[Unit]
Description=php-fpm
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/php/sbin/php-fpm
PrivateTmp=true
[Install]
WantedBy=multi-user.target

启动php-fpm

systemctl start php-fpm.service

开机自启php-fpm

systemctl enable php-fpm.service

查看php-fpm状态

systemctl status php-fpm.service


4.安装MySQL

tar zxvf mysql-5.6.31.tar.gz
cd mysql-5.6.31
cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/usr/local/mysql/data

下面这步请耐心等待

make && make install

配置MySQL

cd /usr/local/mysql

创建用户和组管理mysql

groupadd mysql
useradd -r -g mysql mysql

修改所属者和组

chown -R mysql .
chgrp -R mysql .

删除可能的旧的/etc/my.cnf

rm -f /etc/my.cnf
./scripts/mysql_install_db --user=mysql
chown -R root .
chown -R mysql data
cp support-files/my-default.cnf /etc/my.cnf

开机自启

vim /etc/systemd/system/mysql.service

添加如下信息

[Unit]

Description=MySQL Server

Documentation=man:mysqld(8)

Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html

After=network.target

After=syslog.target

[Install]

WantedBy=multi-user.target

[Service]

User=mysql

Group=mysql

ExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf

LimitNOFILE = 5000

#Restart=on-failure

#RestartPreventExitStatus=1

#PrivateTmp=false

其中最重要的是ExecStart  这里填你安装mysql的路径到bin/mysqld  后面的--defaults-file是mysql配置文件位置

启动mysql

systemclt start mysql.service

查看状态

systemctl status mysql.service

开机自启动

systemctl enable mysql.service

客户端登录

/usr/local/mysql/bin/mysql

初始无密码,登陆后可以重置root密码

use mysql;
update mysql.user set password=password('root') where user='root';

刷新权限

FLUSH PRIVILEGES;

如果到这里都没问题的话,恭喜你,你可以ctrl + c 退出mysql了,你已经成功安装Web环境了!