基于 CentOS6.x 源码编译安装 LNMP 环境

这是之前在学习、工作过程中整理的笔记,完整的记录了以源码方式编译、配置 LNMP 环境的步骤,虽然也小范围用于生产环境的服务构建,但建议只把它拿来学习用,因为有些细节和优化项并不完善。

生产环境,强烈建议使用以下两个相对成熟、稳定的脚本工具:

1. 准备篇

1.1. 推荐目录结构

1
mkdir -p /lnmp/server /lnmp/www /lnmp/log/mysql /lnmp/log/nginx /lnmp/log/php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 注:完整的服务配置好后的目录结构类似如下
/lnmp/
├── log
│   ├── mysql
│   ├── nginx
│   └── php
├── server
│   ├── mysql -> /lnmp/server/mysql-5.x.xx
│   ├── mysql-5.x.xx
│   ├── nginx -> /lnmp/server/nginx-1.4.4
│   ├── nginx-1.4.4
│   ├── php -> /lnmp/server/php-5.x.xx
│   └── php-5.x.xx
├── info.log
└── www
1
2
3
4
chown -R www:www /lnmp/log
chmod -R 755 /lnmp/log
chown -R www:www /lnmp/www
chmod -R 755 /lnmp/www

1.2. 下载源码包

1
2
3
4
5
6
7
# 源码包下载存放目录
mkdir -p /root/src && cd /root/src

# 其它版本及备用下载地址参见附录
➜ wget http://dev.mysql.com/get/Downloads/MySQL-5.5/mysql-5.5.50-linux2.6-x86_64.tar.gz
➜ wget http://nginx.org/download/nginx-1.6.3.tar.gz
➜ wget http://cn2.php.net/distributions/php-5.5.36.tar.gz

1.3. 防火墙开放80/3306端口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 查看当前防火墙状态
➜ /etc/init.d/iptables status

# 编辑防火墙配置文件
➜ vim /etc/sysconfig/iptables
.....
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT     <------- 开放80
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT   <------- 开放3306
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT

# 重启防火墙
➜ /etc/init.d/iptables restart

1.4. 关闭 SELinux

1
2
3
# 对于 Linux 新手,建议直接关闭,以免出现不知所然的问题
➜ sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
➜ shutdown -r now

1.5. 安装编译时所需依赖

1
➜  yum -y install gcc gcc-c++ kernel-devel

2. 安装 MySQL

2.1. 创建用户

1
2
3
4
5
6
7
8
9
10
11
➜  groupadd mysql
➜ useradd -s /sbin/nologin -g mysql -M mysql

# 也可以只使用下面这一条命令替代
➜ useradd mysql -s /sbin/nologin -M

# 检查用户是否创建成功
tail -1 /etc/passwd
mysql:x:501:501::/home/mysql:/sbin/nologin
id mysql
uid=501(mysql) gid=501(mysql) groups=501(mysql)

2.2. 解压、配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
cd /root/src
➜ tar -zxvf mysql-5.5.50-linux2.6-x86_64.tar.gz
mv mysql-5.5.50-linux2.6-x86_64 /lnmp/server/mysql-5.5.50 && cd /lnmp/server
ls -l

# 建立具体版本的软连接
ln -s /lnmp/server/mysql-5.5.50/ mysql
ls -l
cd mysql

# 查看所有配置样例
ls -l support-files/*.cnf

# 使用 /bin/cp 直接覆盖重名文件,不提示
➜ /bin/cp support-files/my-large.cnf /etc/my.cnf

2.3. 初始化数据

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 创建一个单独的目录,存放物理数据文件
mkdir -p /data/mysql
chown -R mysql:mysql /data/mysql

# 执行初始化脚本
➜ ./scripts/mysql_install_db --basedir=/lnmp/server/mysql --datadir=/data/mysql --user=mysql
...
Installing MySQL system tables...
160529 18:23:19 [Note] /lnmp/server/mysql/bin/mysqld (mysqld 5.5.50) starting as process 33182 ...
OK
Filling help tables...
160529 18:23:20 [Note] /lnmp/server/mysql/bin/mysqld (mysqld 5.5.50) starting as process 33189 ...
OK

To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system

...

# 注意:如上出现两个 OK,则表示初始化成功;否则失败,需解决初始化错误。

2.4. 修改设置并启动服务

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
cp ./support-files/mysql.server /etc/init.d/mysqld
chmod 755 /etc/init.d/mysqld

# 替换二进制包的默认MySQL安装路径
➜ sed -i 's#/usr/local/mysql#/lnmp/server/mysql#g' /lnmp/server/mysql/bin/mysqld_safe /etc/init.d/mysqld

# 修改 basedir 和 datadir
➜ vim /etc/init.d/mysqld
basedir=/lnmp/server/mysql
datadir=/data/mysql

# 启动服务
➜ /etc/init.d/mysqld start
Starting MySQL... SUCCESS!

# 检查 3306 是否启动:
➜ netstat -tlunp|grep mysql
tcp        0      0 0.0.0.0:3306     0.0.0.0:*      LISTEN      33457/mysqld

2.5. 设置开机自启动

1
2
3
4
➜  chkconfig --add mysqld
➜ chkconfig mysqld on
➜ chkconfig --list|grep mysqld
mysqld          0:off   1:off   2:on    3:on    4:on    5:on    6:off

2.6. 配置MySQL命令全局使用

1
2
3
4
5
6
7
8
9
10
echo 'export PATH=/lnmp/server/mysql/bin:$PATH' >>/etc/profile

# 检查写入文件
tail -1 /etc/profile
export PATH=/lnmp/server/mysql/bin:$PATH
source /etc/profile

# 检查最终设置结果
echo $PATH
/lnmp/server/mysql/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/whoru/bin

2.7. 登录测试,更改默认管理员密码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 输入 mysql 可以直接登录,安装好后默认密码为空
➜ mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
.....
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>quit;

# 修改 root 默认的空密码
➜ mysqladmin -u root password 'whoru123'

# 重新连接测试
➜ mysql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

# 必须使用用户名和密码登录
➜ mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
.....
mysql>

2.8. 清理没有用的用户及库

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
mysql> select user,host from mysql.user;
+------+-----------+
| user | host      |
+------+-----------+
| root | 127.0.0.1 |
| root | ::1       |
|      | localhost |
| root | localhost |
|      | web1      |
| root | web1      |
+------+-----------+
6 rows in set (0.00 sec)

mysql> drop user "root"@"::1";
Query OK, 0 rows affected (0.00 sec)

mysql> drop user ""@"localhost";
Query OK, 0 rows affected (0.00 sec)

mysql> drop user ""@"web1";
Query OK, 0 rows affected (0.00 sec)

mysql> drop user "root"@"web1";
Query OK, 0 rows affected (0.01 sec)

mysql> select user,host from mysql.user;
+------+-----------+
| user | host      |
+------+-----------+
| root | 127.0.0.1 |
| root | localhost |
+------+-----------+
2 rows in set (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

3. 安装 Nginx

3.1. 安装依赖包

1
2
3
4
5
6
7
8
# 检查所依赖的包是否已经安装
# 其中 pcre 用于 nginx 的 rewrite 模块
➜ rpm -qa pcre* openssl* zlib*
zlib-1.2.3-29.el6.x86_64
openssl-1.0.1e-48.el6_8.1.x86_64
pcre-7.8-7.el6.x86_64

➜ yum -y install pcre-devel openssl-devel zlib-devel

3.2. 添加用户

1
➜  useradd www -s /sbin/nologin -M

3.3. 安装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
cd /root/src
➜ tar -zxvf nginx-1.6.3.tar.gz && cd nginx-1.6.3
➜ ./configure \
--prefix=/lnmp/server/nginx-1.6.3 \
--error-log-path=/lnmp/log/nginx/error.log \
--http-log-path=/lnmp/log/nginx/access.log \
--user=www \
--group=www \
--with-http_realip_module \
--with-http_sub_module \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-pcre
➜ make
➜ make install
cd /lnmp/server
ln -s /lnmp/server/nginx-1.6.3/ nginx
ls -l

提示:编译安装过程中可以使用命令 # echo $?configure / make 的执行结果,0 表示成功.

3.4. 启动

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 检查配置文件语法
➜ /lnmp/server/nginx/sbin/nginx -t
nginx: the configuration file /lnmp/server/nginx-1.6.3//conf/nginx.conf syntax is ok
nginx: configuration file /lnmp/server/nginx-1.6.3//conf/nginx.conf test is successful

# 启动
➜ /lnmp/server/nginx/sbin/nginx

# 检查
➜ lsof -i :80
COMMAND   PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
nginx   38366  root    6u  IPv4  93417      0t0  TCP *:http (LISTEN)
nginx   38367 nginx    6u  IPv4  93417      0t0  TCP *:http (LISTEN)
➜ netstat -tlunp|grep 80
tcp        0      0 0.0.0.0:80    0.0.0.0:*        LISTEN      38366/nginx
➜ wget 127.0.0.1
➜ curl 127.0.0.1

注:当浏览器无法访问时,尝试修改 SELinux、iptables

3.5. 添加快捷启动脚本

,之后可以通过 service nginx xxx 方式控制

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 文件内容见附件 nginx.sh
➜ vim /etc/init.d/nginx
chmod 755 /etc/init.d/nginx
➜ service nginx
Usage: /etc/init.d/nginx {start|stop|reload|restart|configtest}

# 配置文件检查
➜ service nginx configtest
nginx: the configuration file /lnmp/server/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /lnmp/server/nginx/conf/nginx.conf test is successful

# 启动
➜ service nginx start
Starting Nginx:                                            [  OK  ]

3.6. 设置开机自启动

1
2
3
4
➜  chkconfig --add nginx
➜ chkconfig nginx on
➜ chkconfig --list|grep nginx
nginx          0:off   1:off   2:on    3:on    4:on    5:on    6:off

4. 安装 PHP

4.1. 检查 MySQL、Nginx是否正常

1
➜  netstat -tlunp

4.2. 检查安装 PHP 所需的 lib 库

1
2
3
➜  rpm -qa zlib-devel libxm2-devel libjpeg-turbo-devel libpng-devel gd-devel libcurl-devel libxslt-devel freetype-devel
zlib-devel-1.2.3-29.el6.x86_64
➜ yum -y install zlib-devel libxm2-devel libjpeg-turbo-devel libpng-devel gd-devel libcurl-devel libxslt-devel freetype-devel

⚠️ 注意:默认 yum 源没有 libiconv-devel 这个包,需要按照下面的步骤单独编译安装:

1
2
3
4
5
6
7
8
9
cd /root/src
➜ wget http://ftp.gnu.org/gnu/libiconv/libiconv-1.14.tar.gz
➜ tar -zxvf libiconv-1.14.tar.gz && cd libiconv-1.14
➜ ./configure --prefix=/usr/local/libiconv
➜ make && make install

# 安装其它额外库
➜ wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
➜ yum -y install libmcrypt-devel mhash mcrypt

4.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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
cd /root/src
➜ tar zxvf php-5.5.36.tar.gz && cd php-5.5.36
➜ ./configure \
--prefix=/lnmp/server/php5.5.36 \
--with-config-file-path=/lnmp/server/php5.5.36/etc \
--enable-inline-optimization \
--disable-debug \
--disable-rpath \
--enable-shared \
--enable-fpm \
--with-fpm-user=www \
--with-fpm-group=www \
--with-mysql=mysqlnd \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--enable-exif \
--with-gd \
--enable-gd-native-ttf \
--with-jpeg-dir \
--with-png-dir \
--with-freetype-dir \
--with-gettext \
--with-iconv-dir=/usr/local/libiconv \
--enable-mbstring \
--with-mcrypt \
--with-mhash \
--with-openssl \
--enable-bcmath \
--enable-pcntl  \
--enable-shmop \
--enable-sysvsem \
--enable-soap \
--with-libxml-dir \
--enable-sockets \
--with-curl \
--enable-ftp \
--with-zlib-dir \
--enable-zip \
--with-pear

ln -s /lnmp/server/php5.5.36/ /lnmp/server/php
➜ ll php.ini*
-rw-r--r-- 1 1001 1001 69236 5月  25 17:36 php.ini-development   <------- 开发模式,更多开启日志、调试信息
-rw-r--r-- 1 1001 1001 69266 5月  25 17:36 php.ini-production    <------- 生产模式
cp php.ini-production /lnmp/server/php/etc/php.ini

4.4. 配置 PHP 服务(php-fpm 方式)

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
cd /lnmp/server/php/etc/
ls
pear.conf  php-fpm.conf.default
cp php-fpm.conf.default php-fpm.conf

# 启动 php-fmp 服务
➜ /lnmp/server/php/sbin/php-fpm

# 查看 php-fpm 启动进程
➜ ps -ef|grep php-fpm
root       2270      1  0 00:39 ?        00:00:00 php-fpm: master process (/lnmp/server/php5.5.36/etc/php-fpm.conf)
www        2271   2270  0 00:39 ?        00:00:00 php-fpm: pool www
www        2272   2270  0 00:39 ?        00:00:00 php-fpm: pool www
root       2274   2227  0 00:39 pts/0    00:00:00 grep php-fpm

# 查看 php-fpm 默认端口
➜ lsof -i :9000
COMMAND  PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
php-fpm 2270 root    7u  IPv4  12844      0t0  TCP localhost:cslistener (LISTEN)
php-fpm 2271  www    0u  IPv4  12844      0t0  TCP localhost:cslistener (LISTEN)
php-fpm 2272  www    0u  IPv4  12844      0t0  TCP localhost:cslistener (LISTEN)

# 设置 php-fpm 启动脚本
➜ vim /etc/init.d/php-fpm

# 复制 php-fpm.sh 中的内容进去
chmod 755 /etc/init.d/php-fpm
➜ service php-fpm start|stop|restart|reload|configtest

4.5. 配置 Nginx 支持 PHP 程序请求访问

1
2
3
4
5
6
7
8
9
10
11
12
server {
    listen       80;
    server_name  localhost;
    root   /lnmp/www/;
    index  index.html index.htm index.php;

    location ~ .*\.(php|php5)?$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        include        fastcgi.conf;
    }
}

5. 附录(软件下载连接)

5.1. MySQL

5.2. Nginx

5.3. PHP