两台主机搭建lnmp服务
lnmp服务
准备环境
| 主机ip | 安装软件 |
|---|---|
| 192,168,233,10 | mysql php zabbix |
| 192.168.233.20 | nginx |
1.在主机192,168,233,10上源码安装mysql php
[[email protected] ~]# yum -y install ncurses-devel openssl-devel openssl cmake mariadb-devel
//创建用户和组
[[email protected] src]# groupadd -r -g 306 mysql
[[email protected] src]# useradd -M -s /sbin/nologin -g 306 -u 306 mysql
//解压软件至/usr/local/
[[email protected] src]# ls
debug kernels mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
[[email protected] src]# tar xf mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[[email protected] ~]# ls /usr/local/
bin games lib libexec sbin src
etc include lib64 mysql-5.7.22-linux-glibc2.12-x86_64 share
//下载二进制格式的mysql软件包
[[email protected] ~]# cd /usr/local
[[email protected] local]# ln -sv mysql-5.7.23-linux-glibc2.12-x86_64/ mysql
‘mysql’ -> ‘mysql-5.7.23-linux-glibc2.12-x86_64/’
[[email protected] local]# ll
total 0
drwxr-xr-x. 13 root root 152 Feb 20 01:39 apache
drwxr-xr-x. 6 root root 58 Feb 20 01:27 apr
drwxr-xr-x. 5 root root 43 Feb 20 01:29 apr-util
drwxr-xr-x. 2 root root 6 Mar 10 2016 bin
drwxr-xr-x. 2 root root 6 Mar 10 2016 etc
drwxr-xr-x. 2 root root 6 Mar 10 2016 games
drwxr-xr-x. 2 root root 6 Mar 10 2016 include
drwxr-xr-x. 2 root root 6 Mar 10 2016 lib
drwxr-xr-x. 2 root root 6 Mar 10 2016 lib64
drwxr-xr-x. 2 root root 6 Mar 10 2016 libexec
lrwxrwxrwx. 1 root root 36 Feb 20 01:48 mysql -> mysql-5.7.23-linux-glibc2.12-x86_64/
drwxr-xr-x. 9 root root 129 Feb 20 01:47 mysql-5.7.23-linux-glibc2.12-x86_64
drwxr-xr-x. 2 root root 6 Mar 10 2016 sbin
drwxr-xr-x. 5 root root 49 Feb 18 03:59 share
drwxr-xr-x. 2 root root 6 Mar 10 2016 src
//修改目录/usr/local/mysql的属主属组
[[email protected] local]# chown -R mysql.mysql /usr/local/mysql
[[email protected] local]# ll /usr/local/mysql -d
lrwxrwxrwx. 1 mysql mysql 36 Feb 20 01:48 /usr/local/mysql -> mysql-5.7.23-linux-glibc2.12-x86_64/
//添加环境变量
[[email protected] ~]# ls /usr/local/mysql
bin COPYING docs include lib man README share support-files
[[email protected] ~]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[[email protected] ~]# . /etc/profile.d/mysql.sh
[[email protected] ~]# echo $PATH
/usr/local/mysql/bin:/usr/local/apache/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
//建立数据存放目录
[[email protected] ~]# mkdir /opt/data
[[email protected] ~]# chown -R mysql.mysql /opt/data/
[[email protected] ~]# ll /opt/
total 0
drwxr-xr-x. 2 mysql mysql 6 Feb 20 02:04 data
//初始化数据库
[ro[email protected] ~]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/opt/data/
2019-02-20T07:05:14.920035Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2019-02-20T07:05:19.027724Z 0 [Warning] InnoDB: New log files created, LSN=45790
2019-02-20T07:05:20.108182Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2019-02-20T07:05:20.536652Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: e2955a52-34dd-11e9-a0fd-000c29c6e1a8.
2019-02-20T07:05:20.537729Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2019-02-20T07:05:20.538850Z 1 [Note] A temporary password is generated for [email protected]: BcJ5cCpyrr;t
//请注意,这个命令的最后会生成一个临时密码,此处密码是BcJ5cCpyrr;t
//再次注意,这个密码是随机的,你的不会跟我一样,一定要记住这个密码,因为一会登录时会用到
//配置mysql
[[email protected] ~]# ln -sv /usr/local/mysql/include/ /usr/local/include/mysql
‘/usr/local/include/mysql’ -> ‘/usr/local/mysql/include/’
[[email protected] ~]# echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
[[email protected] ~]# ldconfig -v
ldconfig: Can't stat /libx32: No such file or directory
ldconfig: Path `/usr/lib' given more than once
ldconfig: Path `/usr/lib64' given more than once
ldconfig: Can't stat /usr/libx32: No such file or directory
/usr/lib64/mysql:
libmysqlclient.so.18 -> libmysqlclient_r.so
/usr/local/mysql/lib:
libmysqlclient.so.20 -> libmysqlclient.so.20.3.10
/lib:
/lib64:
libarchive.so.13 -> libarchive.so.13.1.2
libkadm5srv_mit.so.11 -> libkadm5srv_mit.so.11.0
libmpc.so.3 -> libmpc.so.3.0.0
...省略
/lib/sse2: (hwcap: 0x0000000004000000)
/lib64/sse2: (hwcap: 0x0000000004000000)
/lib64/tls: (hwcap: 0x8000000000000000)
[[email protected] ~]# ldconfig -p |grep mysql
libmysqlclient.so.20 (libc6,x86-64) => /usr/local/mysql/lib/libmysqlclient.so.20
libmysqlclient.so.18 (libc6,x86-64) => /usr/lib64/mysql/libmysqlclient.so.18
libmysqlclient.so (libc6,x86-64) => /usr/lib64/mysql/libmysqlclient.so
libmysqlclient.so (libc6,x86-64) => /usr/local/mysql/lib/libmysqlclient.so
//启动mysql
[[email protected] ~]# service mysqld start
Starting MySQL.. SUCCESS!
[[email protected] ~]# ps -ef|grep mysql
root 37808 1 0 02:59 pts/1 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/opt/data --pid-file=/opt/data/mysql.pid
mysql 37988 37808 0 02:59 pts/1 00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/opt/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=localhost.localdomain.err --pid-file=/opt/data/mysql.pid --socket=/tmp/mysql.sock --port=3306
root 38018 3254 0 02:59 pts/1 00:00:00 grep --color=auto mysql
[[email protected] ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 :::80 :::*
LISTEN 0 32 :::21 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
LISTEN 0 80 :::3306
修改密码
[[email protected] ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.23
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
设置新密码
mysql> set password = password('gaosihan123!');
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> quit
Bye
3.3 安装php
//安装依赖包
[[email protected] ~]# yum -y install libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libicu-devel libjpeg libjpeg-devel libpng libpng-devel openldap-devel libpcre-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel mhash mhash-devel php72w-mysqlnd
//编译安装php
[[email protected] src]# tar xf php-7.2.8.tar.xz
[[email protected] src]# cd php-7.2.8
[[email protected] php-7.2.8]# ./configure --prefix=/usr/local/php7 \
--with-config-file-path=/etc \
--enable-fpm \
--enable-inline-optimization \
--disable-debug \
--disable-rpath \
--enable-shared \
--enable-soap \
--with-openssl \
--enable-bcmath \
--with-iconv \
--with-bz2 \
--enable-calendar \
--with-curl \
--enable-exif \
--enable-ftp \
--with-gd \
--with-jpeg-dir \
--with-png-dir \
--with-zlib-dir \
--with-freetype-dir \
--with-gettext \
--enable-json \
--enable-mbstring \
--enable-pdo \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-readline \
--enable-shmop \
--enable-simplexml \
--enable-sockets \
--enable-zip \
--enable-mysqlnd-compression-support \
--with-pear \
--enable-pcntl \
--enable-posix
[[email protected] php-7.2.8]# make -j $(cat /proc/cpuinfo |grep processor|wc -l)
编译过程略
[[email protected] php-7.2.8]# make install
安装过程略
//安装后配置
[[email protected] ~]# echo 'export PATH=/usr/local/php7/bin:$PATH' > /etc/profile.d/php7.sh
[[email protected] ~]# source /etc/profile.d/php7.sh
[[email protected] php-7.2.8]# which php
/usr/local/php7/bin/php
[[email protected] php-7.2.8]# php -v
PHP 7.2.8 (cli) (built: Feb 20 2019 03:59:53) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
//配置php-fpm
[[email protected] php-7.2.8]# cp php.ini-production /etc/php.ini
[[email protected] php-7.2.8]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[[email protected] php-7.2.8]# chmod +x /etc/rc.d/init.d/php-fpm
[[email protected] php-7.2.8]# cp /usr/local/php7/etc/php-fpm.conf.default /usr/local/php7/etc/php-fpm.conf
[[email protected] php-7.2.8]# cp /usr/local/php7/etc/php-fpm.d/www.conf.default /usr/local/php7/etc/php-fpm.d/www.conf
//编辑php-fpm的配置文件(/usr/local/php7/etc/php-fpm.conf):
//配置fpm的相关选项为你所需要的值:
[[email protected] ~]# vim /usr/local/php7/etc/php-fpm.conf
.....
.....
pm.max_children = 50 //最多同时提供50个进程提供50个并发服务
pm.start_servers = 5 //启动时启动5个进程
pm.min_spare_servers = 2 //最小空闲进程数
pm.max_spare_servers = 8 //最大空闲进程数
[[email protected] ~]# tail /usr/local/php7/etc/php-fpm.conf
; - the global prefix if it's been set (-p argument)
; - /usr/local/php7 otherwise
include=/usr/local/php7/etc/php-fpm.d/*.conf
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 2
pm.max_spare_servers = 8
//启动php-fpm
[[email protected] ~]# service php-fpm start
Starting php-fpm done
//默认情况下,fpm监听在127.0.0.1的9000端口,也可以使用如下命令验证其是否已经监听在相应的套接字
[[email protected] ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 127.0.0.1:9000 *:*
LISTEN 0 128 :::80 :::*
LISTEN 0 32 :::21 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
LISTEN 0 80 :::3306
[[email protected] ~]# ps -ef | grep php
root 55504 3254 0 03:32 pts/1 00:00:01 /usr/bin/python /usr/bin/yum -y install libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libicu-devel libjpeg libjpeg-devel libpng libpng-devel openldap-devel libpcre-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel mhash mhash-devel php72w-mysqlnd
root 73530 1 0 04:08 ? 00:00:00 php-fpm: master process (/usr/local/php7/etc/php-fpm.conf)
nobody 73531 73530 0 04:08 ? 00:00:00 php-fpm: pool www
nobody 73532 73530 0 04:08 ? 00:00:00 php-fpm: pool www
nobody 73533 73530 0 04:08 ? 00:00:00 php-fpm: pool www
nobody 73534 73530 0 04:08 ? 00:00:00 php-fpm: pool www
nobody 73535 73530 0 04:08 ? 00:00:00 php-fpm: pool www
root 73538 3254 0 04:09 pts/1 00:00:00 grep --color=auto php
安装zabbix
[[email protected] ~]# yum -y install net-snmp-devel libevent-devel
安装过程略…
//下载zabbix
[[email protected] ~]# cd /usr/src/
[[email protected] src]# wget https://nchc.dl.sourceforge.net/project/zabbix/ZABBIX Latest Stable/4.0.3/zabbix-4.0.3.tar.gz
下载过程略…
//解压
[[email protected] src]# tar xf zabbix-3.4.12.tar.gz
//创建zabbix用户和组
[[email protected] ~]# groupadd -r zabbix
[[email protected] ~]# useradd -r -g zabbix -M -s /sbin/nologin zabbix
//配置zabbix数据库
[[email protected] mysql]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.22 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> create database zabbix character set utf8 collate utf8_bin;
Query OK, 1 row affected (0.00 sec)
mysql> grant all privileges on zabbix.* to [email protected] identified by 'zabbix123!';
Query OK, 0 rows affected, 2 warnings (0.29 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
mysql> quit
Bye
[[email protected] ~]# cd /usr/src/zabbix-4.0.3/database/mysql/
[[email protected] mysql]# ls
data.sql images.sql schema.sql
[[email protected] mysql]# mysql -uzabbix -pzabbix123! zabbix < schema.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
[[email protected] mysql]# mysql -uzabbix -pzabbix123! zabbix < images.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
[[email protected] mysql]# mysql -uzabbix -pzabbix123! zabbix < data.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
//编译安装zabbix
[[email protected] ~]# cd /usr/src/zabbix-3.4.12
[[email protected] zabbix-3.4.12]# ./configure --enable-server \
--enable-agent \
--with-mysql \
--with-net-snmp \
--with-libcurl \
--with-libxml2
[[email protected] zabbix-3.4.12]# make install
//修改/etc/php.ini的配置并重启php-fpm
[[email protected] etc]# sed -ri 's/(post_max_size =).*/\1 16M/g' /etc/php.ini [[email protected] etc]# sed -ri 's/(max_execution_time =).*/\1 300/g' /etc/php.ini
[[email protected] etc]# sed -ri 's/(max_input_time =).*/\1 300/g' /etc/php.ini
[[email protected] etc]# sed -i '/;date.timezone/a date.timezone = Asia/Shanghai' /etc/php.ini
[[email protected] etc]# service php-fpm restart Gracefully shutting down php-fpm . done
Starting php-fpm done
在192.168.233.20上安装nginx服务
//创建系统用户nginx
[[email protected] ~]# groupadd -r nginx
[[email protected] ~]# useradd -r -M -s /sbin/nologin nginx
//安装依赖环境
[[email protected] ~]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++
安装过程略....
[[email protected] ~]# yum -y groups mark install 'Development Tools'
Loaded plugins: product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager to register.
There is no installed groups file.
Maybe run: yum groups mark convert (see man yum)
Marked install: Development Tools
//创建日志存放目录
[[email protected] ~]# mkdir -p /var/log/nginx
[[email protected] ~]# chown -R nginx.nginx /var/log/nginx
[[email protected] ~]# cd /usr/src/
[[email protected] src]# wget http://nginx.org/download/nginx-1.14.0.tar.gz
//编译安装
[[email protected] src]# ls
debug kernels nginx-1.14.0.tar.gz
[[email protected] src]# tar xf nginx-1.14.0.tar.gz
[[email protected] src]# cd nginx-1.14.0
[[email protected] nginx-1.14.0]# ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-debug \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_image_filter_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--http-log-path=/var/log/nginx/access.log \
--error-log-path=/var/log/nginx/error.log
[[email protected] nginx-1.14.0]# make -j 4 && make install
//配置环境变量
[[email protected] ~]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[[email protected] ~]# . /etc/profile.d/nginx.sh
//启动nginx
[[email protected] ~]# nginx
[[email protected] ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:80 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
2.配置
2.1 两边都关闭防火墙
[[email protected] ~]# systemctl stop firewalld
[[email protected] ~]# setenforce 0
[[email protected] ~]# systemctl stop firewalld
[[email protected] ~]# setenforce 0
2.2 修改主机10的php配置文件
[[email protected] ~]# vim /usr/local/php7/etc/php-fpm.conf
listen = 192.168.233.10:9000 //监控本机9000端口
### 2.3 zabbix安装配置
[[email protected] ~]# cd /usr/src/zabbix-3.4.12
[[email protected] zabbix-3.4.12]# ls
aclocal.m4 ChangeLog config.log configure.ac frontends m4 man README
AUTHORS compile config.status COPYING include Makefile misc sass
bin conf config.sub database INSTALL Makefile.am missing src
build config.guess configure depcomp install-sh Makefile.in NEWS upgrades
[[email protected] zabbix-3.4.12]# mkdir -p /usr/local/gsh/www/zabbix
[[email protected] zabbix-3.4.12]# cp -a frontends/php/* /usr/local/gsh/www/zabbix/
[[email protected] zabbix-3.4.12]# chown -R 777 /usr/local/gsh/www/zabbix/
2.4 安装nfs共享
[[email protected] ~]# yum -y install nfs-utils
[[email protected] ~]# systemctl start rpcbind nfs-server
[[email protected] ~]# cat /etc/exports /usr/local/gsh/www/zabbix/192.168.233.20(insecure,rw)
[[email protected] ~]# exportfs -r
修改nginx的配置文件
[[email protected] ~]# vim /usr/local/nginx/conf/nginx.conf
location / {
root html; index
index.php index.html index.htm ; ##注:添加index.php
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root /usr/local/gsh/www/zabbix; ##注://zabbix目录
fastcgi_pass 192.168.233.10:9000; ##注:修改为php服务器地址
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/gsh/www/zabbix$fastcgi_script_name;
include fastcgi_params; }
挂载共享zabbix
[[email protected] ~]# yum -y install nfs-utls
[[email protected] ~]# mount -t nfs 192.168.233.10:/usr/local/gsh/www/zabbix /usr/local/nginx/html/
重启服务
[[email protected] ~]# service php-fpm restart
[[email protected] ~]# nginx -s reload