linux下安装nginx+php+mysql环境 详细教程

话不多说上代码

linux环境:centos 7.0 64位

nginx:nginx-1.8.0.tar.gz

php: php-7.1.1.tar.gz

mysql: mysql-5.6.21.tar.gz

libxml2:libxml2-2.9.1.tar.gz

openssl:openssl-1.0.1e.tar.gz

zlib:zlib-1.2.3.tar.gz

pcre:pcre-8.36.tar.gz

这里所需的压缩包 我已经下载好,并且亲测可用

链接:https://pan.baidu.com/s/1Ge1ucbld37zXy_nRgFTU1Q
提取码:w3by

一、准备工作

1、创建安装目录

这里创建 /www/web 用于存入程序代码;   /www/source 环境的安装目录;   /www/lnmp 用于存放安装软件

创建安装目录命令:

mkdir -p /www/{lnmp,source,web}

2、将上面下载好的软件用ftp(或其它工具)上传到 /www/lnmp,并进行解压:

[root@localhost lnmp]# cd /www/lnmp/
[root@localhost lnmp]# find ./*.tar.gz -exec tar zxvf {} \;

3、安装工具

这里用yum安装一下在编译过程中所需要的编译工具和小程序,如:gcc、gd库、cmake等等。这么多小软件,我们不需要编译安装,因为这些软件安装后,以后并不会修改操作,只是一个工具而已。

yum install -y gcc gcc-c++ make sudo autoconf libtool-ltdl-devel gd-devel \
        freetype-devel libxml2-devel libjpeg-devel libpng-devel \
        openssl-devel curl-devel patch libmcrypt-devel \
        libmhash-devel ncurses-devel bzip2 \
        libcap-devel ntp sysklogd diffutils sendmail iptables unzip cmake

注意:这里可能会出现以下错误

Another app is currently holding the yum lock; waiting for it to exit...
  The other application is: yum
    Memory :  71 M RSS (370 MB VSZ)
    Started: Sat Feb 11 18:45:08 2017 - 00:34 ago
    State  : Running, pid: 3033

这是因为yum正在运行着,我们需要停止yum,停止yum的命令如下:

[root@localhost ~]# kill /var/run/yum.pid

停止yum之后,再进行上面的yum安装。

4、创建用户

我们需要创建2个用户,一个用来启动mysql,一个用来启动nginx

[root@localhost ~]# groupadd mysql
[root@localhost ~]# useradd -r mysql -g mysql
[root@localhost ~]# groupadd www
[root@localhost ~]# useradd -r www -g www

这里我们成功创建了两个用户组 mysql 和 www ,也创建了两个用户 mysql 和 www。现在我们的准备工作就已经做好了,下面就是lnmp环境的安装了。

二、安装nginx

安装nginx前,我们需要安装3个依赖包:

pcre:在使用 nginx 的 rewrite 模块的时候,需要有pcre库的支持

openssl:在使用ssl功能时,需要有 openssl库的支持

zlib:在使用gzip模块时,需要有zlib库的支持。

而这三个模块都是我们常用的,所以这3个依赖包还是要安装的。

1、安装 pcre

[root@localhost lnmp]# cd /www/lnmp/pcre-8.36
[root@localhost pcre-8.36]# ./configure
[root@localhost pcre-8.36]# make
[root@localhost pcre-8.36]# make install

注: 这里给刚用linux的朋友说一下,在上面的 ./configure 之后是按一下“回车”,然后等待配置,配置之后,再输入 make,然后“回车”,等待编译,编译好后,输入 make install 就可以了。编译过程出现的代码太多,我不好全复制下来。哈哈!!!

2、安装 openssl

[root@localhost pcre-8.36]# cd /www/lnmp/openssl-1.0.1e
[root@localhost openssl-1.0.1e]# ./config
[root@localhost openssl-1.0.1e]# make
[root@localhost openssl-1.0.1e]# make install

3、安装 zlib

[root@localhost openssl-1.0.1e]# cd /www/lnmp/zlib-1.2.3
[root@localhost zlib-1.2.3]# CFLAGS="-O3 -fPIC" ./configure
[root@localhost zlib-1.2.3]# make && make install

4、安装 nginx

安 装nginx的时候,参数可能会多一些。但是,我建议最简化安装。nginx有很多模块,如果哪个模块用不到,尽量不要安装,我们进入 nginx 源码目录可以使用 ./configure --help 查看有哪些编译参数。参数太多,这里不一一介绍,如果您想了解可以自行查看help或百度。不过,常用的有以下几个:

--prefix=PATH      要安装到的目录

--sbin-path=PATH   指定nginx二进制文件的路径,没指定的话这个路径依赖 --prefix 选项

--conf-path=PATH   如果在命令行未指定配置文件,那么将会通过 --prefix 指定的路径去查找配置文件

--error-log-path=PATH  错误文件路径,nginx写入错误日志文件地址

--pid-path=<path>   nginx master进程pid写入的文件位置,通常在var/run下 

--user=<user>       worker进程运行的用户

--group=<group>     worker进程运行的组

--with-http_ssl_module 开启 ssl 模块

--with-zlib=DIR 设置 zlib 的源码目录

--with-openssl=DIR  设置 openssl 的源码目录

--with-pcre=DIR设置 pcre 的源码目录

了解完编译参数之后,我们进行编译安装

[root@localhost zlib-1.2.3]# cd /www/lnmp/nginx-1.8.0
[root@localhost nginx-1.8.0]# ./configure --help // 这是查看帮助的命令
[root@localhost nginx-1.8.0]# ./configure --user=www --group=www --prefix=/www/source/nginx --with-pcre=/www/lnmp/pcre-8.36 --with-zlib=/www/lnmp/zlib-1.2.3 --with-openssl=/www/lnmp/openssl-1.0.1e
[root@localhost nginx-1.8.0]# make && make install

安装好之后,我们可以用 ls 查看 /www/source,发现有一个 nginx,说明已经安装成功了。

[root@localhost nginx-1.8.0]# ls /www/source/nginx
[root@localhost nginx-1.8.0]#

安装成功之后,还需要配置,配置先不讲解,安后好php后,一起来讲解配置。

三、安装mysql

下面就开始安装 mysql 了,不过,我个人感觉 mysql 是最好安装的。因为编译参数不需要太多(实际上是有很多编译参数的),我们可以在以后的配置文件中手动修改。如果你想了解的话,可以查看http://laowafang.blog.51cto.com/251518/1294964/,感觉写的还是很详细的。

1、安装过程

安装mysql还是很简单的,mysql 5.5 以后要用 cmake 来编译,不过,在开始准备工作时,我们已经安装了 cmake,所以放心使用下面的命令安装吧,安装 mysql 的过程有点长,要有点心里准备。

[root@localhost nginx-1.8.0]# cd /www/lnmp/mysql-5.6.21
[root@localhost mysql-5.6.21]# cmake -DCMAKE_INSTALL_PREFIX=/www/source/mysql
[root@localhost mysql-5.6.21]# make
[root@localhost mysql-5.6.21]# make install

2、配置 mysql

安装好后并不能直接使用,我们要配置一下,才能启动,进入安装好的目录。

[root@localhost mysql-5.6.21]# cd /www/source/mysql/
[root@localhost mysql]# rm -f /etc/my.cnf // 删除系统自带的mysql配置文件
[root@localhost mysql]# chown -R mysql:mysql ./
[root@localhost mysql]# ./scripts/mysql_install_db --user=mysql   // 安装数据库
[root@localhost mysql]# chown -R root:root ./*
[root@localhost mysql]# chown -R mysql:mysql ./data/

我们将 ./support-files/mysql.server 复制到 /etc/init.d/ 下

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

修改 linux 的环境变量,在/etc/profile文件的最后加入一句 

export PATH="/www/source/mysql/bin:$PATH"

[root@localhost mysql]# vim /etc/profile
[root@localhost mysql]# source /etc/profile  // 这一句是让配置立即生效
[root@localhost mysql]# service mysqld start  // 启动 mysql
Starting MySQL..                                           [确定]

下面就可以直接操作数据库了,刚安装好 mysql root 是没有密码的,(mysql 5.7刚安装好是有密码的) 如:

四、安装php

安装php之前,也要安装一个依赖包 libxml2

1、安装 libxml2

[root@localhost mysql]# cd /www/lnmp/libxml2-2.9.1
[root@localhost libxml2-2.9.1]# ./configure --with-python=no
[root@localhost libxml2-2.9.1]# make && make install

特别注意:如果出现这个错误,可以用下面这个方法 ,其它软件,如果在安装的时候,也是同理

/usr/bin/ld: /usr/local/lib/libz.a(crc32.o): relocation R_X86_64_32 against `.ro  
data' can not be used when making a shared object; recompile with -fPIC
/usr/local/lib/libz.a: could not read symbols: Bad value
collect2: ld returned 1 exit status
make[2]: *** [libxml2.la] 错误 1
make[2]: Leaving directory `/lnmp/libxml2-2.9.1'
make[1]: *** [all-recursive] 错误 1
make[1]: Leaving directory `/lnmp/libxml2-2.9.1'
make: *** [all] 错误 2

解决方法:

./configure --with-python=no --enable-shared=no --with-pic=PIC

2、安装 php7.1

php的编译参数也是很多的,这里了不能一一介绍,可以使用 ./configure --help 查看,这里使用的参数如下:

--prefix 安装到的目录

--enable-fpm 开始 fpm 模式,nginx 下必需开启

--enable-fpm-user  fpm 的启动账户

--enable-fpm-group fpm 的启动账户组

--with-openssl开启 openssl

--with-libxml-dir 开启 libxml

--with-zlib     开启 zlib

--enable-mbstring开启 mbstring

--with-mysqli=mysqlnd 开启 mysqli

--with-pdo-mysql 开启 pdo mysql

--with-gd 开启gd库

--enable-sockets 开启 sockets

--with-curl 开启 curl

--enable-maintainer-zts 开启 maintainer zts,以后安装多线程的话,这个必须开启

[root@localhost libxml2-2.9.1]# cd /www/lnmp/php-7.1.1
[root@localhost php-7.1.1]# ./configure --prefix=/www/source/php --enable-fpm --with-fpm-user=www --with-fpm-group=www --with-openssl --with-libxml-dir --with-zlib --enable-mbstring --with-mysqli=mysqlnd --enable-mysqlnd --with-pdo-mysql=/www/source/mysql/ --with-gd --with-jpeg-dir --with-png-dir --with-zlib-dir --with-freetype-dir --enable-sockets --with-curl --enable-maintainer-zts
[root@localhost php-7.1.1]# make
[root@localhost php-7.1.1]# make test // 测试完之后,输入 n
[root@localhost php-7.1.1]# make install

到目前为止,所有的软件已经安装好了,下面来讲解一下,环境配置

五、环境配置

1、php 配置

[root@localhost php-7.1.1]# cd /www/source/php/
[root@localhost php]# cp etc/php-fpm.conf.default etc/php-fpm.conf
[root@localhost php]# cp etc/php-fpm.d/www.conf.default etc/php-fpm.d/www.conf
[root@localhost php]# cp /www/lnmp/php-7.1.1/php.ini-production lib/php.ini

2、启动 php-fpm

[root@localhost php]# /www/source/php/sbin/php-fpm

3、nginx 配置

[root@localhost php]# cd /www/source/nginx/conf/
[root@localhost conf]# vim nginx.conf

将 nginx.conf 中的 server 保存成如下

server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  logs/host.access.log  main;

    root    /www/web;
    index   index.html index.php;

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }

    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;

        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        fastcgi_param  SCRIPT_FILENAME  /www/web$fastcgi_script_name;
        fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;

        include        fastcgi_params;
    }
}

4、启动 nginx

[root@localhost conf]# /www/source/nginx/sbin/nginx

六、测试环境

在 /www/web/ 目录下创建一个 index.php,内容如下:

<?php
phpinfo();

修改 index.php 所属组

[root@localhost php]# chown -R www:www /www/web/

打开浏览器访问 localhost,能看到如下页面,说明环境已经搭建成功了。

这篇博文不是小编原创,但经小编亲测,真是可用,并附上压缩包,欢迎大家参考。

猜你喜欢

转载自www.cnblogs.com/phpxj/p/11587381.html