之前总结过一个lamp环境的搭建,现在几乎已经没有用到Apache的httpd了吧,因此重新整理一个lnmp的环境搭建过程
lnmp简介
lnmp(Linux,Nginx,MySQL/MariaDB,PHP),这个架构师从lamp发展而来的,在互联网行业刚刚兴起的时候,利用这样一套免费且可以非常迅速的开发一个web站点并上线使用,在当时可谓是各个老板非常中意的技术,apahce的httpd以稳定号称,但是在发展了几年后发现,其并发性显得有点捉襟见肘,这时俄罗斯人开发的nginx逐渐走入了大家的实现,因此慢慢的发展成了lnmp的架构,
而慢慢这套架构应付业务的请求也很吃力了,人们开始拆分,将数据库拆出来,将PHP拆出来,php可以部署多台组成集群,nginx也需要至少两台来做高可用,数据库可能也分出了一主多从,甚至加入memcached,redis使用内存来缓存数据,对于静态资源还可以通过CND进行缓存,减少主站的压力并且提高其读取的速度,这样架构又可以支撑很长时间,
但是这好像还有问题,一是任何一部分出现问题都可能导致全站宕机,另一方面这种扩展对数据库也是有上限的,毕竟读数据可以通过多从,缓存实现,但是写请求没有办法分摊到所有机器上平均的执行,再后来人们开始拆分业务,搞这种微服务的架构,当然这里又需要到好多分布式式相关的内容,做这种服务发现,服务治理,而docker,kubernetes这些新技术的出现更是为微服务这条路填了不少砖瓦
牛逼吹完了,下面就搭一个最简单的lnmp环境,供大家参考
软件版本
软件名称 | 软件版本 |
---|---|
操作系统 | CentOS7.6最小化安装 |
NGINX | 1.16.1 |
PHP | 7.3.15 |
MariaDB | 10.4.12(二进制包) |
软件包下载
cd /usr/local/src
wget http://nginx.org/download/nginx-1.16.1.tar.gz # nginx
wget https://www.php.net/distributions/php-7.3.15.tar.xz # php
wget https://downloads.mariadb.org/interstitial/mariadb-10.4.12/bintar-linux-systemd-x86_64/mariadb-10.4.12-linux-systemd-x86_64.tar.gz/from/http%3A//mirrors.tuna.tsinghua.edu.cn/mariadb/ -O mariadb-10.4.12-linux-systemd-x86_64.tar.gz # MariaDB
依赖包下载
wget https://github.com/openssl/openssl/archive/OpenSSL_1_1_1d.tar.gz
wget http://zlib.net/zlib-1.2.11.tar.xz
wget https://ftp.pcre.org/pub/pcre/pcre-8.43.tar.bz2
wget https://libzip.org/download/libzip-1.3.1.tar.xz
对于以上的这些源码,如果下载不下来,大家可以到这个链接 http://dl.smartyhero.com/lnmp/ 进行下载
基础软件包安装
yum -y install vim wget nc tree zip bzip2 unzip git ntpdate tcpdump traceroute bc net-tools iotop iftop gcc gcc-c++ bash-completion git sysstat
NGINX安装
tar xf nginx-1.16.1.tar.gz
tar xf OpenSSL_1_1_1d.tar.gz
tar xf zlib-1.2.11.tar.xz
tar xf pcre-8.43.tar.bz2
cd nginx-1.16.1
useradd -s /sbin/nologin -r nginx
./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-select_module \
--with-poll_module \
--with-threads \
--with-file-aio \
--with-http_ssl_module \
--with-http_sub_module \
--with-http_dav_module \
--with-http_v2_module \
--with-http_mp4_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_auth_request_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-stream \
--with-stream_ssl_module \
--with-stream_realip_module \
--with-stream_ssl_preread_module \
--with-http_stub_status_module \
--with-pcre-jit \
--with-openssl=../openssl-OpenSSL_1_1_1d \
--with-pcre=../pcre-8.43 \
--with-zlib=../zlib-1.2.11
make && make install
cp -r contrib/vim/* /usr/share/vim/vim74/
echo 'alias nginx=/usr/local/nginx/sbin/nginx' >>~/.bashrc
. ~/.bashrc
注意: --with-openssl这些指向的是其源码而不是安装目录
MariaDB安装
-
环境准备
yum install -y libaio-devel useradd -s /sbin/nologin -r mysql # 创建MariaDB使用的账号 mkdir -pv /data/MariaDB_data # 创建数据目录
-
安装
cd /usr/local/src/ tar xf mariadb-10.4.12-linux-systemd-x86_64.tar.gz mv mariadb-10.4.12-linux-systemd-x86_64 /usr/local/mysql cd /usr/local/mysql scripts/mariadb-install-db --user=mysql --datadir=/data/MariaDB_data # 初始化数据库 指定用户名和数据目录 mkdir var # 创建pid使用的目录 chown mysql. var cp support-files/mysql.server /etc/init.d/ # 配置启动脚本 vim /etc/init.d/mysql.server # 编辑启动脚本并修改以下内容 echo 'export PATH=/usr/local/mysql/bin:$PATH' >>/etc/profile.d/env.sh # 将mysql相关命令加入环境变量 . /etc/profile.d/env.sh
-
创建启动脚本
cd /usr/local/mysql mkdir var # 创建pid使用的目录 chown mysql. var cp support-files/mysql.server /etc/init.d/ # 配置启动脚本
修改启动脚本
vim /etc/init.d/mysql.server # 编辑启动脚本并修改以下内容
basedir=/usr/local/mysql datadir=/data/MariaDB_data mysqld_pid_file_path=/usr/local/mysql/var/mysqld.pid
-
配置文件
在新版本的MariaDB二进制包中没有包含配置文件了,需要自己准备一个,如下示例
mkdir /data/MariaDB_binlog # 创建binlog日志目录 chown mysql. /data/MariaDB_binlog vim /etc/my.cnf
[client] port = 3306 socket = /tmp/mysql.sock [mysqld] bind-address = 127.0.0.1 port = 3306 socket = /tmp/mysql.sock datadir = /data/MariaDB_data innodb_file_per_table skip-external-locking key_buffer_size = 384M max_allowed_packet = 64M table_open_cache = 512 sort_buffer_size = 2M read_buffer_size = 2M read_rnd_buffer_size = 8M myisam_sort_buffer_size = 64M thread_cache_size = 8 query_cache_size = 32M thread_concurrency = 8 log-bin=/data/MariaDB_binlog/mysql-bin server-id = 1 [mysqldump] quick max_allowed_packet = 16M [mysql] no-auto-rehash [myisamchk] key_buffer_size = 256M sort_buffer_size = 256M read_buffer = 2M write_buffer = 2M [mysqlhotcopy] interactive-timeout
-
启动服务,并做安全初始化
/etc/init.d/mysql.server start mysql_secure_installation # 安全初始化脚本,是一个交互式的脚本,可以在这里设置mysql的root用户的密码,然后其他的直接回车就可以了,会删除匿名用户等 mysql -uroot -p # 输入刚才设置的密码就可以登录数据库了
-
导出库文件,头文件,帮助文档
ln -sv /usr/local/mysql/include/mysql/ /usr/include/ echo "/usr/local/mysql/lib" >> /etc/ld.so.conf.d/mysql.conf ldconfig vim /etc/man.config MANPATH /usr/local/mysql/man/
PHP安装
依赖编译
-
OpenSSL
cd /usr/local/src/openssl-OpenSSL_1_1_1d make clean ./config shared --prefix=/usr/local/openssl make && make install echo /usr/local/openssl/lib/ >> /etc/ld.so.conf.d/openssl.conf ldconfig ln -sv /usr/local/openssl/include/openssl/ /usr/local/include/
-
libzip
这个要稍微说一下,因为centos7的yum仓库提供的libzip的版本是0.10的,而php7.3需要的版本最低是0.11的,因此我们需要自己编译libzip,而最新版本的libzip是通过cmake编译的,centos7的cmake版本还是比较低,因此我们这里找了一个在使用cmake编译之前的版本进行的编译
cd /usr/local/src tar xf libzip-1.3.1.tar.xz cd libzip-1.3.1 ./configure --prefix=/usr/local/libzip make && make install echo /usr/local/libzip/lib/ >> /etc/ld.so.conf.d/libzip.conf
php安装
cd /usr/local/src/php-7.3.15
./configure \
--prefix=/usr/local/php \
--with-mysql-sock=/tmp/mysql.sock \
--with-openssl \
--enable-mbstring \
--with-freetype-dir \
--with-jpeg-dir \
--with-png-dir \
--with-zlib \
--with-libxml-dir=/usr \
--enable-xml \
--with-gettext \
--enable-bcmath \
--enable-sockets \
--with-config-file-path=/etc \
--with-config-file-scan-dir=/etc/php.d \
--enable-exif \
--with-bz2 \
--with-curl \
--enable-zip \
--with-libzip=/usr/local/libzip \
--with-openssl=/usr/local/openssl/ \
--with-gd \
--enable-fpm \
--with-pdo-mysql \
--with-mysqli \
--with-fpm-user=nginx \
--with-fpm-group=nginx \
--enable-opcache \
--enable-opcache-file
make && make install
cp php.ini-production /etc/php.ini
cp sapi/fpm/php-fpm.service /etc/systemd/system/
cd /usr/local/php/etc/
cp php-fpm.conf.default php-fpm.conf
cp php-fpm.d/www.conf.default php-fpm.d/www.conf
systemctl daemon-reload
systemctl start php-fpm
systemctl enable php-fpm
```
配置nginx转发动态请求到php-fpm
vim /usr/local/nginx/conf/nginx.conf
将以下内容加入到server块中
location ~ \.php$ {
try_files $uri =404;
fastcgi_index index.php;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
nginx -t # 测试配置文件是否有问题
nginx # 启动nginx
nginx -s reload # 启动nginx
下面分享一下个人的一个nginx配置
user nginx;
worker_processes auto;
error_log logs/error.log warn;
worker_rlimit_nofile 65535;
timer_resolution 1000ms;
events {
use epoll;
multi_accept on;
worker_connections 65535;
}
http {
include mime.types;
default_type text/html;
log_not_found off;
log_format main '$remote_addr - $remote_user [$time_iso8601] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
server_tokens off;
client_max_body_size 32m;
charset utf-8;
gzip on;
gzip_buffers 6 64k;
gzip_comp_level 6;
gzip_http_version 1.1;
gzip_min_length 1k;
gzip_types text/plain text/css text/javascript application/x-javascript application/xml application/json image/svg+xml;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 60;
server {
listen 80 default;
access_log logs/access.log main;
root /usr/local/nginx/html;
location / {
index index.html index.htm;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_index index.php;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
编写t.php文件进行测试
vim /usr/local/nginx/html/t.php
<?php
phpinfO();
?>
在浏览器输入http://your_ip/t.php正常情况下会显示出phpinfo的页面
对PHP的几点配置
vim /etc/php.ini
expose_php = Off
date.timezone = Asia/Shanghai
为php-fpm开启opcache缓存
vim /etc/php.ini
zend_extension=opcache.so
[opcache]
opcache.enable=1
opcache.enable_cli=0
opcache.memory_consumption=128
opcache.interned_strings_buffer=64
opcache.max_accelerated_files=1000000
opcache.max_wasted_percentage=99
opcache.use_cwd=1
opcache.validata_timestamps=0
opcache.revalidate_path=0
opcache.save_comments=0
opcache.enable_file_override=0
opcache.dups_fix=1
opcache.max_file_size=0
opcache.consistency_checks=0
opcache.error_log=/tmp/opcache.log
opcahce.log_verbosity_level=4
systemctl restart php-fpm
向PHP添加扩展
首先需要安装autoconf
yum -y install autoconf
PHP自带的的扩展的源码都在源码目录中的ext
目录下
下面通过安装swoole扩展做一个演示
cd /usr/local/src/
wget https://github.com/swoole/swoole-src/archive/v4.4.16.tar.gz
tar xf v4.4.16.tar.gz
cd swoole-src-4.4.16
/usr/local/php/bin/phpize # 生成configure脚本
./configure \
--with-php-config=/usr/local/php/bin/php-config \
--enable-sockets \
--enable-http2 \
--enable-openssl \
--enable-swoole \
--enable-mysqlnd \
--with-openssl-dir=/usr/local/openssl \
--enable-gcov
make && make install
修改配置文件/etc/php.ini
加入:
extension=swoole.so
而后可以验证,重启使其生效
/usr/local/php/bin/php -m
systemctl restart php-fpm
需要安装扩展时可按照上述步骤进行安装,
至此,LNMP整体的搭建就到这里
文章评论(0)