搭建LNMP
两台服务器搭建LNMP
配置需求
服务器配置 | ip |
---|---|
master(mysql,php) | 192.168.125.131 |
xj(nginx) | 192.168.125.128 |
步骤:
- 1.安装nginx,详见nginx服务
//创建系统组和用户nginx
[[email protected] ~]# groupadd -r nginx
[[email protected] ~]# useradd -r -M -s /sbin/nologin -g nginx 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' --安装开发工具
//创建日志存放目录
[[email protected] ~]# mkdir -p /var/log/nginx
[[email protected] ~]# chown -R nginx.nginx /var/log/nginx
//下载nginx
[[email protected] ~]# cd /usr/src/
[[email protected] src]# wget http://nginx.org/download/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 && make install
[[email protected] nginx-1.14.0]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[[email protected] nginx-1.14.0]# source /etc/profile.d/nginx.sh
[[email protected] ~]# nginx启动服务
- 1.1 配置虚拟主机
user nginx; --将用户改为nginx并取消注释
worker_processes 1;
error_log logs/error.log; --错误日志
error_log logs/error.log notice;
error_log logs/error.log info;
events {
worker_connections 1024;
}
location / {
root html; --默认
index index.html index.htm;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root /test/www; --网页文件存放目录(php端)
fastcgi_pass 192.168.125.131:9000; --修改为远程php服务器地址
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /test/www$fastcgi_script_name;
include fastcgi_params;
}
- 在存放网页的目录中创建index.php文件(与配置文件中的“root”项对应)
[[email protected] nginx]# cat > html/index.php << EOF
> <?php
> phpinfo();
> ?>
> EOF
- 2.安装mysql
安装网络源
[[email protected] ~]# wget http://mirrors.163.com/.help/CentOS7-Base-163.repo
[[email protected] ~]# sed -i 's/\$releasever/7/g' /etc/yum.repos.d/CentOS7-Base-163.repo
[[email protected]~]# sed -i 's/^enabled=.*/enabled=1/g' /etc/yum.repos.d/CentOS7-Base-163.repo
[[email protected] ~]# yum -y install epel-release
[[email protected] ~]# rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
[[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
[[email protected] ~]# tar xf php-7.2.8.tar.xz
[[email protected]~]# 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&make install --编译安装
[[email protected] php-7.2.8]#echo 'export PATH=/usr/local/php7/bin:$PATH' > [[email protected]~]/etc/profile.d/php7.sh --配置环境变量
[[email protected] php-7.2.8]# source /etc/profile.d/php7.sh
配置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
[[email protected] ~]# service php-fpm start
Starting php-fpm done
- 3 安装mysql
[[email protected] src]# yum -y install ncurses-devel openssl-devel openssl cmake mariadb-devel --安装依赖包
...
创建用户和组
[[email protected] src]# groupadd -r -g 300 mysql
[[email protected] src]# useradd -M -s /sbin/nologin -g 300 -u 300 mysql
[[email protected] src]# tar xf mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz /usr/local
[[email protected] src]# 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 mysql
lrwxrwxrwx. 1 root root 36 2月 20 12:08 mysql -> mysql-5.7.23-linux-glibc2.12-x86_64/
[[email protected] local]# chown -R mysql.mysql /usr/local/mysql --更改属主属组
[[email protected] local]# ll /usr/local/mysql
lrwxrwxrwx. 1 mysql mysql 36 2月 20 12:08 /usr/local/mysql -> mysql-5.7.23-linux-glibc2.12-x86_64/
[[email protected] local]# vim /etc/profile.d/mysql.sh --添加环境变量
[[email protected] local]# cat /etc/profile.d/mysql.sh
export PATH=/usr/loacl/mysql/bin:$PATH
[[email protected] local]# . /etc/profile.d/mysql.sh
创建数据存放文件 并更改属主属组
[[email protected] local]# mkdir /opt/test
[[email protected] local]# chown -R mysql.mysql /opt/test/
[[email protected] local]# ll /opt/test/ -d
drwxr-xr-x. 2 mysql mysql 6 2月 20 12:12 /opt/test/
数据库初始化
[[email protected] local]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/opt/test/
2019-02-20T04:15:37.920812Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
...
2019-02-20T04:15:38.761539Z 1 [Note] A temporary password is generated for [email protected]: O7n/<2ziRu(/ --初始化密码
生成配置文件
[[email protected] local]# vim /etc/my.cnf
[[email protected] local]# cat /etc/my.cnf
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/test
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/test/mysql.pid
user = mysql
skip-name-resolve
配置服务启动脚本
[[email protected] ~]# \cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[[email protected] ~]# vim /etc/init.d/mysqld
basedir=/usr/local
datadir=/opt/test
[[email protected] ~]# service mysqld start
Starting MySQL.. SUCCESS!
[[email protected] ~]# ps -ef|grep mysql --查看mysql进程
root 72198 1 0 21:28 pts/0 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/opt/test --pid-file=/opt/test/mysql.pid
mysql 72376 72198 0 21:28 pts/0 00:00:03 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/opt/test --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=xj.err --pid-file=/opt/test/mysql.pid --socket=/tmp/mysql.sock --port=3306
root 92692 92584 0 23:39 pts/1 00:00:00 grep --color=auto mysql
[[email protected]~]# [[email protected] ~]# mysql -uroot -p
Enter password:
mysql> set password = password('123456'); --更改密码
验证php界面
- 4.安装zabiix
[[email protected] src]# wget https://nchc.dl.sourceforge.net/project/zabbix/ZABBIX%20Latest%20Stable/4.0.3/zabbix-4.0.3.tar.gz
[[email protected] src]# tar zabbix-4.0.3.tar.gz
[[email protected] src]# yum -y install net-snmp-devel libevent-devel
...
[[email protected] src]# groupadd -r zabbix
[[email protected] src]# useradd -r -g zabbix -M -s /sbin/nologin zabbix
[[email protected] ~]# ss -anlt
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 192.168.125.131:9000 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 100 ::1:25 :::*
LISTEN 0 80 :::3306 :::*
LISTEN 0 128 :::22 :::*
[[email protected] ~]# vim mysql.sh
[[email protected] ~]# mysql -uroot -pxj123456
mysql> create database zabbix character set utf8 collate utf8_bin;
Query OK, 1 row affected (0.01 sec)
mysql> grant all privileges on zabbix.* to [email protected] identified by 'zabbix123!';
Query OK, 0 rows affected, 2 warnings (0.02 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.02 sec)
[[email protected] ~]# cd /usr/src/zabbix-4.0.3/database/mysql/
[[email protected] mysql]# mysql -uzabbix -pxj123456 zabbix < schema.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
[[email protected] mysql]# mysql -uzabbix -pxj123456 zabbix < images.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
[[email protected] mysql]# mysql -uzabbix -pxj123456 zabbix < data.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
[[email protected] zabbix-4.0.3]# ./configure --prefix=/usr/local/zabbix --enable-server \
> --enable-agent \
> --with-mysql \
> --with-net-snmp \
> --with-libcurl \
> --with-libxml2
[[email protected] zabbix-4.0.3]# make install
- 5.配置nfs,共享zabbix目录文件
[[email protected] ~]# yum -y install nfs-utils
[[email protected] ~]# systemctl restart nfs
[[email protected] ~]# systemctl enable nfs
[[email protected] ~]# systemctl status nfs
创建共享目录
[[email protected] ~]# vim /etc/exports
[[email protected] ~]# cat /etc/exports
/test/www 192.168.125.128(insecure,rw)
在nginx服务器上查看
[[email protected] ~]# yum -y install showmount
[[email protected] ~]# showmount -e 192.168.125.129
/test/www 192.168.125.128
[[email protected] ~]# mount -t nfs -o nfsvers=3,vers=3 192.168.125.129:/test/www /usr/local/nginx/html/
[[email protected] ~]# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/mapper/rhel-root 18G 4.6G 13G 26% /
devtmpfs 908M 0 908M 0% /dev
tmpfs 914M 0 914M 0% /dev/shm
tmpfs 914M 8.5M 906M 1% /run
tmpfs 914M 0 914M 0% /sys/fs/cgroup
/dev/sda1 497M 96M 401M 20% /boot
192.168.125.129:/test/www 18G 5.9G 12G 34% /usr/local/nginx/html
测试web访问zabbix网页