第一部分:LNMP环境搭建
一、环境说明:
OS: centos7.6_x64
nginx:nginx-1.16.0
php: php-7.1.11
mysql:mysql-5.6.44
zabbix:zabbix-4.0.10
二、安装前准备:
2.1 准备yum源
# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
2.2 安装依赖关系
# yum -y install gcc automake autoconf libtool make cmake gcc-c++ glibc pcre pcre-devel zlib zlib-devel openssl openssl-devel
3.3 创建目录
# mkdir /data
二、nginx安装
2.1 建立用户
# groupadd -r nginx
2.2 下载安装包
# wget http://nginx.org/download/nginx-1.16.0.tar.gz
2.3 编译安装
# tar –zxvf nginx-1.16.0.tar.gz
./configure --prefix=/data/server/nginx-1.16.0 \
--sbin-path=/data/server/nginx-1.16.0/sbin/nginx \
--conf-path=/data/server/nginx-1.16.0/nginx.conf \
--pid-path=/data/server/nginx-1.16.0/nginx.pid \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--http-proxy-temp-path=/var/tmp/nginx/proxy/ \
--http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \
--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \
--http-scgi-temp-path=/var/tmp/nginx/scgi \
--with-select_module \
--with-poll_module \
--error-log-path=/data/logs/nginx/error.log \
--http-log-path=/data/logs/nginx/access.log \
--with-pcre
2.4 建立软链接
方便以后升级,建立软链接
# ln -sv /data/server/nginx-1.16.0 /data/server/nginx
2.5 为nginx提供SysV init启动脚本
新建文件/etc/rc.d/init.d/nginx,内容如下:
#!/bin/sh
. /etc/rc.d/init.d/functions
. /etc/sysconfig/network
[ "$NETWORKING" = "no" ] && exit 0
nginx="/data/server/nginx/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/data/server/nginx/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx
make_dirs() {
# make required directories
user=`nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
options=`$nginx -V 2>&1 | grep 'configure arguments:'`
for opt in $options; do
if [ `echo $opt | grep '.*-temp-path'` ]; then
value=`echo $opt | cut -d "=" -f 2`
if [ ! -d "$value" ]; then
# echo "creating" $value
mkdir -p $value && chown -R $user $value
fi
fi
done
}
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
make_dirs
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
configtest || return $?
stop
sleep 1
start
}
reload() {
configtest || return $?
echo -n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}
force_reload() {
restart
}
configtest() {
$nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac
2.6 为此脚本赋予执行权限
# chmod +x /etc/rc.d/init.d/nginx
2.7 添加至服务管理列表,并让其开机自动启动:
# chkconfig --add nginx
2.8 启动nginx服务
# service nginx start
2.9 配置PATH环境变量
# echo 'export PATH=$PATH://data/server/nginx/sbin/' > /etc/profile.d/nginx.sh
2.10 配置nginx
编辑nginx主配置文件,如下,
# vim /data/server/nginx/nginx.conf
user nginx nginx;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
log\_format main '$remote\_addr - $remote\_user \[$time\_local\] "$request" '
'$status $body\_bytes\_sent "$http\_referer" '
'"$http\_user\_agent" "$http\_x\_forwarded\_for"';
sendfile on;
tcp\_nopush on;
keepalive\_timeout 65;
gzip on;
gzip\_min\_length 1k;
gzip\_buffers 4 16k;
gzip\_http\_version 1.0;
gzip\_comp\_level 2;
gzip\_types text/plain application/x-javascript text/css application/xml;
gzip\_vary on;
tcp\_nodelay on;
fastcgi\_connect\_timeout 300;
fastcgi\_send\_timeout 300;
fastcgi\_read\_timeout 300;
fastcgi\_buffer\_size 64k;
fastcgi\_buffers 4 64k;
fastcgi\_busy\_buffers\_size 128k;
fastcgi\_temp\_file\_write\_size 128k;
include /data/server/nginx/conf.d/\*.conf;
}
# 建立一个虚拟主机
server {
listen 80;
server_name localhost;
index index.html;
root /data/webapps;
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 1h;
}
access_log /data/logs/nginx/access/default.log main;
error_log /data/logs/nginx/error/default.log;
#伪静态规则
include /data/server/nginx/conf.d/rewrite/default.conf;
}
rewrite ^(.*)-htm-(.*)$ $1.php?$2 last;
rewrite ^(.*)/simple/([a-z0-9\_]+\.html)$ $1/simple/index.php?$2 last;
rewrite ^(.*)/data/(.*)\.(htm|php)$ 404.html last;
rewrite ^(.*)/attachment/(.*)\.(htm|php)$ 404.html last;
rewrite ^(.*)/html/(.*)\.(htm|php)$ 404.html last;
三、安装mysql
3.1 先检查是否安装了其它版本的mysql或mariadb
# rpm -qa|grep mariadb
mariadb-libs-5.5.52-1.el7.x86_64
3.2 建立mysql用户及组
# groupadd mysql
3.3 下载并安装mysql
# wget https://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.44-linux-glibc2.12-x86_64.tar.gz
3.4 配置环境变量
# echo "export PATH=$PATH:/data/server/mysql/bin" > /etc/profile.d/mysql.sh
3.5 编辑mysql配置文件,如下:
# vim /etc/my.cnf
[mysqld]
basedir=/data/server/mysql
datadir=/data/mysql
socket=/data/server/mysql/mysql.sock
log-error=/data/logs/mysql/error.log
pid-file=/data/logs/mysql/mysql.pid
symbolic-links=0
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
default-storage-engine = innodb
innodb_file_per_table
collation-server = utf8_general_ci
init-connect = 'SET NAMES utf8'
character-set-server = utf8
max_connections=1000
max_connect_errors=10000
thread_cache_size=100
thread_stack=192K
ssl=false
max_allowed_packet=60M
max_heap_table_size=64M
max_length_for_sort_data=8M
net_buffer_length=65536
skip-name-resolve ##ip/localhost
skip-external-locking ##system lock
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
[client]
socket=/data/server/mysql/mysql.sock
[mysql]
socket=/data/server/mysql/mysql.sock
3.6 准备启动脚本
# cp /data/server/mysql/support-files/mysql.server /etc/init.d/mysqld
…………
basedir=/data/server/mysql
datadir=/data/mysql
…………
# 添加服务
# chkconfig --add mysqld
3.7 初始化mysql
# mysql_install_db --user=mysql --basedir=/data/server/mysql/ --datadir=/data/mysql
3.8 启动mysql并设置开机自启动
# service enable mysqld && service sttart mysqld
mysql> delete from mysql.user where user = '' or host = '::1';
3.8 为了使用mysql的安装符合系统使用规范,并将其开发组件导出给系统使用,这里还需要进行如下步骤:
输出mysql的man手册至man命令的查找路径:
编辑/etc/man.config,添加如下行即可:
输出mysql的头文件至系统头文件路径/usr/include:
这可以通过简单的创建链接实现:
输出mysql的库文件给系统库查找路径:
而后让系统重新载入系统库:
四、安装php
4.1 下载php
# wget http://cn2.php.net/distributions/php-7.1.11.tar.gz
4.2 安装依赖扩展包
# yum -y install libmcrypt libmcrypt-devel mhash mhash-devel mcrypt libxml2-devel bzip2-devel libcurl-devel libjpeg-devel libpng libpng-devel freetype freetype-devel libmcrypt libmcrypt-devel
4.3 编译安装php
# tar xf php-7.1.11.tar.gz
--with-config-file-path=/data/server/php-7.1.11/etc \
--enable-fpm \
--with-mcrypt \
--enable-mbstring \
--enable-pdo \
--with-curl \
--disable-debug \
--disable-rpath \
--enable-inline-optimization \
--with-bz2 \
--with-zlib \
--enable-sockets \
--enable-sysvsem \
--enable-sysvshm \
--enable-pcntl \
--enable-mbregex \
--with-mhash \
--enable-zip \
--with-pcre-regex \
--with-mysqli=/data/server/mysql/bin/mysql_config \
--with-gd \
--with-jpeg-dir \
--with-freetype-dir \
--with-png-dir \
--enable-calendar \
--with-openssl \
--with-bcmath \
--with-gettext
4.4 准备php配置文件
# cp php.ini-production /data/server/php/etc/php.ini
4.5 为php-fpm提供配置文件:
# cp /data/server/php/etc/php-fpm.conf.default /data/server/php/etc/php-fpm.conf
4.6 编辑php-fpm的配置文件:
# vim /data/server/php/etc/php-fpm.conf
取消pid的注释
……
[global]
pid = run/php-fpm.pid
……
4.7 设置环境变量
# echo 'export PATH=$PATH:/data/server/php/sbin' > /etc/profile.d/php-fpm.sh
4.8 为php-fpm提供Sysv init脚本,并将其添加至服务列表:
# cp sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm
接下来就可以启动php-fpm了:
五、整合nginx和php5
5.1 编辑nginx.conf,启用如下选项:
# vi /data/server/nginx/nginx.conf
location ~ \.(php|php5)$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi_params;
}
5.2 编辑/data/server/nginx/fastcgi_params,将其内容更改为如下内容:
# cp fastcgi_params{,.bak}
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
并在所支持的主页面格式中添加php格式的主页,类似如下:
location / {
root html;
index index.php index.html index.htm;
}
5.3 完整的配置文件如下:
# cat /data/server/nginx/conf.d/default.conf
server {
listen 80;
server_name localhost;
index index.php index.html index.htm;
root /data/webapps;
location ~ \.(php|php5)$ {
root /data/webapps;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi_params;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 1h;
}
access_log /data/logs/nginx/access/default.log main;
error_log /data/logs/nginx/error/default.log;
}
而后重新载入nginx的配置文件:
# service nginx reload
5.4 在/data/webapps新建index.php的测试页面,测试php是否能正常工作及能否连接数据库:
mysql> create user 'test'@'localhost' identified by 'test123';
mysql> flush privileges;
接着就可以通过浏览器访问此测试页面了。
六、安装php扩展,为php
xcache不支持php7.1了,使用php原生的opcache代替
# vim /data/server/php/etc/php.ini
zend_extension=opcache.so
[opcache]
;开启opcache
opcache.enable=1
;CLI环境下,PHP启用OPcache
opcache.enable_cli=1
;OPcache共享内存存储大小,单位MB
opcache.memory_consumption=128
;PHP使用了一种叫做字符串驻留(string interning)的技术来改善性能。例如,如果你在代码中使用了1000次字符串“foobar”,在PHP内部只会在第一使用这个字符串的时候分配一个不可变的内存区域来存储这个字符串,其他的999次使用都会直接指向这个内存区域。这个选项则会把这个特性提升一个层次——默认情况下这个不可变的内存区域只会存在于单个php-fpm的进程中,如果设置了这个选项,那么它将会在所有的php-fpm进程中共享。在比较大的应用中,这可以非常有效地节约内存,提高应用的性能。
这个选项的值是以兆字节(megabytes)作为单位,如果把它设置为16,则表示16MB,默认是4MB
opcache.interned_strings_buffer=8
;这个选项用于控制内存中最多可以缓存多少个PHP文件。这个选项必须得设置得足够大,大于你的项目中的所有PHP文件的总和。
设置值取值范围最小值是 200,最大值在 PHP 5.5.6 之前是 100000,PHP 5.5.6 及之后是 1000000。也就是说在200到1000000之间。
opcache.max_accelerated_files=4000
;设置缓存的过期时间(单位是秒),为0的话每次都要检查
opcache.revalidate_freq=60
;从字面上理解就是“允许更快速关闭”。它的作用是在单个请求结束时提供一种更快速的机制来调用代码中的析构器,从而加快PHP的响应速度和PHP进程资源的回收速度,这样应用程序可以更快速地响应下一个请求。把它设置为1就可以使用这个机制了。
opcache.fast_shutdown=1
;如果启用(设置为1),OPcache会在opcache.revalidate_freq设置的秒数去检测文件的时间戳(timestamp)检查脚本是否更新。
如果这个选项被禁用(设置为0),opcache.revalidate_freq会被忽略,PHP文件永远不会被检查。这意味着如果你修改了你的代码,然后你把它更新到服务器上,再在浏览器上请求更新的代码对应的功能,你会看不到更新的效果
强烈建议你在生产环境中设置为0,更新代码后,再平滑重启PHP和web服务器。
opcache.validate_timestamps=0
;开启Opcache File Cache(实验性), 通过开启这个, 我们可以让Opcache把opcode缓存缓存到外部文件中, 对于一些脚本, 会有很明显的性能提升.
这样PHP就会在/tmp目录下Cache一些Opcode的二进制导出文件, 可以跨PHP生命周期存在.
opcache.file_cache=/tmp
6.2 重新启动php-fpm
# service php-fpm restart
七、如何安装第三方php扩展模块
7.1 安装在源码包中带的扩展模块bcmatch
# cd /tmp/lnmp/php-7.1.11/ext/bcmath/
安装完成的模块在下面的目录下
Installing shared extensions: /data/server/php-7.1.11/lib/php/extensions/no-debug-non-zts-20160303/
# ls -lhrt /data/server/php-7.1.11/lib/php/extensions/no-debug-non-zts-20160303/
total 5.2M
-rwxr-xr-x 1 root root 3.3M Jul 16 14:11 opcache.a
-rwxr-xr-x 1 root root 1.6M Jul 16 14:11 opcache.so
-rwxr-xr-x 1 root root 357K Jul 16 16:56 bcmath.so
-rwxr-xr-x 1 root root 52K Jul 16 20:07 gettext.so
7.2 编辑配置文件,启用
# vim /data/server/php-7.1.11/etc/php.ini
[bcmath]
extension=bcmath.so
第二部分:zabbix安装
1、安装Zabbix Server
1.1 先安装依赖包
# yum -y install net-snmp gcc mysql-devel libxml2-devel net-snmp-devel libevent-devel curl-devel
1.2 下载软件包
1.3 创建用户
# groupadd --system zabbix
1.4 创建数据库
mysql> create database zabbix character set utf8 collate utf8_bin;
mysql> grant all privileges on zabbix.* to zabbix@localhost identified by 'zabbix';
1.5 导入数据库
# cd database/mysql
1.6 编译安装
# tar -zxvf zabbix-4.0.10.tar.gz
./configure --prefix=/data/server/zabbix-4.0.10 \
--enable-server \
--enable-agent \
--with-mysql \
--enable-ipv6 \
--with-net-snmp \
--with-libcurl \
--with-libxml2
1.7 配置环境变量
# echo 'export PATH=$PATH:/data/server/zabbix/sbin' > /etc/profile.d/zabbix.sh
1.8 编辑zabbix_server.conf
# vim /data/server/zabbix/etc/zabbix_server.conf
DBPassword=zabbix
1.9 安装zabbix
拷贝网页文件至web服务器目录下
# mkdir /data/webapps/zabbix
1.10 编辑php配置,以满足zabbix要求
# vim /data/server/php/etc/php.ini
post_max_size = 16M
max_execution_time = 300
max_input_time = 300
1.11 准备zabbix server的启动脚本
# vim /etc/rc.d/init.d/zabbix_server
#!/bin/sh
. /etc/rc.d/init.d/functions
. /etc/sysconfig/network
[ "$NETWORKING" = "no" ] && exit 0
zabbix_server="/data/server/zabbix/sbin/zabbix_server"
prog=$(basename $zabbix_server)
ZABBIX_SERVER_CONF_FILE="/data/server/zabbix/etc/zabbix_server.conf"
lockfile=/var/lock/subsys/zabbix_server
start() {
[ -x $zabbix_server ] || exit 5
[ -f $ZABBIX_SERVER_CONF_FILE ] || exit 6
echo -n $"Starting $prog: "
daemon $zabbix_server -c $ZABBIX_SERVER_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
stop
sleep 1
start
}
rh_status() {
status $prog
}
case "$1" in
start)
rh_status && exit 0
$1
;;
stop)
rh_status || exit 0
$1
;;
restart)
$1
;;
status)
rh_status
;;
*)
echo $"Usage: $0 {start|stop|status|restart}"
exit 2
esac
1.12 启动zabbix_server
加入系统自启动
# service zabbix_server start
2、安装zabbix agent
2.1 编译安装agent
# ./configure --prefix=/data/server/zabbix-4.0.10 \
--enable-agent \
--with-mysql \
--enable-ipv6 \
--with-net-snmp \
--with-libcurl \
--with-libxml2
2.2 编辑配置文件
# vim /data/server/zabbix/etc/zabbix_agentd.conf
Server=127.0.0.1
ServerActive=127.0.0.1
Hostname=Zabbix server
2.3 准备zabbix_agentd启动脚本
# vim /etc/rc.d/init.d/zabbix_agentd
#!/bin/sh
. /etc/rc.d/init.d/functions
. /etc/sysconfig/network
[ "$NETWORKING" = "no" ] && exit 0
zabbix_agentd="/data/server/zabbix/sbin/zabbix_agentd"
prog=$(basename $zabbix_agentd)
ZABBIX_AGENTD_CONF_FILE="/data/server/zabbix/etc/zabbix_agentd.conf"
lockfile=/var/lock/subsys/zabbix_agentd
start() {
[ -x $zabbix_agentd ] || exit 5
[ -f $ZABBIX_AGENTD_CONF_FILE ] || exit 6
echo -n $"Starting $prog: "
daemon $zabbix_agentd -c $ZABBIX_AGENTD_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
stop
sleep 1
start
}
rh_status() {
status $prog
}
case "$1" in
start)
rh_status && exit 0
$1
;;
stop)
rh_status || exit 0
$1
;;
restart)
$1
;;
status)
rh_status
;;
*)
echo $"Usage: $0 {start|stop|status|restart}"
exit 2
esac
2.4 启动zabbix_agent
加入系统自启动
参考链接:https://www.zabbix.com/documentation/4.0/manual/installation/install
手机扫一扫
移动阅读更方便
你可能感兴趣的文章