首页 手动编译并配置lnmp
文章
取消

手动编译并配置lnmp

本篇教程是lnmp(centos7+nginx1.15-mysql8-php7)编译配置步骤说明,这里只编译nginx和php,linux自己编译太耗费时间,mysql8编译要5G内存,所以这个两个就不手动编译了。

一、安装nginx

1).下载nginx

nginx:https://nginx.org/download/nginx-1.15.9.tar.gz

pcre(nginx-rewrite需要):ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.38.tar.bz2

zlib(nginx-gzip需要):https://www.zlib.net/zlib-1.2.11.tar.xz

openssl(nginx-ssl需要,即开https需要):https://www.openssl.org/source/openssl-1.0.2r.tar.gz

2).编译nginx

下载几个包并解压,然后进入nginx目录开始编译:

1
2
[root@liuquanhao ~]# ./configure --with-pcre=../pcre-8.38 --with-zlib=../zlib-1.2.11 --with-openssl=../openssl-1.0.2r --prefix=/opt/nginx
[root@liuquanhao ~]# make && make install

很简单,没有配置其他, 只是引入了3个依赖包,然后用--prefix指定了nginx安装到/opt/nginx目录下。后面的软件都会装到/opt下,这个目录是linux目录规范里,用来装个人软件的地方。

3).配置nginx

安装好后先添加一个www-data用户:

1
2
[root@liuquanhao ~]# groupadd www-data
[root@liuquanhao ~]# useradd -r -M -s /bin/false -g www-data www-data

然后添加一个systemd配置用来自启动nginx:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[root@liuquanhao ~]# cat /usr/lib/systemd/system/nginx.service
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=local-fs.target network.target

[Service]
Type=forking
PrivateTmp=true
PIDFile=%t/nginx/nginx.pid
ExecStartPre=/bin/mkdir -p %t/nginx
ExecStartPre=/bin/chown -R www-data:www-data %t/nginx
ExecStartPre=/opt/nginx/sbin/nginx -t
ExecStart=/opt/nginx/sbin/nginx
ExecStartPost=/bin/sleep 0.1
ExecReload=/opt/nginx/sbin/nginx -s reload
ExecStop=/bin/kill -s QUIT $MAINPID

[Install]
WantedBy=multi-user.target

[root@liuquanhao ~]# systemctl enable nginx.service
[root@liuquanhao ~]# systemctl start nginx.service

二、安装php7

1).下载php7

python:https://www.python.org/ftp/python/3.7.2/Python-3.7.2.tgz

libxml2:ftp://xmlsoft.org/libxml2/libxml2-sources-2.9.9.tar.gz

php7:http://cn2.php.net/distributions/php-7.3.2.tar.xz

2).编译php7

编译php7稍微多一步,因为php7依赖libxml2,而libxml2需要先编译安装好。这个可以直接用yum或apt安装相应的libxml2-dev包,但是我这里选手动编译。

因为libxml2依赖python.h,所以需要下载python源码,然后解压它们,进入libxml2目录开始编译。

1
2
[root@liuquanhao ~]# ./configure --with-python=../Python-3.7.2 --prefix=/opt/libxml2
[root@liuquanhao ~]# make && make install

然后进入php目录编译php7:

1
2
[root@liuquanhao ~]# ./configure --prefix=/opt/php --with-libxml-dir=/opt/libxml2 --enable-fpm --with-fpm-user=www-data --with-fpm-group=www-data --sysconfdir=/opt/php/etc/fpm --with-config-file-path=/opt/php/etc/cli --with-config-file-scan-dir=/opt/php/etc
[root@liuquanhao ~]# make && make install

这里我开启了php-fpm模块,这个是肯定要添加的,然后php-fpm的配置在/opt/php/etc下,php cli的配置文件在/opt/php/etc/cli目录下。

3).配置php7

先把php-fpm配置设置好:

1
2
[root@liuquanhao fpm]# cp /opt/php/etc/fpm/php-fpm.conf.default /opt/php/etc/fpm/php-fpm.conf
[root@liuquanhao fpm]# cp /opt/php/etc/fpm/php-fpm.d/www.conf.default /opt/php/etc/fpm/php-fpm.d/www.conf

然后添加一个systemd配置用来自启动php-fpm:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[root@liuquanhao ~]# cat /usr/lib/systemd/system/php-fpm.service
[Unit]
Description=PHP FastCGI process manager
After=local-fs.target network.target nginx.service

[Service]
Type=simple
PrivateTmp=true
PIDFile=%t/php7-fpm/php-fpm.pid
ExecStartPre=/bin/mkdir -p %t/php7-fpm
ExecStartPre=/bin/chown -R www-data:www-data %t/php7-fpm
ExecStart=/opt/php/sbin/php-fpm --fpm-config /opt/php/etc/fpm/php-fpm.conf --nodaemonize
ExecReload=/bin/kill -USR2 $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID

[Install]
WantedBy=multi-user.target

[root@liuquanhao ~]# systemctl enable php-fpm.service
[root@liuquanhao ~]# systemctl start php-fpm.service

三、安装mysql

1).下载mysql

mysql8:https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.15-linux-glibc2.12-x86_64.tar.xz libaio:ttp://ftp.de.debian.org/debian/pool/main/liba/libaio/libaio_0.3.112.orig.tar.xz

这里说明一下,其实mysql8编译最简单,装好了libaio,只需要./configure --prefix=/opt/mysql && make && make install即可。

2).编译mysql依赖包

mysql依赖libaio配置,可以用yum或apt安装libaio-dev包,但我这里也是选择手动编译。

进入libaio目录:

1
root@liuquanhao ~]# ./configure && make && make install

很简单,但是这时mysql依然会找不到libaio.so,因为libaio默认安装到了/lib/usr/lib下,而mysql会在/lib64下找,所以需要把文件软链接到这个目录下:

1
2
root@liuquanhao ~]# ln -s /usr/lib/libaio.so.1.0.1 /lib64/libaio.so
root@liuquanhao ~]# ln -s /usr/lib/libaio.so.1.0.1 /lib64/libaio.so.1

然后把mysql8解压到/opt目录下:

1
2
3
4
[root@liuquanhao ~]# cp mysql-8.0.15-linux-glibc2.12-x86_64.tar.xz /opt
[root@liuquanhao ~]# cd /opt
[root@liuquanhao ~]# tar -xvf mysql-8.0.15-linux-glibc2.12-x86_64.tar.xz
[root@liuquanhao ~]# ln -s mysql-8.0.15-linux-glibc2.12-x86_64 mysql

3).配置mysql8

先添加mysql用户:

1
2
[root@liuquanhao ~]# groupadd mysql
[root@liuquanhao ~]# useradd -r -M -s /bin/false -g mysql mysql

然后配置mysql敏感数据目录:

1
2
3
4
[root@liuquanhao ~]# cd mysql
[root@liuquanhao ~]# chown -R root:root .
[root@liuquanhao ~]# mkdir mysql-files
[root@liuquanhao ~]# chown -R mysql:mysql mysql-files

接着初始化mysql数据库文件:

1
[root@liuquanhao ~]# mysqld --user=mysql --initialize

这样mysql目录下就有了一个data目录,里面就是mysql的数据库文件。

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
[root@liuquanhao ~]# ls -al /opt/mysql/data
total 168032
drwxr-x---.  6 mysql mysql     4096 3月   5 19:58 .
drwxr-xr-x. 11 root  root      4096 3月   5 19:27 ..
-rw-r-----.  1 mysql mysql       56 3月   5 19:08 auto.cnf
-rw-r-----.  1 mysql mysql      664 3月   5 19:20 binlog.000001
-rw-r-----.  1 mysql mysql      178 3月   5 19:39 binlog.000002
-rw-r-----.  1 mysql mysql      178 3月   5 19:44 binlog.000003
-rw-r-----.  1 mysql mysql      178 3月   5 19:56 binlog.000004
-rw-r-----.  1 mysql mysql      178 3月   5 19:57 binlog.000005
-rw-r-----.  1 mysql mysql      155 3月   5 19:58 binlog.000006
-rw-r-----.  1 mysql mysql       96 3月   5 19:58 binlog.index
-rw-------.  1 mysql mysql     1680 3月   5 19:08 ca-key.pem
-rw-r--r--.  1 mysql mysql     1112 3月   5 19:08 ca.pem
-rw-r--r--.  1 mysql mysql     1112 3月   5 19:08 client-cert.pem
-rw-------.  1 mysql mysql     1676 3月   5 19:08 client-key.pem
-rw-r-----.  1 mysql mysql     3298 3月   5 19:57 ib_buffer_pool
-rw-r-----.  1 mysql mysql 12582912 3月   5 19:58 ibdata1
-rw-r-----.  1 mysql mysql 50331648 3月   5 19:58 ib_logfile0
-rw-r-----.  1 mysql mysql 50331648 3月   5 19:08 ib_logfile1
-rw-r-----.  1 mysql mysql 12582912 3月   5 19:58 ibtmp1
drwxr-x---.  2 mysql mysql     4096 3月   5 19:58 #innodb_temp
drwxr-x---.  2 mysql mysql     4096 3月   5 19:08 mysql
-rw-r-----.  1 mysql mysql 25165824 3月   5 19:58 mysql.ibd
drwxr-x---.  2 mysql mysql     4096 3月   5 19:08 performance_schema
-rw-------.  1 mysql mysql     1680 3月   5 19:08 private_key.pem
-rw-r--r--.  1 mysql mysql      452 3月   5 19:08 public_key.pem
-rw-r--r--.  1 mysql mysql     1112 3月   5 19:08 server-cert.pem
-rw-------.  1 mysql mysql     1676 3月   5 19:08 server-key.pem
drwxr-x---.  2 mysql mysql     4096 3月   5 19:08 sys
-rw-r-----.  1 mysql mysql 10485760 3月   5 19:58 undo_001
-rw-r-----.  1 mysql mysql 10485760 3月   5 19:58 undo_002

然后添加一个systemd配置用来自启动mysqld:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[root@liuquanhao ~]# cat /usr/lib/systemd/system/mysqld.service
[Unit]
Description=Mysql Server
After=syslog.target network.target

[Service]
Type=simple
User=mysql
Group=mysql
PermissionsStartOnly=true
TimeoutSec=300
PrivateTmp=true
PIDFile=%t/mysqld/mysqld.pid
ExecStartPre=/bin/mkdir -p %t/mysqld
ExecStartPre=/bin/chown -R mysql:mysql %t/mysqld
ExecStart=/opt/mysql/bin/mysqld
ExecStop=/bin/kill -s QUIT $MAINPID

[Install]
WantedBy=multi-user.target

[root@liuquanhao ~]# systemctl enable mysqld.service
[root@liuquanhao ~]# systemctl start mysqld.service

四、配置环境变量

现在都安装好了,但是/opt目录下的软件包的bin/目录不在$PATH变量里,所以输入命令会提示找不到命令,这就添加一下:

1
2
3
4
5
6
7
8
[root@liuquanhao ~]# cat /etc/profile.d/sh.local
#Add any required envvar overrides to this file, it is sourced from /etc/profile

export PATH=/opt/nginx/sbin:$PATH
export PATH=/opt/php/bin:$PATH
export PATH=/opt/mysql/bin:$PATH

[root@liuquanhao ~]# source /etc/profile.d/sh.local

五、总结

好了,这样就全部安装好了,如果还想自己编译linux系统,可以学习lfs

知识共享许可协议 本文由作者按照 CC BY-SA 4.0 进行授权