Ubuntu 16.04 LTS 安装 Nginx/PHP 5.6/MySQL 5.7 (LNMP)+zabbix3.0

3690阅读 0评论2016-06-15 龙飞九州
分类:LINUX

Nginx (读”engine x”) 是一款免费、开源的高性能 HTTP 服务。Nginx 稳定、丰富的功能集、配置简单、资源消耗低。本教程介绍了如何通过PHP5.6(通过PHP-FPM)和MySQL5.7(LNMP= LINUX + nginx(发音为“engine x”)+ MySQL5.7+ PHP)以及zabbix3.0 16.04服务器搭建lnmp环境

对我这个刚接触ubuntu没多久的小菜鸟搭建这个环境费了不少时间。

希望这个文档可以帮到像我一样的
那些伙伴们!

1. 初步说明


本来是打算用ubuntu16.04+nginx+php7+mysql5.7搭建LNMP环境。ubuntu16.04自带软件源也都是最新版本的。
搭建好LNMP后安装zabbix3.0,发现成功不了,弄了大半天终于找到解决方法。原来zabbix3.0不支持php7.0.官网要求:

Software Version Comments
Apache 1.3.12 or later
PHP 5.4.0 or later PHP v7 is not supported yet.

所以只能把php7换成php5.6.

2 .安装 MySQL 5.7

安装 MySQL 运行命令:

apt-get -y install mysql-server mysql-client

你会被要求提供MySQL的root用户密码 :

New password for the MySQL “root” user: <– yourrootsqlpassword
Repeat password for the MySQL “root” user: <– yourrootsqlpassword

为了确保数据库服务器,并删除匿名用户和测试数据库,运行mysql_secure_installation命令。

mysql_secure_installation

你会问这些问题:

root@server1:~# mysql_secure_installation

保护MySQL服务器部署。

Enter password for user root: <– Enter the MySQL root password

VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin?

Press y|Y for Yes, any other key for No: <– Press y if you want this function or press Enter otherwise.
Using existing password for root.
Change the password for root ? ((Press y|Y for Yes, any other key for No) : <– Press enter

… skipping.
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No) : <– y
Success.

Normally, root should only be allowed to connect from
‘localhost’. This ensures that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : <– y
Success.

By default, MySQL comes with a database named ‘test’ that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.

Remove test database and access to it? (Press y|Y for Yes, any other key for No) : <– y
– Dropping test database…
Success.

– Removing privileges on test database…
Success.

Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : <– y
Success.

All done!

MySQL is secured now.

mysql初始化完毕后,我们现在来创建zabbix数据库及其用户,使用如下命令:

mysql -uroot -p`zabbix’ -e “create database zabbix default character set utf8 collate utf8_bin;”

mysql -uroot -p`zabbix’ -e “grant all on zabbix.* to ‘zabbix’@’%’ identified by ‘zabbix’;”

现在来测试刚刚创建的zabbix用户,是否可以连接mysql数据库,如下:

mysql -uzabbix -pzabbix

mysql> show databases;
--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| zabbix             |
+--------------------+
5 rows in set (0.06 sec)
通过上图,我们可以很明显的看出zabbix用户是可以正常连接数据库的。


3 .安装 Nginx

在你已经安装了Apache2的话,那么使用这些命令先删除再安装nginx:

service apache2 stop
update-rc.d -f apache2 remove
apt-get remove apache2

Ubuntu16.04有Nginx安装包,我们可以安装。

apt-get -y install nginx

Start nginx afterwards:

service nginx start

输入您的Web服务器的IP地址或主机名到浏览器(例如),你应该看到如下页面:

在Ubuntu16.04的默认nginx的文档根目录为/var/www/html


4. 安装 PHP 5.6

我在Ubuntu16.04桌面系统安装php5时,提示Package php5 have no installation candidate,在上面找到了答案。

Ubuntu 16.04 comes with PHP7 as the standard, so there are no PHP5 packages

However if you like you can add a PPA to get those packages anyways: 
Add the PPA

1.sudo add-apt-repository ppa:ondrej/php
                   Install your need  PHP Version
2.   sudo apt-get update 
        sudo apt-get install php5.6

3. 我们可以通过使nginx的PHP工作PHP-FPM(PHP-FPM(FastCGI进程管理器)是为任何规模的网站,
  尤其是繁忙的网站有用的一些附加功能的替代PHP的FastCGI实现),我们安装如下:

        
sudo apt-get -y install php5.6-fpm

注意:sudo apt-get install -y php5.6-bcmath php5.6-gd php5.6-mbstring php5.6-xml  
          我再安装zabbix3.0的
过程中就要求安装这些)。


5 配置 nginx

打开配置文件 /etc/nginx/nginx.conf:

nano /etc/nginx/nginx.conf

配置是很容易理解 (你可以点击官方教程:  或:)

首先(这是可选)调整keepalive_timeout到一个合理的值:

[...]
    keepalive_timeout   2;
[...]

虚拟主机服务器{}容器定义。默认的虚拟主机是在文件中定义的/etc/nginx/sites-available/default – 让我们来修改它,如下所示:

sudo cat /etc/nginx/sites-available/default | grep -v  '#'下面是我的配置:

server {
        listen 80 default_server;
        listen [::]:80 default_server ipv6only=on;

        root /var/www/html;
        index index.php index.html index.htm;

        server_name localhost;

        location / {
                try_files $uri $uri/ =404;
        }
        error_page 404 /404.html;
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
                root /var/www/html;
        }

        location ~ \.php$ {
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
        
                fastcgi_pass unix:/var/run/php/php5.6-fpm.sock;
                fastcgi_index index.php;
                include fastcgi_params;
                
fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name; 
        }

        location ~ /\.ht {
                deny all;
        }
}

sudo service nginx reload

下一步打开 /etc/php/5.6/fpm/php.ini

nano /etc/php/5.6/fpm/php.ini

设置 cgi.fix_pathinfo=0:


重新加载 PHP-FPM:

service php5.6-fpm reload

建立探针文件/var/www/html:

sudo vim /var/www/html/info.php


						

浏览器访问 (e.g. /info.php):



OK,LNMP环境搭建完毕!

6.安装zabbix server3.0

在ubuntu 16上安装zabbix server 3.0就简单的多了,我们也是直接使用apt-get方式进行的。
1.下载deb
# wget http://repo.zabbix.com/zabbix/3.0/ubuntu/pool/main/z/zabbix-release/zabbix-release_3.0-1+trusty_all.deb
# dpkg -i zabbix-release_3.0-1+trusty_all.deb
# apt-get update
2.安装服务端
sudo apt-get install zabbix-server-mysql php5.6-mysql zabbix-frontend-php zabbix-agent

3.配置zabbix_server.conf

# sudo vim /etc/zabbix/zabbix_server.conf
DBHost=localhost
DBName=zabbix
DBUser=zabbix D
BPassword
=zabbix

4.
设置mysql

cd /usr/share/doc/zabbix-server-mysql
zcat create.sql.gz | mysql -u root -p zabbix
sudo cp -r
/usr/share/zabbix /var/www/html/zabbix
service zabbix-server restart


5.

配置PHP



sudo vim /etc/php/5.6/fpm/php.ini 
post_max_size = 16M
max_execution_time = 300
max_input_time = 300
date.timezone = "Asia/Shanghai"
always_populate_raw_post_data -1

重启nginx
sudo service nginx restart

6.zabbix网页配置


install_1.png
下一步
install_2.png

再下一步

Enter details for connecting to the database. Zabbix database must already be created.



Enter Zabbix server details.



Review a summary of settings.


Finish the installation.



Zabbix frontend is ready! The default user name is Admin, password zabbix.


上一篇:用find和rm命令来批量删除文件
下一篇:ubuntu server设置时区和更新时间