LNMP之源码安装搭建

1. LNMP介绍

和LAMP唯一不同的是,LNMP中的N指的是Nginx(类似Apache的一种Web服务软件)。目前这种环境的应用也非常多。Nginx设计的初衷是提供一种快速、高效、多并发的Web服务软件。在静态页面的处理上,Nginx较Apache更胜一筹;但在动态页面的处理上,Nginx并不比Apache有优势。但是目前还是有很多爱好者对Nginx比较热衷。随着Nginx技术的逐渐成熟,它在Web服务软件领域的地位也会越来越高。


2. 安装MySQL

LNMP中MySQL的安装步骤和LAMP一样,这里简单地叙述一下过程,在这里以MySQL5.6版本为例,如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# cd /usr/local/src/ #<==建议把所有的软件包都放在这个目录下面,方便管理
# wget http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz #<==下载MySQL安装包
# tar zxf mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz #<==解压
# [ -d /usr/local/mysql ] && mv /usr/local/mysql /usr/local/mysql_old
# mv mysql-5.6.35-linux-glibc2.5-x86_64 /usr/local/mysql #<==挪动位置
# useradd -s /sbin/nologin mysql #<==建立mysql用户,因为启动MySQL需要该用户
# cd /usr/local/mysql #<==进入mysql目录
# mkdir -p /data/mysql #<==创建datadir,数据库文件会放到这里面
# chown -R mysql:mysql /data/mysql #<==更改文件属主和属组为mysql,不更改后续操作会出问题
# ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql
# echo $?
0
# cp support-files/my-default.cnf /etc/my.cnf
cp:是否覆盖"/etc/my.cnf"? y
# cp support-files/mysql.server /etc/init.d/mysqld
# chmod 755 /etc/init.d/mysqld
# vim /etc/init.d/mysqld

需要修改的地方有datadir=/data/mysql(即前面步骤中初始化数据库时定义的目录)。把启动脚本加入到系统服务向,设定开机启动并启动MySQL,如下所示:

1
2
3
4
5
# chkconfig --add mysqld #<==把mysqld服务加入到系统服务列表中
# chkconfig mysqld on #<==使其开机启动
# service mysqld start #<==启动服务
Starting MySQL.Logging to '/data/mysql/localhost.localdomain.err'.
.. SUCCESS!

查看MySQL是否启动的命令如下:

1
2
3
4
# ps aux | grep mysql | grep -v grep
#<==显示结果应该大于2行
# netstat -lnp | grep 3306 #<==看看有没有监听3306端口
tcp 0 0 :::3306 :::* LISTEN 1904/mysqld


3. 安装PHP

在安装PHP之前先要声明一下,针对Nginx的PHP安装和LAMP中PHP的安装是有区别的。因为Nginx中的PHP是以fastcgi的方式结合Nginx的,可以理解为Nginx代理了PHP的fastcgi,而httpd是把PHP作为自己的模块来调用的。

PHP的官方下载地址为:(http://www.php.net/downloads.php)[http://www.php.net/downloads.php]。这在里建议使用PHP5.6版本。

3.1. 下载PHP源码包

下载PHP5.6版本的源码包,命令如下:

1
2
[root@localhost mysql]# cd /usr/local/src/
[root@localhost src]# wget http://cn2.php.net/distributions/php-5.6.30.tar.gz

3.2. 解压源码包,创建账号

解压下载的PHP5.6版本的源码包,并创建相应的用户,命令如下:

1
2
[root@localhost src]# tar zxf php-5.6.30.tar.gz
[root@localhost src]# useradd -s /sbin/nologin php-fpm

创建的php-fpm账号是用来运行php-fpm服务的。在LNMP环境中,PHP以一个服务php-fpm的形式出现,独立存在于Linux系统中,方便管理。

3.3. 配置编译选项

配置编译选项,命令如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
[root@localhost src]# cd php-5.6.30
[root@localhost src]# ./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-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-bz2 \
--with-mcrypt \
--with-curl \
--with-openssl \
--with-pear \
--enable-soap \
--enable-gd-native-ttf \
--enable-ftp \
--enable-mbstring \
--enable-exif \
--disable-ipv6
# echo $?
0

说明:

  • 编译参数和LAMP中的PHP编译参数不一样,多了--enable-fpm,如果不加该参数,则不会有php-fpm执行文件生成,更不能启动php-fpm服务。还多了一些和fpm相关的配置参数。
  • 在配置过程中,如果出现问题,参考LAMP学习笔记中所记录的问题和解决办法。但是对于一台最小化安装的新机器来说要安装这些:yum install -y libxml2-devel openssl openssl-devel bzip2 bzip2-devel libpng libpng-devel freetype freetype-devel libmcrypt-devel libjpeg-devel libcurl-devel
  • 其实只是比在LAMP中PHP的安装中的问题多了下面的问题,参考下面的解决办法:
    • 问题:configure: error: Please reinstall the libcurl distribution - easy.h should be in <curl-dir>/include/curl/
    • 解决办法:yum install -y libcurl-devel

3.4. 编译PHP

成功的配置编译参数后,就需要进行编译了,命令如下:

1
2
3
4
[root@localhost php-5.6.30]# make
...
[root@localhost php-5.6.30]# echo $?
0

在这一步,可能也会遇到问题。比如下面的几个问题:

  • 问题1:

    1
    2
    3
    4
    /usr/bin/ld: TSRM/.libs/TSRM.o: undefined reference to symbol 'pthread_sigmask@@GLIBC_2.2.5'
    /usr/lib64/libpthread.so.0: error adding symbols: DS0 missing from command line
    collect2: error: ld returned 1 exit status
    make: *** [sapi/cli/php] 错误1
  • 解决方法如下:

    1
    2
    [root@localhost php-5.6.30]# vim Makefile
    #<==在大概102行,-lcrypt后面加 -lpthread
  • 问题2:

    • 修复完上面的问题后继续make,又遇到错误:
      1
      2
      clloect2: error: ld returned 1 exit status
      make: *** [sapi/cli/php] 错误1
  • 解决方法如下:

    1
    [root@localhost php-5.6.30]# make clean && make

3.5. 安装PHP

安装PHP,命令如下:

1
2
3
4
[root@localhost php-5.6.30]# make install
...
[root@localhost php-5.6.30]# echo $?
0

以上的每一个步骤,如果没有完全执行正确,其下一步是无法进行的。判断执行是否正确的方法为,echo $?的结果是否为0

3.6. 修改配置文件

修改配置文件,命令如下:

1
2
[root@localhost php-5.6.30]# cp php.ini-production /usr/local/php-fpm/etc/php.ini
[root@localhost php-5.6.30]# vim /usr/local/php-fpm/etc/php-fpm.conf

把以下内容写入到该文件中:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[global]
pid = /usr/local/php-fpm/var/run/php-fpm.pid
error_log = /usr/local/php-fpm/var/log/php-fpm.log
[www]
listen = /tmp/php-fcgi.sock
listen.mode = 666
user = php-fpm
group = php-fpm
pm = dynamic
pm.max_children = 50
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.max_requests = 500
rlimit_files = 1024

保存配置文件后,检验配置是否正确的方法如下:

1
2
[root@localhost php-5.6.30]# /usr/local/php-fpm/sbin/php-fpm -t
[06-Mar-2018 20:54:34] NOTICE: configuration file /usr/local/php-fpm/etc/php-fpm.conf test is successful

如果显示test is successful,则说明配置没有问题,否则就要根据提示检查配置文件。


3.7. 启动php-fpm

启动php-fpm,命令如下:

1
2
3
4
[root@localhost php-5.6.30]# cp /usr/local/src/php-5.6.30/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@localhost php-5.6.30]# chmod 755 /etc/init.d/php-fpm
[root@localhost php-5.6.30]# service php-fpm start
Starting php-fpm done

设置php-fpm开机启动的命令:chkconfig php-fpm on

检测php-fpm是否启动的命令:pa sux | grep php-fpm | grep -v grep。执行这条命令后,可以看到启动了很多个进程。(大概二十多个)


4. 安装Nginx

从Nginx的官方网站(http://nginx.org)可以看到Nginx的更新速度很快,这说明目前使用Nginx的用户越来越多了。但是不建议安装最新版本的Nginx,因为新版本难免会有一些bug或者漏洞。在本实验中使用stable版本1.10作为安装演示。

4.1. 下载和解压Nginx

下载和解压Nginx,命令如下:

1
2
3
[root@localhost ~]# cd /usr/local/src/
[root@localhost src]# wget http://nginx.org/download/nginx-1.10.3.tar.gz
[root@localhost src]# tar zxvf nginx-1.10.3.tar.gz

4.2. 配置编译选项

配置编译选项,命令如下:

1
2
[root@localhost src]# cd nginx-1.10.3
[root@localhost nginx-1.10.3]# ./configure --prefix=/usr/local/nginx

遇到了一个错误:

1
2
3
4
./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.

解决方法:

1
[root@localhost nginx-1.10.3]# yum install -y pcre-devel

再重新配置编译选项

1
2
3
[root@localhost nginx-1.10.3]# yum install -y pcre-devel
[root@localhost nginx-1.10.3]# echo $?
0

echo $?检查上一步骤是否正确执行。若正确,继续,否则重新排查问题。

4.3. 编译和安装Nginx

编译和安装Nginx,命令如下:

1
2
3
4
5
6
[root@localhost nginx-1.10.3]# make
[root@localhost nginx-1.10.3]# echo $?
0
[root@localhost nginx-1.10.3]# make install
[root@localhost nginx-1.10.3]# echo $?
0

因为Nginx的安装文件比较小,所以很快就会安装完,而且也不会出什么错误。

4.4. 编写Nginx启动脚本,并加入到系统服务

编写Nginx启动脚本:vim /etc/init.d/nginx,写入如下内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/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

保存该脚本后更改权限,并加入到系统服务列表里面,设置成开机启动。命令如下:

1
2
3
[root@localhost nginx-1.10.3]# chmod 755 /etc/init.d/nginx
[root@localhost nginx-1.10.3]# chkconfig --add nginx
[root@localhost nginx-1.10.3]# chkconfig nginx on

4.5. 更改Nginx的配置文件

首先把原来的配置文件清空,命令如下:

1
2
3
4
[root@localhost nginx-1.10.3]# mv /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf_bak
#<==上面这一步就是把原来的默认的Nginx配置文件备份下来
[root@localhost nginx-1.10.3]# vim /usr/local/nginx/conf/nginx.conf
#<==然后新建一个nginx.conf,并写入如下内容

Nginx的配置内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
user nobody nobody;
worker_processes 2;
error_log /usr/local/nginx/logs/nginx_error.log crit;
pid /usr/local/nginx/logs/nginx.pid;
worker_rlimit_nofile 51200;
events
{
use epoll;
worker_connections 6000;
}
http
{
include mime.types;
default_type application/octet-stream;
server_names_hash_bucket_size 3526;
server_names_hash_max_size 4096;
log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]'
' $host "$request_uri" $status'
' "$http_referer" "$http_user_agent"';
sendfile on;
tcp_nopush on;
keepalive_timeout 30;
client_header_timeout 3m;
client_body_timeout 3m;
send_timeout 3m;
connection_pool_size 256;
client_header_buffer_size 1k;
large_client_header_buffers 8 4k;
request_pool_size 4k;
output_buffers 4 32k;
postpone_output 1460;
client_max_body_size 10m;
client_body_buffer_size 256k;
client_body_temp_path /usr/local/nginx/client_body_temp;
proxy_temp_path /usr/local/nginx/proxy_temp;
fastcgi_temp_path /usr/local/nginx/fastcgi_temp;
fastcgi_intercept_errors on;
tcp_nodelay on;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 8k;
gzip_comp_level 5;
gzip_http_version 1.1;
gzip_types text/plain application/x-javascript text/css text/htm application/xml;
server
{
listen 80;
server_name localhost;
index index.html index.htm index.php;
root /usr/local/nginx/html;
location ~ \.php$
{
include fastcgi_params;
fastcgi_pass unix:/tmp/php-fcgi.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
}
}
}

关于配置文件各个参数的含义,先不解释,其实很多配置都可以根据字面意思猜到含义。保存配置文件后,需要先检验一下是否有错误,命令如下:

1
2
3
[root@localhost nginx-1.10.3]# /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

显示如上两行便是配置正确,否则要根据错误提示修改配置文件。

该配置文件可以作为一个模板,你也可以用在你的服务器上,以后工作中也可以作为参考。

4.6. 启动Nginx

启动Nginx,命令如下:

1
2
[root@localhost nginx-1.10.3]# service nginx start
正在启动 Nginx: [确定]

如果不能启动,请查看/etc/local/nginx/logs/error.log文件。

检查Nginx是否成功启动的命令:ps aux | grep nginx | grep -v grep

4.7. 测试是否正确解析PHP

首先创建测试文件,操作方法如下:

1
2
3
4
5
[root@localhost nginx-1.10.3]# vim /usr/local/nginx/html/2.php
#<==测试代码如下
<?php
echo "test php scripts.\n";
?>

执行如下命令测试文件:

1
2
[root@localhost nginx-1.10.3]# curl localhost/2.php
test php scripts.

上面显示的内容说明PHP解析正常。


到此LNMP的环境就搭建安装完毕了。只是还没有做一些基础的设置和优化配置。


0%