搭建LNMP

两台服务器搭建LNMP

配置需求

服务器配置 ip
master(mysql,php) 192.168.125.131
xj(nginx) 192.168.125.128

步骤:

  • 1.安装nginx,详见nginx服务
//创建系统组和用户nginx
[root@xj ~]# groupadd -r nginx
[root@xj ~]# useradd -r -M -s /sbin/nologin -g nginx nginx

/ /安装依赖环境
[root@xj ~]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++
安装过程略....
[root@xj~]# yum -y groups mark install 'Development Tools'   --安装开发工具
//创建日志存放目录
[root@xj ~]# mkdir -p /var/log/nginx
[root@xj ~]# chown -R nginx.nginx /var/log/nginx
//下载nginx
[root@xj ~]# cd /usr/src/
[root@xj src]# wget http://nginx.org/download/nginx-1.14.0.tar.gz
[root@xj src]# tar xf nginx-1.14.0.tar.gz 
[root@xj src]# cd nginx-1.14.0
[root@xj 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
[root@xj nginx-1.14.0]# make && make install 
[root@xj nginx-1.14.0]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@xj nginx-1.14.0]# source /etc/profile.d/nginx.sh
[root@xj ~]# 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”项对应)
[root@xjt nginx]# cat > html/index.php << EOF
>      phpinfo();
> ?>
> EOF
  • 2.安装mysql
安装网络源
[root@master ~]#  wget http://mirrors.163.com/.help/CentOS7-Base-163.repo
[root@master ~]# sed -i 's/\$releasever/7/g' /etc/yum.repos.d/CentOS7-Base-163.repo
[root@master~]# sed -i 's/^enabled=.*/enabled=1/g' /etc/yum.repos.d/CentOS7-Base-163.repo
[root@master ~]# yum -y install epel-release
[root@master ~]# rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
[root@master~]# 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@master ~]# tar xf php-7.2.8.tar.xz
[root@master~]# cd php-7.2.8
[root@master 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
[root@xxx php-7.2.8]#make&make install --编译安装
[root@xxx php-7.2.8]#echo 'export PATH=/usr/local/php7/bin:$PATH' > [[email protected]~]/etc/profile.d/php7.sh --配置环境变量
[root@xxx php-7.2.8]# source /etc/profile.d/php7.sh
配置php-fpm
[root@xxx php-7.2.8]# cp php.ini-production /etc/php.ini
[root@xxx php-7.2.8]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@xxx php-7.2.8]# chmod +x /etc/rc.d/init.d/php-fpm
[root@xxx php-7.2.8]# cp /usr/local/php7/etc/php-fpm.conf.default /usr/local/php7/etc/php-fpm.conf
[root@xxx 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
[root@xxx ~]# service php-fpm start
Starting php-fpm  done
  • 3 安装mysql
[root@master src]# yum -y install ncurses-devel openssl-devel openssl cmake mariadb-devel --安装依赖包
...
创建用户和组
[root@master src]# groupadd -r -g 300 mysql
[root@master src]# useradd -M -s /sbin/nologin -g 300 -u 300 mysql
[root@master src]# tar xf mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz  /usr/local
[root@master src]# cd /usr/local/
[root@master local]# ln -sv mysql-5.7.23-linux-glibc2.12-x86_64/ mysql
"mysql" -> "mysql-5.7.23-linux-glibc2.12-x86_64/"
[root@master local]# ll mysql
lrwxrwxrwx. 1 root root 36 2月  20 12:08 mysql -> mysql-5.7.23-linux-glibc2.12-x86_64/
[root@master local]# chown -R mysql.mysql /usr/local/mysql  --更改属主属组
[root@master 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/
[root@master local]# vim /etc/profile.d/mysql.sh --添加环境变量
[root@master local]# cat /etc/profile.d/mysql.sh
export PATH=/usr/loacl/mysql/bin:$PATH
[root@master local]# . /etc/profile.d/mysql.sh
创建数据存放文件 并更改属主属组
[root@master local]# mkdir /opt/test
[root@master local]# chown -R mysql.mysql /opt/test/
[root@master local]# ll /opt/test/ -d
drwxr-xr-x. 2 mysql mysql 6 2月  20 12:12 /opt/test/
数据库初始化
[root@master 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 root@localhost: O7n/<2ziRu(/    --初始化密码
生成配置文件
[root@master local]# vim /etc/my.cnf
[root@master 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
配置服务启动脚本
[root@master ~]# \cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@master ~]# vim /etc/init.d/mysqld
basedir=/usr/local
datadir=/opt/test
[root@master ~]# service mysqld start
Starting MySQL.. SUCCESS!  
[root@master ~]# 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
[root@master~]# [root@xj ~]# mysql -uroot -p
Enter password: 
mysql> set password = password('123456');  --更改密码

验证php界面
搭建LNMP_第1张图片

  • 4.安装zabiix
[root@master src]# wget https://nchc.dl.sourceforge.net/project/zabbix/ZABBIX%20Latest%20Stable/4.0.3/zabbix-4.0.3.tar.gz
[root@master src]#  tar zabbix-4.0.3.tar.gz 
[root@master src]# yum -y install net-snmp-devel libevent-devel
...
[root@master src]# groupadd -r zabbix
[root@master src]# useradd -r -g zabbix -M -s /sbin/nologin zabbix
[root@master ~]# 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                           :::*     
[root@master ~]# vim mysql.sh 
[root@master ~]# 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 zabbix@localhost identified by 'zabbix123!';
Query OK, 0 rows affected, 2 warnings (0.02 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.02 sec)
[root@master ~]# cd /usr/src/zabbix-4.0.3/database/mysql/
[root@master mysql]# mysql -uzabbix -pxj123456 zabbix < schema.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@master mysql]# mysql -uzabbix -pxj123456 zabbix < images.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@master mysql]#  mysql -uzabbix -pxj123456 zabbix < data.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@master zabbix-4.0.3]# ./configure --prefix=/usr/local/zabbix --enable-server \
> --enable-agent \
> --with-mysql \
> --with-net-snmp \
> --with-libcurl \
> --with-libxml2
[root@master zabbix-4.0.3]#  make install
  • 5.配置nfs,共享zabbix目录文件
[root@master ~]# yum -y install nfs-utils 
[root@master ~]# systemctl restart nfs 
[root@master ~]# systemctl enable nfs
[root@master ~]# systemctl status nfs
创建共享目录
[root@master ~]# vim /etc/exports
[root@master ~]# cat /etc/exports
/test/www 192.168.125.128(insecure,rw)
在nginx服务器上查看
[root@xj ~]# yum -y install showmount
[root@xj ~]# showmount -e 192.168.125.129
/test/www 192.168.125.128
[root@xj ~]# mount -t nfs -o nfsvers=3,vers=3 192.168.125.129:/test/www /usr/local/nginx/html/
[root@xj ~]# 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网页
搭建LNMP_第2张图片
搭建LNMP_第3张图片

你可能感兴趣的:(搭建LNMP)