LNMP环境搭建(zabbix监控需要)

因为zabbix需要LNMP或LAMP的环境,本人写了一套搭建lnmp的流程供大家入门参考。

Nginx为yum安装,MySQL为二进制包安装,PHP为源码包安装。

安装依赖包:

#yum install gcc bison bison-devel zlib-devel libmcrypt-devel mcrypt mhash-devel openssl-devel libxml2-devel libcurl-devel bzip2-devel readline-devel libedit-devel sqlite-devel libpng-devel libjpeg-devel freetype freetype-devel -y

创建www用户:

或者直接在编译的时候选择root

#groupadd www

#useradd -g www -s /sbin/nologin -M www

一、安装Nginx

在/etc/yum.repos.d/目录下创建一个源配置文件nginx.repo:

vim /etc/yum.repos.d/nginx.repo

填写如下内容:

[nginx]

name=nginx repo

baseurl=http://nginx.org/packages/centos/$releasever/$basearch/

gpgcheck=0

enabled=1

保存,则会产生一个/etc/yum.repos.d/nginx.repo文件。

下面直接执行如下指令即可自动安装好Nginx:

yum install nginx -y

安装完成,下面直接就可以启动Nginx了:

/etc/init.d/nginx start (service nginx start)

现在Nginx已经启动了,直接访问服务器就能看到Nginx欢迎页面了的。

二、安装mysql(二进制包方式安装)

1、添加mysql用户组及mysql用户

[root@localhost ~]# groupadd mysql

[root@localhost ~]# useradd -r -g mysql mysql

 

2、将二进制文件解压到指定的安装目录:/usr/local/mysql

到mysql-5.6.26-linux-glibc2.5-x86_64.tar.gz包的目录下,解压到/usr/local目录下:

[root@localhost tmp]# tar zxvf mysql-5.6.26-linux-glibc2.5-x86_64.tar.gz -C /usr/local/

到/usr/local/目录中,重命名解压出来的目录名,改为mysql:

[root@localhost tmp]# cd /usr/local/

[root@localhost local]# mv mysql-5.6.26-linux-glibc2.5-x86_64 mysql

 

3、创建mysql数据存放的目录:/data/mysql

[root@localhost mysql]# mkdir -p /data/mysql

 

4、更改所属的组和用户

[root@localhost mysql]# chown -R mysql:mysql /usr/local/mysql/

[root@localhost mysql]# chown -R mysql:mysql /data/mysql/

 

5、初始化mysql

注释:以mysql用户执行mysql_install_db脚本,基本目录为:/usr/local/mysql/,数据文件目录为:/data/mysql/

[root@localhost mysql]# pwd

/usr/local/mysql

[root@localhost mysql]#./scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/data/mysql/

linux 安装mysql 报错 :

Installing MySQL system tables.../usr/local/mysql//bin/mysqld: error while loading shared libraries: libnuma.so.1: cannot open shared object file: No such file or directory 

centos用yum装yum -y install numactl

 

6、修改my.cnf配置文件

复制/usr/local/mysql/support-files/my-default.conf 文件到/etc/ 目录下,覆盖原有的配置文件mysql/my.cnf:

[root@localhost mysql]# cp support-files/my-default.cnf /etc/my.cnf

修改/etc/my.cnf 文件,指定datadir、basedir 路径,同时开启3306端口:

basedir = /usr/local/mysql

datadir = /data/mysql

port = 3306

设置字符集

character-set-server=utf8

collation-server=utf8_unicode_ci

 

7、复制mysql 启动脚本mysql.server 到/etc/init.d/mysqld

[root@localhost mysql]# cp support-files/mysql.server /etc/init.d/mysqld

 

8、修改启动脚本的权限为755,赋予执行权限

[root@localhost mysql]# chmod 755 /etc/init.d/mysqld

 

9、通过chkconfig命令将mysqld服务加入到自启动服务项中:

[root@localhost init.d]# pwd

/etc/init.d

[root@localhost init.d]# chkconfig --add mysqld

检查是否加入到自动启动

[root@localhost init.d]# chkconfig --list mysqld

mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off

 

10、启动mysql服务:

[root@localhost mysql]# service mysqld start

Starting MySQL... SUCCESS!

注:在/etc/profile中加入以下环境变量,可以用mysql代替/usr/local/mysql/bin/mysql启动

MYSQL_HOME=/usr/local/mysql

export PATH=$PATH:$MYSQL_HOME/bin

设置后需要重新加载一下:

[root@localhost mysql]# source /etc/profile

 

三、安装PHP

3.1 安装依赖包

确保安装之前有安装gd,png,curl,xml等等lib开发库。如果不确定,执行以下命令:

yum install gcc make gd-devel libjpeg-devel libpng-devel libxml2-devel bzip2-devel libcurl-devel -y

编译安装

#wget php-7.2.0.tar.gz

#tar zxvf php-7.2.0.tar.gz -C /usr/local/

#cd /usr/local/php-7.2.0

#./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --enable-fpm --with-fpm-user=www --with-fpm-group=www --with-mysqli --with-pdo-mysql --with-iconv-dir --with-freetype-dir --with-gd --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex --enable-mbstring --enable-ftp --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --without-pear --with-gettext --disable-fileinfo --enable-maintainer-zts --with-libdir=lib64

 

# make

# make install

 

3.2 配置PHP

#cp /usr/local/php-7.2.0/php.ini-development /usr/local/php/etc/php.ini

#cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf

#cp /usr/local/php-7.2.0/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm

#chmod +x /etc/init.d/php-fpm

#cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf

3.3启动php-fpm

# /etc/init.d/php-fpm start

Starting php-fpm done

 

3.4添加 PHP 命令到环境变量

export PATH=$PATH:$HOME/bin:/usr/local/php/bin

使之生效:

source /etc/profile

 

3.5查看PHP版本:

 

# php -v

PHP 7.2.0 (cli) (built: Dec 17 2017 19:58:31) ( ZTS )

Copyright (c) 1997-2017 The PHP Group

Zend Engine v3.2.0, Copyright (c) 1998-2017 Zend Technologies

 

 

3.6测试结果:

 

vim /usr/share/nginx/html/a.php

phpinfo();

?>

 

Nginx配置文件修改的地方(打开fastcgi的注释)

LNMP环境搭建(zabbix监控需要)_第1张图片

重新加载Nginx,重启php-fpm

 

# /etc/init.d/nginx restart

Stopping nginx: [ OK ]

Starting nginx: [ OK ]

# /etc/init.d/php-fpm restart

Gracefully shutting down php-fpm . done

Starting php-fpm done

到此LNMP基本已经搭建完毕!

 

你可能感兴趣的:(Linux基础)