centos7编译安装LNMP:nginx1.16+mysql5.7+php7.2

nginx

nginx中的rewrite module、gzip module、Http SSL module需要安装PCRE、zlib、OpenSSL,这里为了方便,可以使用yum安装,也可以自行下载安装包,进行编译安装:

1
2
3
# 创建nginx运行用户
useradd -M -s /sbin/nologin www
yum -y install gcc gcc-c++ autoconf automake pcre pcre-devel zlib zlib-devel openssl openssl-devel gperftools gperftools-devel

TCMalloc的全称为Thread-Caching Malloc,是谷歌开发的开源工具google-perftools中的一个成员。与标准的glibc库的Malloc相比,TCMalloc库在内存分配效率和速度上要高很多,这在很大程度上提高了服务器在高并发情况下的性能,从而降低了系统的负载。为了优化内存,需要安装gperftools gperftools-devel,直接yum即可,想动手的话,请看后面的参考链接。

下载相关的软件包:

1
2
3
4
5
cd /usr/local/src
git clone https://github.com/alibaba/nginx-http-concat.git
git clone https://github.com/FRiCKLE/ngx_cache_purge.git
wget https://nginx.org/download/nginx-1.16.1.tar.gz
tar zxvf nginx-1.16.1.tar.gz

编译参数如下,NGINX核心知识100讲课件

1
2
3
./configure --with-http_auth_request_module --with-http_realip_module --with-http_v2_module --with-debug --with-http_random_index_module --with-http_sub_module --with-http_addition_module --with-http_secure_link_module --with-http_ssl_module --with-stream_ssl_module --with-stream_realip_module --with-stream_ssl_preread_module --with-stream --add-module=/usr/local/src/ngx_cache_purge/ --add-module=/usr/local/src/nginx-http-concat/ --with-http_slice_module --with-google_perftools_module --with-threads --with-ld-opt=-ltcmalloc --with-http_gzip_static_module --with-http_gunzip_module --with-http_stub_status_module
make
make install

安装完成之后,启动nginx:

1
2
3
4
5
6
7
8
9
10
11
12
## 检查配置文件是否正确
/usr/local/nginx/sbin/nginx -t
./sbin/nginx -V # 可以看到编译选项

## 启动、关闭
./sbin/nginx # 默认配置文件 conf/nginx.conf,-c 指定
./sbin/nginx -s stop
或 pkill nginx

## 重启,不会改变启动时指定的配置文件
./sbin/nginx -s reload
或 kill -HUP `cat /usr/local/nginx-1.6/logs/nginx.pid`

使用systemctl设置开机启动:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[root@bogon ~]# cat /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx - high performance web server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop

[Install]
WantedBy=multi-user.target

[root@bogon ~]# systemctl enable nginx.service
[root@bogon ~]# systemctl restart nginx.service

文件说明:

1
2
3
4
5
6
7
8
9
10
11
12
13
[Unit]:服务的说明
Description:描述服务
After:依赖,当依赖的服务启动之后再启动自定义的服务

[Service]服务运行参数的设置
Type=forking是后台运行的形式
ExecStart为服务的具体运行命令
ExecReload为重启命令
ExecStop为停止命令
PrivateTmp=True表示给服务分配独立的临时空间
注意:启动、重启、停止命令全部要求使用绝对路径

[Install]服务安装的相关设置,可设置为多用户

参考链接:

nginx安装第三方ngx_cache_purge模块

nginx-合并前端资源nginx-http-concat模块

如何给NGINX安装ngx_http_geoip2_module模块

使用gperftools对程序进行性能分析

systemctl 设置自定义服务管理(以nginx为例)

附安装jemalloc的方法,本次未使用:

1
2
3
4
5
6
7
wget https://github.com/jemalloc/jemalloc/releases/download/5.2.1/jemalloc-5.2.1.tar.bz2
tar jxvf jemalloc-5.2.1.tar.bz2
./configure --prefix=/usr/local/jemalloc --libdir=/usr/local/lib
make && make install
echo /usr/local/lib >> /etc/ld.so.conf
ldconfig
ldconfig -v 2>/dev/null | grep jemalloc #验证系统有没有正常加载

MYSQL 5.7

先安装相关的依赖包:

1
yum install -y gcc gcc-devel gcc-c++ gcc-c++-devel autoconf* automake* zlib* libxml* ncurses-devel ncurses libgcrypt* libtool* cmake openssl openssl-devel bisonbison-devel perl-Data-Dumper boost boost-doc boost-devel

创建用户以及数据存放目录:

1
2
3
useradd -M -s /sbin/nologin mysql
mkdir -p /data/mysql
chown mysql:mysql /data/mysql/

打开https://dev.mysql.com/downloads/mysql/ ,Select Operating System 选 Source Code,Select OS Version选 Generic Linux,下载带boost库的源码:

1
2
3
wget https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-boost-5.7.27.tar.gz
tar zxvf mysql-boost-5.7.27.tar.gz
cd mysql-5.7.27

编译参数:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DDOWNLOAD_BOOST=1 \
-DWITH_BOOST=./boost/boost_1_59_0 \
-DSYSCONFDIR=/etc \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_MEMORY_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_FEDERATED_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DWITH_READLINE=1 \
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \
-DMYSQL_TCP_PORT=3306 \
-DENABLED_LOCAL_INFILE=1 \
-DENABLE_DTRACE=0 \
-DEXTRA_CHARSETS=all \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DMYSQL_USER=mysql

make
make install

make阶段会非常久,需要耐心等待下。编译完成之后,创建所需目录,然后创建my.cnf文件:

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
mkdir -p /usr/local/mysql/data                #数据存储目录
mkdir -p /usr/local/mysql/log #日志目录
chown -R mysql.mysql /usr/local/mysql/ #更改属主数组为MySQL
cat >/etc/my.cnf <<EOF
[client]
port=3306
default-character-set=utf8

[mysqld]
user=mysql
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
port=3306
socket=/usr/local/mysql/mysql.sock
symbolic-links=0
character-set-server=utf8
pid-file=/usr/local/mysql/mysqld.pid
log-error=/usr/local/mysql/log/mysqld.log
EOF

# 加环境变量
cat > /etc/profile.d/mysql.sh << EOF
PATH=/usr/local/mysql/bin:$PATH
export PATH
EOF
source /etc/profile

初始化 mysql 数据库

之前版本初始化数据库的脚本是放在/usr/local/mysql/script目录下的mysql_install_db,但是现在已移除,转而使用mysqld替代。

注意:-–datadir目标目录下不能有数据文件。-–initialize 会生成一个随机密码,而 -–initialize-insecure 不会生成密码。如果有随机密码,可以在/usr/local/mysql/log/mysqld.log找到。如果未定义log日志目录,则直接使用grep ‘temporary password’ /var/log/mysqld.log查找。

要修改root密码,可以直接运行mysql_secure_installation即可。

1
2
rm -rf /usr/local/mysql/data/*
/usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data

初始化/usr/local/mysql/bin/mysqld运行完成之后,就直接退出了,需要再次启动,这次就直接以服务方式启动:

1
2
3
cp support-files/mysql.server /etc/init.d/mysqld
chmod 755 /etc/init.d/mysqld
/etc/init.d/mysqld start

然后再使用mysql_secure_installation修改root本地登录密码

1
2
3
4
5
6
7
8
mysql_secure_installation
Change the password for root ? y
New password:Xsssx1231231
Remove anonymous users?  y
Disallow root login remotely? y
Remove test database and access to it?  y
Reload privilege tables now?  y
All done!

参考链接

源码安装:https://dev.mysql.com/doc/refman/5.7/en/source-installation.html

使用systemd:https://dev.mysql.com/doc/refman/5.7/en/using-systemd.html

编译选项:https://dev.mysql.com/doc/refman/5.7/en/source-configuration-options.html

my.cnf配置文件说明:https://dev.mysql.com/doc/refman/5.7/en/binary-installation.html

CentOS7编译安装MySQL 5.7.20

在CentOS7上编译安装MySQL 5.7.13步骤详解

修改mysql root为弱密码的方法

PHP 7.2

先安装基础环境:

1
yum install php-mcrypt libmcrypt libmcrypt-devel  autoconf  freetype gd libmcrypt libpng libpng-devel libjpeg libxml2 libxml2-devel zlib curl curl-devel re2c net-snmp-devel libjpeg-devel php-ldap openldap-devel openldap-servers openldap-clients freetype-devel gmp-devel -y

https://www.php.net/downloads.php 下载7.2.22版本:

1
2
3
wget https://www.php.net/distributions/php-7.2.22.tar.bz2
xz -d php-7.2.22.tar.bz2
tar xvf php-7.2.22.tar

编译安装:

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
cp -frp /usr/lib64/libldap* /usr/lib/

./configure --prefix=/usr/local/php \
--with-config-file-path=/usr/local/php/etc \
--with-mysqli \
--with-pdo-mysql \
--with-mysql-sock=/usr/local/mysql/mysql.sock \
--with-iconv-dir \
--with-freetype-dir \
--with-jpeg-dir \
--with-png-dir \
--with-curl \
--with-gd \
--with-gmp \
--with-zlib \
--with-xmlrpc \
--with-openssl \
--without-pear \
--with-snmp \
--with-gettext \
--with-mhash \
--with-libxml-dir=/usr \
--with-ldap \
--with-ldap-sasl \
--with-fpm-user=www \
--with-fpm-group=www \
--enable-xml \
--enable-fpm \
--enable-ftp \
--enable-bcmath \
--enable-soap \
--enable-shmop \
--enable-sysvsem \
--enable-sockets \
--enable-inline-optimization \
--enable-maintainer-zts \
--enable-mbregex \
--enable-mbstring \
--enable-pcntl \
--enable-zip \
--disable-fileinfo \
--disable-rpath \
--enable-libxml \
--enable-opcache \
--enable-mysqlnd

make
make install

配置:

1
2
3
4
5
cp php.ini-production /usr/local/php/etc/php.ini
cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.conf
cp sapi/fpm/php-fpm.service /usr/lib/systemd/system/
systemctl enable php-fpm
systemctl status php-fpm

修改/usr/local/nginx/conf/nginx.conf的配置,让其解析php:

1
2
3
4
5
6
7
8
9
10
11
12
location / {
root html;
index index.php index.html index.htm;
}

location ~* \.php$ {
fastcgi_index index.php;
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
}

修改完systemctl reload nginx.service,再编写一个phpinfo页面,能正常访问就表示部署成功了。

1
2
3
4
vim /usr/local/nginx/html/index.php
<?php
phpinfo();
?>

参考资料:

centos7.2源码编译安装LNMP

Centos7.4 编译安装PHP7.2(Nginx篇)

  • 本文作者: wumingx
  • 本文链接: https://www.wumingx.com/linux/centos7-install-lnmp.html
  • 本文主题: centos7编译安装LNMP:nginx1.16+mysql5.7+php7.2
  • 版权声明: 本站所有文章除特别声明外,转载请注明出处!如有侵权,请联系我删除。
0%