LNMP源码编译安装

LNMP是一个Web应用软件组合,即(Linux+Nginx+MySQL+PHP)

  首先你需要一个Linux系统,可以是CentosDebianUbuntu等类Linux/Unix系统,下面我们来逐个进行编译安装,这里以Centos为例:

1、Nginx(Nginx1.14.2)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//安装依赖
yum install -y pcre-devel zlib-devel openssl-devel
//获取源码
wget http://nginx.org/download/nginx-1.14.2.tar.gz
//解压安装
tar -zxvf nginx-1.14.2.tar.gz
cd nginx-1.14.2
./configure --prefix=/usr/local/nginx
make && make install
//创建用户www
groupadd www
useradd -g www www
//修改nginx.conf配置文件支持php
 fastcgi_param  SCRIPT_FILENAME 
$document_root$fastcgi_script_name;

2、MySQL(MySQL5.7)

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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
//安装依赖
yum -y install gcc gcc-c++ ncurses ncurses-devel cmake bison
//获取源码
wget https://sourceforge.net/projects/boost/files/boost/1.59.0/boost_1_59_0.tar.gz
wget http://cdn.mysql.com/Downloads/MySQL-5.7/mysql-5.7.13.tar.gz
//创建MySQL用户和用户组
groupadd -r mysql && useradd -r -g mysql -s /sbin/nologin -M mysql
//解压
tar -zxvf boost_1_59_0.tar.gz
tar -zxvf mysql-5.7.13.tar.gz
//创建数据目录
mkdir -p /data/mysql
//进入mysql-5.7.13预编译MySQL
cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_DATADIR=/data/mysql \
-DWITH_BOOST=../boost_1_59_0 \
-DSYSCONFDIR=/etc \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DWITH_FEDERATED_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DENABLED_LOCAL_INFILE=1 \
-DENABLE_DTRACE=0 \
-DDEFAULT_CHARSET=utf8mb4 \
-DDEFAULT_COLLATION=utf8mb4_general_ci \
-DWITH_EMBEDDED_SERVER=1
//编译安装
make -j `grep processor /proc/cpuinfo | wc -l`
make install
//设置启动脚本,开机自启动
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld
systemctl enable mysqld
//修改my.cnf配置文件
cat > /etc/my.cnf << EOF
[client]
port = 3306
socket = /dev/shm/mysql.sock
[mysqld]
port = 3306
socket = /dev/shm/mysql.sock
basedir = /usr/local/mysql
datadir = /data/mysql
pid-file = /data/mysql/mysql.pid
user = mysql
bind-address = 0.0.0.0
server-id = 1
init-connect = 'SET NAMES utf8mb4'
character-set-server = utf8mb4
#skip-name-resolve#skip-networking
back_log = 300
max_connections = 1000
max_connect_errors = 6000
open_files_limit = 65535
table_open_cache = 128
max_allowed_packet = 4M
binlog_cache_size = 1M
max_heap_table_size = 8M
tmp_table_size = 16M
read_buffer_size = 2M
read_rnd_buffer_size = 8M
sort_buffer_size = 8M
join_buffer_size = 8M
key_buffer_size = 4M
thread_cache_size = 8
query_cache_type = 1
query_cache_size = 8M
query_cache_limit = 2M
ft_min_word_len = 4
log_bin = mysql-bin
binlog_format = mixed
expire_logs_days = 30
log_error = /data/mysql/mysql-error.log
slow_query_log = 1
long_query_time = 1
slow_query_log_file = /data/mysql/mysql-slow.log
performance_schema = 0
explicit_defaults_for_timestamp
#lower_case_table_names = 1
skip-external-locking
default_storage_engine = InnoDB
#default-storage-engine = MyISAM
innodb_file_per_table = 1
innodb_open_files = 500
innodb_buffer_pool_size = 64M
innodb_write_io_threads = 4
innodb_read_io_threads = 4
innodb_thread_concurrency = 0
innodb_purge_threads = 1
innodb_flush_log_at_trx_commit = 2
innodb_log_buffer_size = 2M
innodb_log_file_size = 32M
innodb_log_files_in_group = 3
innodb_max_dirty_pages_pct = 90
innodb_lock_wait_timeout = 120
bulk_insert_buffer_size = 8M
myisam_sort_buffer_size = 8M
myisam_max_sort_file_size = 10G
myisam_repair_threads = 1
interactive_timeout = 28800
wait_timeout = 28800
[mysqldump]
quick
max_allowed_packet = 16M
[myisamchk]
key_buffer_size = 8M
sort_buffer_size = 8M
read_buffer = 4M
write_buffer = 4M
EOF
//初始化数据库
mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql
//启动数据库
systemctl start mysqld
//设置数据库root用户密码
mysql_secure_installation
//设置MySQL服务可被其他服务调用
echo "/usr/local/mysql/lib" > /etc/ld.so.conf.d/mysql.conf
ldconfig

3、PHP(PHP7.1.6)

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
//安装依赖
yum -y install libxml2 libxml2-devel openssl openssl-devel curl-devel libjpeg-devel libpng-devel freetype-devel libmcrypt-devel
//解压安装
tar -zxvf php-7.1.6.tar.gz
./configure \
--prefix=/usr/local/php71 \
--exec-prefix=/usr/local/php71 \
--bindir=/usr/local/php71/bin \
--sbindir=/usr/local/php71/sbin \
--includedir=/usr/local/php71/include \
--libdir=/usr/local/php71/lib/php \
--mandir=/usr/local/php71/php/man \
--with-config-file-path=/usr/local/php71/etc \
--with-mysql-sock=/dev/shm/mysql.sock \
--with-mcrypt=/usr/include \
--with-mhash \
--with-openssl \
--with-mysqli=shared,mysqlnd \
--with-pdo-mysql=shared,mysqlnd \
--with-gd \
--with-iconv \
--with-zlib \
--enable-zip \
--enable-inline-optimization \
--disable-debug \
--disable-rpath \
--enable-shared \
--enable-xml \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--enable-mbregex \
--enable-mbstring \
--enable-ftp \
--enable-gd-native-ttf \
--enable-pcntl \
--enable-sockets \
--with-xmlrpc \
--enable-soap \
--without-pear \
--with-gettext \
--enable-session \
--with-curl \
--with-jpeg-dir \
--with-freetype-dir \
--enable-opcache \
--enable-fpm \
--with-fpm-user=www \
--with-fpm-group=www \
--without-gdbm \
--disable-fileinfo
make && make install
//配置nginx支持php-fpm,首先创建www用户和用户组
groupadd www
useradd -g www www
//复制php-fpm配置文件
cd /usr/local/php71/etc
cp php-fpm.conf.default php-fpm.conf
//进入php-fpm.d目录,复制www.conf文件
cd php-fpm.d
cp www.conf.default www.conf
//从PHP源码复制php.ini文件
cp /usr/src/php-7.1.6/php.ini-development /usr/local/php71/etc/php.ini

编译安装扩展时出现如下错误,这是因为没有安装autoconf

1
2
3
4
5
//错误
Cannot find autoconf. Please check your autoconf installation and the $PHP_AUTOCONF environment variable. Then, rerun this scrip
//解决办法
yum -y install m4
yum -y install autoconf

4、安装composer

1
2
3
4
5
6
//下载composer
curl -sS https://getcomposer.org/installer | php
//将composer.phar移入bin目录,使composer可全局使用
mv composer.phar /usr/local/bin/composer
//切换国内源,以加快composer速度
composer config -g repo.packagist composer https://packagist.laravel-china.org

5、安装redis(redis5.03)

1
2
3
4
5
6
7
8
9
10
//获取redis源码
wget http://download.redis.io/releases/redis-5.0.3.tar.gz
//解压
tar -zxvf redis-5.0.3.tar.gz
//安装
make && make install
//修改redis.conf,使redis后台运行
daemonize yes
//启动redis
redis-server redis.conf

安装redis扩展

1
2
3
4
5
6
7
//获取源码
git clone https://github.com/phpredis/phpredis.git
//安装
phpize
./configure --with-php-config=/usr/local/php71/bin/php-config
make && make install
//将生成的redis.so加入php.ini文件

6、安装swoole扩展(swoole4.3.2)

1
2
3
4
5
6
7
//获取源码
git clone https://github.com/swoole/swoole-src/releases/tag/v4.3.2-rc1
//安装
phpize
./configure --with-php-config=/usr/local/php71/bin/php-config
make && make install
//将生成的swoole.so加入php.ini文件

7、安装amqp扩展(amqp1.9.4)

1
2
3
4
5
6
7
8
9
10
11
12
13
//安装依赖
wget https://github.com/alanxz/rabbitmq-c/releases/download/v0.7.1/rabbitmq-c-0.7.1.tar.gz
tar -zxvf rabbitmq-c-0.7.1.tar.gz
cd rabbitmq-c-0.7.1
./configure --prefix=/usr/local/rabbitmq-c
make && make install
//安装amqp
wget http://pecl.php.net/get/amqp-1.9.4.tgz
tar -zxvf amqp-1.9.4.tgz
cd amqp-1.9.4
/usr/local/php71/bin/phpize
./configure --with-php-config=/usr/local/php71/bin/php-config --with-amqp --with-librabbitmq-dir=/usr/local/rabbitmq-c
make && make install