如果你按照我下面提供的步骤一步一步的安装,保证你一定会成功搭建LNMP环境,必且中间不会有任何报错!
1.1准备工作:我们首先需要把LNMP需要用到的软件包都提前安装好,不然的话到时候编译安装的时候就会出一些错误,
安装主机具备编译程序源码的环境,他需要具备C语言、C++语言、Perl语言的编译器,以及各种常见的编译支持函数库程序。
由于软件包比较多,可能安装时间会有点长!
[root@localhost ~]# yum -y install gcc gcc-c++ automake autoconf libtool make openssl openssl-devel mhash-devel libxslt-devel libjpeg libjpeg-devel libpng libpng-devel
freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel
glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel
e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl
openssl-devel mysql-server libaio-devel.x86_64
................................................................
perl-Pod-Simple.noarch 1:3.28-4.el7 perl-Pod-Usage.noarch 0:1.63-3.el7
perl-Scalar-List-Utils.x86_64 0:1.27-248.el7 perl-Socket.x86_64 0:2.010-4.el7
perl-Storable.x86_64 0:2.45-3.el7 perl-Test-Harness.noarch 0:3.28-3.el7
perl-Text-ParseWords.noarch 0:3.29-4.el7 perl-Thread-Queue.noarch 0:3.02-2.el7
perl-Time-HiRes.x86_64 4:1.9725-3.el7 perl-Time-Local.noarch 0:1.2300-2.el7
perl-constant.noarch 0:1.27-2.el7 perl-libs.x86_64 4:5.16.3-294.el7_6
perl-macros.x86_64 4:5.16.3-294.el7_6 perl-parent.noarch 1:0.225-244.el7
perl-podlators.noarch 0:2.5.1-3.el7 perl-threads.x86_64 0:1.87-4.el7
perl-threads-shared.x86_64 0:1.43-6.el7 xz-devel.x86_64 0:5.2.2-1.el7
libselinux-python.x86_64 0:2.5-14.1.el7 libselinux-utils.x86_64 0:2.5-14.1.el7
libsepol.x86_64 0:2.5-10.el7 libss.x86_64 0:1.42.9-13.el7
libstdc++.x86_64 0:4.8.5-36.el7_6.2 nspr.x86_64 0:4.19.0-1.el7_5
nss.x86_64 0:3.36.0-7.1.el7_6 nss-pem.x86_64 0:1.0.3-5.el7_6.1
nss-softokn.x86_64 0:3.36.0-5.el7_5 nss-softokn-freebl.x86_64 0:3.36.0-5.el7_5
nss-sysinit.x86_64 0:3.36.0-7.1.el7_6 nss-tools.x86_64 0:3.36.0-7.1.el7_6
nss-util.x86_64 0:3.36.0-1.1.el7_6 openssl-libs.x86_64 1:1.0.2k-16.el7_6.1
Complete!
安装完成后我们可以看到这个complete就可以了!
1.2这里我们把所有需要的软件源码包文件都统一放在/usr/local/src,这些源码包很方便的在网上可以找到.如果找不到可以联系我我可以把打包好的文件发给你。。
[root@localhost src]# ls
cmake-2.8.11.2.tar.gz libgd-2.1.0.tar.gz zlib-1.2.8.tar.gz
Discuz_X3.2_SC_GBK.zip libmcrypt-2.5.8.tar.gz t1lib-5.1.2.tar.gz
freetype-2.5.3.tar.gz libpng-1.6.12.tar.gz openssl-1.0.1h.tar.gz tiff-4.0.3.tar.gz
jpegsrc.v9a.tar.gz libvpx-v1.3.0.tar.bz2 pcre-8.35.tar.gz yasm-1.2.0.tar.gz
我们这里只安装mysql5.6版本,其他版本大同小异请自行研究。
[root@localhost src]# wget http://mirrors.163.com/mysql/Downloads/MySQL-5.6/mysql-5.6.42-linux-glibc2.12-x86_64.tar.gz
--2019-06-04 21:33:16-- http://mirrors.163.com/mysql/Downloads/MySQL-5.6/mysql-5.6.42-linux-glibc2.12-x86_64.tar.gz
Resolving mirrors.163.com (mirrors.163.com)... 59.111.0.251
Connecting to mirrors.163.com (mirrors.163.com)|59.111.0.251|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 328979165 (314M) [application/octet-stream]
Saving to: ‘mysql-5.6.42-linux-glibc2.12-x86_64.tar.gz.1’
100%[==============================================================================>] 328,979,165 6.50MB/s in 42s
2019-06-04 21:33:58 (7.39 MB/s) - ‘mysql-5.6.42-linux-glibc2.12-x86_64.tar.gz.1’ saved [328979165/328979165]
//查看一下我们刚刚下好的软件包在不在里面
[root@localhost src]# ls
cmake-2.8.11.2.tar.gz libmcrypt-2.5.8.tar.gz nginx-1.6.0.tar.gz tiff-4.0.3.tar.gz
Discuz_X3.2_SC_GBK.zip libpng-1.6.12.tar.gz openssl-1.0.1h.tar.gz yasm-1.2.0.tar.gz
freetype-2.5.3.tar.gz libvpx-v1.3.0.tar.bz2 pcre-8.35.tar.gz zlib-1.2.8.tar.gz
jpegsrc.v9a.tar.gz mysql-5.6.19.tar.gz php-5.5.14.tar.gz
libgd-2.1.0.tar.gz mysql-5.6.42-linux-glibc2.12-x86_64.tar.gz t1lib-5.1.2.tar.gz
[root@localhost src]# tar xzvf mysql-5.6.42-linux-glibc2.12-x86_64.tar.gz
.................................................................
[root@localhost src]# mv mysql-5.6.42-linux-glibc2.12-x86_64 /usr/local/mysql
[root@localhost src]# ls /usr/local/mysql
bin COPYING data docs include lib man mysql-test README scripts sql-bench support-files
[root@localhost src]# useradd mysql -s /sbin/nologin
[root@localhost src]# mkdir -p /usr/local/data/mysql
[root@localhost mysql]# ./scripts/mysql_install_db --user=mysql --datadir=/usr/local/data/mysql
....................................................
The latest information about MySQL is available on the web at
http://www.mysql.com
Support MySQL by buying support/licenses at http://shop.mysql.com
WARNING: Found existing config file ./my.cnf on the system.
Because this file might be in use, it was not replaced,
but was used in bootstrap (unless you used --defaults-file)
and when you later start the server.
The new default config file was created as ./my-new.cnf,
please compare it with your file and take the changes you need.
WARNING: Default config file /etc/my.cnf exists on the system
This file will be read by default by the MySQL server
If you do not want to use this, either remove it, or use the
--defaults-file argument to mysqld_safe when starting the server
//可以验证一下安装是否成功
[root@localhost mysql]# echo $?
0
[root@localhost mysql]# cp support-files/my-default.cnf /etc/my.cnf
cp: overwrite ‘/etc/my.cnf’? y
[root@localhost mysql]# cp support-files/mysql.server /etc/init.d/mysqld
//给启动文件增加相关执行权限
[root@localhost mysql]# chmod 755 /etc/init.d/mysqld
//编辑配置文件,增加下面两行
[root@localhost mysql]# vi /etc/my.cnf
basedir = /usr/local/mysql
datadir = /usr/local/data/mysql
[root@localhost mysql]# vi /etc/init.d/mysqld
basedir=/usr/local/mysql
datadir=/usr/local/data/mysql
[root@localhost mysql]# chkconfig --add mysqld
[root@localhost mysql]# chkconfig mysqld on
[root@localhost mysql]# /etc/init.d/mysqld start
Starting MySQL.Logging to '/usr/local/data/mysql/localhost.localdomain.err'.
. SUCCESS!
[root@localhost mysql]# ps aux |grep mysqld
root 1795 0.0 0.0 113312 1628 pts/0 S 21:54 0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/usr/local/data/mysql --pid-file=/usr/local/data/mysql/localhost.localdomain.pid
mysql 1930 4.7 24.1 1302748 449792 pts/0 Sl 21:54 0:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/usr/local/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=localhost.localdomain.err --pid-file=/usr/local/data/mysql/localhost.localdomain.pid
root 1956 0.0 0.0 112708 980 pts/0 R+ 21:54 0:00 grep --color=auto mysqld
//查看mysqld的端口号是3306
[root@localhost mysql]# netstat -lnp |grep mysqld
tcp6 0 0 :::3306 :::* LISTEN 1930/mysqld
unix 2 [ ACC ] STREAM LISTENING 22355 1930/mysqld /tmp/mysql.sock
这里我们下载的是1.8.0,具体的版本我们可以自行安排,统一下载到相关目录下
[root@localhost src]# wget http://nginx.org/download/nginx-1.8.0.tar.gz
--2019-06-04 21:56:46-- http://nginx.org/download/nginx-1.8.0.tar.gz
Resolving nginx.org (nginx.org)... 62.210.92.35, 95.211.80.227, 2001:1af8:4060:a004:21::e3
Connecting to nginx.org (nginx.org)|62.210.92.35|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 832104 (813K) [application/octet-stream]
Saving to: ‘nginx-1.8.0.tar.gz’
100%[==============================================================================>] 832,104 12.3KB/s in 16s
2019-06-04 21:57:03 (50.5 KB/s) - ‘nginx-1.8.0.tar.gz’ saved [832104/832104]
[root@localhost src]# mv nginx-1.8.0 /usr/local/nginx-1.8.0
[root@localhost src]# cd /usr/local/nginx-1.8.0/
[root@localhost nginx-1.8.0]# ./configure --prefix=/usr/local/nginx
...................................................
Configuration summary
+ using system PCRE library
+ OpenSSL library is not used
+ md5: using system crypto library
+ sha1: using system crypto library
+ using system zlib library
nginx path prefix: "/usr/local/nginx"
nginx binary file: "/usr/local/nginx/sbin/nginx"
nginx configuration prefix: "/usr/local/nginx/conf"
nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
nginx pid file: "/usr/local/nginx/logs/nginx.pid"
nginx error log file: "/usr/local/nginx/logs/error.log"
nginx http access log file: "/usr/local/nginx/logs/access.log"
nginx http client request body temporary files: "client_body_temp"
nginx http proxy temporary files: "proxy_temp"
nginx http fastcgi temporary files: "fastcgi_temp"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp"
[root@localhost nginx-1.8.0]# make && make install
..................................................
cp conf/nginx.conf '/usr/local/nginx/conf/nginx.conf.default'
test -d '/usr/local/nginx/logs' || mkdir -p '/usr/local/nginx/logs'
test -d '/usr/local/nginx/logs' || mkdir -p '/usr/local/nginx/logs'
test -d '/usr/local/nginx/html' || cp -R html '/usr/local/nginx'
test -d '/usr/local/nginx/logs' || mkdir -p '/usr/local/nginx/logs'
make[1]: Leaving directory `/usr/local/nginx-1.8.0'
[root@localhost nginx-1.8.0]# echo $?
0
[root@localhost nginx-1.8.0]# vi /etc/init.d/nginx
#!/bin/bash
# chkconfig: - 30 21
# description: http service.
# Source Function Library
. /etc/init.d/functions
# Nginx Settings
NGINX_SBIN="/usr/local/nginx/sbin/nginx"
NGINX_CONF="/usr/local/nginx/conf/nginx.conf"
NGINX_PID="/usr/local/nginx/logs/nginx.pid"
RETVAL=0
prog="Nginx"
start()
{
echo -n $"Starting $prog: "
mkdir -p /dev/shm/nginx_temp
daemon $NGINX_SBIN -c $NGINX_CONF
RETVAL=$?
echo
return $RETVAL
}
stop()
{
echo -n $"Stopping $prog: "
killproc -p $NGINX_PID $NGINX_SBIN -TERM
rm -rf /dev/shm/nginx_temp
RETVAL=$?
echo
return $RETVAL
}
reload()
{
echo -n $"Reloading $prog: "
killproc -p $NGINX_PID $NGINX_SBIN -HUP
RETVAL=$?
echo
return $RETVAL
}
restart()
{
stop
start
}
configtest()
{
$NGINX_SBIN -c $NGINX_CONF -t
return 0
}
case "$1" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
restart
;;
configtest)
configtest
;;
*)
echo $"Usage: $0 {start|stop|reload|restart|configtest}"
RETVAL=1
esac
exit $RETVAL
[root@localhost nginx-1.8.0]# chmod 755 /etc/init.d/nginx
[root@localhost nginx-1.8.0]# chkconfig --add nginx
[root@localhost nginx-1.8.0]# chkconfig nginx on
[root@localhost nginx-1.8.0]# /etc/init.d/nginx start
Starting nginx (via systemctl): [ OK ]
[root@localhost nginx-1.8.0]# curl -I localhost
HTTP/1.1 200 OK
Server: nginx/1.8.0
Date: Tue, 04 Jun 2019 14:10:18 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 04 Jun 2019 14:03:12 GMT
Connection: keep-alive
ETag: "5cf67a20-264"
Accept-Ranges: bytes
前面两个mysql和nginx感觉都比较简单和顺利吧,php的安装才是重点!使用源码包的方式编译安装PHP语言环境其实并不复杂,难点在于解决PHP的程序包和其他软件的依赖关系。为此需要先安装部署将近十个用于搭建网站页面的软件程序包,然后才能正式安装PHP程序。
yasm源码包是一款常见的开源汇编器,其解压、编译、安装过程中生成的输出信息均已省略:
[root@localhost yasm-1.2.0]# ./configure
[root@localhost yasm-1.2.0]# make && make install
libmcrypt源码包是用于加密算法的扩展库程序,其解压、编译、安装过程中生成的输出信息均已省略:
[root@localhost libmcrypt-2.5.8]# ./configure
[root@localhost libmcrypt-2.5.8]# make && make install
libvpx源码包是用于提供视频编码器的服务程序,其解压、编译、安装过程中生成的输出信息均已省略。由于libvpx源码包的后缀是.tar.bz2,即表示使用bzip2格式进行的压缩,因此正确的解压参数应该是xjvf:
[root@localhost libvpx-v1.3.0]# ./configure --prefix=/usr/local/libvpx --enable-shared --enable-vp9
[root@localhost libvpx-v1.3.0]# make && make install
tiff源码包是用于提供标签图像文件格式的服务程序,其解压、编译、安装过程中生成的输出信息均已省略:
[root@localhost tiff-4.0.3]# ./configure --prefix=/usr/local/tiff --enable-shared
[root@localhost tiff-4.0.3]# make && make install
libpng源码包是用于提供png图片格式支持函数库的服务程序,其解压、编译、安装过程中生成的输出信息均已省略:
[root@localhost libpng-1.6.12]# ./configure --prefix=/usr/local/libpng --enable-shared
[root@localhost libpng-1.6.12]# make && make install
freetype源码包是用于提供字体支持引擎的服务程序,其解压、编译、安装过程中生成的输出信息均已省略:
[root@localhost freetype-2.5.3]# ./configure --prefix=/usr/local/freetype --enable-shared
[root@localhost freetype-2.5.3]# make && make install
jpeg源码包是用于提供jpeg图片格式支持函数库的服务程序,其解压、编译、安装过程中生成的输出信息均已省略:
[root@localhost jpeg-9a]# ./configure --prefix=/usr/local/jpeg --enable-shared
[root@localhost jpeg-9a]# make && make install
libgd源码包是用于提供图形处理的服务程序,其解压、编译、安装过程中生成的输出信息均已省略。在编译libgd源码包时,请记得写入的是jpeg、libpng、freetype、tiff、libvpx等服务程序在系统中的安装路径,即在上面安装过程中使用--prefix参数指定的目录路径:
//我们先增加一个www用户,用来运行web程序
[root@localhost libgd-2.1.0]# useradd www
因为编译参数比较多,可能编译时间会比较长:
//可能具体过程编译内容太多有一些我就直接省略了....不过我们可以用echo $?验证一下是否成功
[root@localhost php-5.6.32]./configure --prefix=/usr/local/php-fpm --with-config-file-path=/usr/local/php-fpm/etc --enable-fpm --with-fpm-user=php-fpm --with-fpm-group=php-fpm --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-pdo-mysql=/usr/local/mysql --with-mysql-sock=/tmp/mysql.sock --with-libxml-dir --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-iconv-dir --with-zlib-dir --with-mcrypt --enable-soap --enable-gd-native-ttf --enable-ftp --enable-mbstring --enable-exif --with-pear --with-curl --with-openssl
+--------------------------------------------------------------------+
| License: |
| This software is subject to the PHP License, available in this |
| distribution in the file LICENSE. By continuing this installation |
| process, you are bound by the terms of this license agreement. |
| If you do not agree with the terms of this license, you must abort |
| the installation process at this point. |
+--------------------------------------------------------------------+
Thank you for using PHP.
config.status: creating php5.spec
config.status: creating main/build-defs.h
config.status: creating scripts/phpize
config.status: creating scripts/man1/phpize.1
config.status: creating scripts/php-config
config.status: creating scripts/man1/php-config.1
config.status: creating sapi/cli/php.1
config.status: creating sapi/fpm/php-fpm.conf
config.status: creating sapi/fpm/init.d.php-fpm
config.status: creating sapi/fpm/php-fpm.service
config.status: creating sapi/fpm/php-fpm.8
config.status: creating sapi/fpm/status.html
config.status: creating sapi/cgi/php-cgi.1
config.status: creating ext/phar/phar.1
config.status: creating ext/phar/phar.phar.1
config.status: creating main/php_config.h
config.status: executing default commands
[root@localhost php-5.6.32]# make && make install
.....................................
[root@localhost php-5.6.32]# echo $?
0
[root@localhost php-5.6.32]# cp php.ini-production /usr/local/php-fpm/etc/php.ini
[root@localhost php-5.6.32]# cp /usr/local/php-fpm/etc/php-fpm.conf.default /usr/local/php-fpm/etc/php-fpm.conf
[root@localhost php-5.6.32]# vi /usr/local/php-fpm/etc/php-fpm.conf
148 user = www
149 group = www
[root@localhost php-5.6.32]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@localhost php-5.6.32]# chmod 755 /etc/init.d/php-fpm
[root@localhost php-5.6.32]# chkconfig --add php-fpm
[root@localhost php-5.6.32]# chkconfig php-fpm on
编辑nginx.conf配置文件,重点配置文件
65 location ~ \.php$ {
66 root html;
67 fastcgi_pass 127.0.0.1:9000;
68 fastcgi_index index.php;
//如果这里没有配置正确到时候访问的时候会出现404错误
69 fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
70 include fastcgi_params;
71 }
1
2 user www www;
3 worker_processes 1;
4
5 #error_log logs/error.log;
6 #error_log logs/error.log notice;
7 #error_log logs/error.log info;
8
9 #pid logs/nginx.pid;
10
11
………………省略部分输出信息………………
40
41 #access_log logs/host.access.log main;
42
43 location / {
44 root html;
45 index index.html index.htm index.php;
46 }
47
………………省略部分输出信息………………
62
63 #pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
64
65 location ~ \.php$ {
66 root html;
67 fastcgi_pass 127.0.0.1:9000;
68 fastcgi_index index.php;
69 fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
70 include fastcgi_params;
71 }
72
编辑php-fpm.conf配置文件
148 user = www
149 group = www
1 ;;;;;;;;;;;;;;;;;;;;;
2 ; FPM Configuration ;
3 ;;;;;;;;;;;;;;;;;;;;;
4
5 ; All relative paths in this configuration file are relative to PHP's instal l
6 ; prefix (/usr/local/php). This prefix can be dynamically changed by using t he
7 ; '-p' argument from the command line.
8
9 ; Include one or more files. If glob(3) exists, it is used to include a bunc h of
10 ; files from a glob(3) pattern. This directive can be used everywhere in the
11 ; file.
12 ; Relative path can also be used. They will be prefixed by:
13 ; - the global prefix if it's been set (-p argument)
14 ; - /usr/local/php otherwise
15 ;include=etc/fpm.d/*.conf
16
17 ;;;;;;;;;;;;;;;;;;
18 ; Global Options ;
19 ;;;;;;;;;;;;;;;;;;
20
21 [global]
22 ; Pid file
23 ; Note: the default prefix is /usr/local/php/var
24 ; Default Value: none
25 pid = run/php-fpm.pid
26
………………省略部分输出信息………………
145 ; Unix user/group of processes
146 ; Note: The user is mandatory. If the group is not set, the default user's g roup
147 ; will be used.
148 user = www
149 group = www
150
………………省略部分输出信息………………
[root@localhost php-5.6.32]# /etc/init.d/php-fpm start
Starting php-fpm done
[root@localhost php-5.6.32]# ps aux |grep php
root 58770 0.0 0.2 229472 4800 ? Ss 22:56 0:00 php-fpm: master process (/usr/local/php-fpm/etc/php-fpm.conf)
www 58771 0.0 0.2 229472 4712 ? S 22:56 0:00 php-fpm: pool www
www 58772 0.0 0.2 229472 4712 ? S 22:56 0:00 php-fpm: pool www
root 58774 0.0 0.0 112708 976 pts/0 R+ 22:56 0:00 grep --color=auto php
编辑一个1.php文件用来测试我们的nginx是否对php解析成功
[root@localhost php-5.6.32]# vi /usr/local/nginx/html/1.php
[root@localhost php-5.6.32]# cat /usr/local/nginx/html/1.php
如果出现hello word说明我们的环境搭建成功!
[root@localhost php-5.6.32]# curl localhost/1.php
hello word!