LNMP环境安装方法

Cetetos7.6 lnmp环境安装步骤

lnmp 是由Linux+nginx+mysql+php这个几个组件组成的,用来搭建php网站应用而生。

网上有很多一键安装脚本,不过 如果你是运维工程师 建议你还是手工去搭建这个环境,那样你对整个过程就比较清楚排除问题起来也更加有思路,好那么来开始我们的安装之旅吧!

(1)安装Nginx

nginx的安装可以通过yum进行安装也可以通过源码包编译安装,我们以淘宝的tengine作为nginx的版本进行安装,本文采用最新的稳定版 Tengine-2.1.2 为例

$mkdir /opt/tools
$cd /opt/tools
$wget http://tengine.taobao.org/download/tengine-2.1.2.tar.gz
$tar -xzf tengine-2.1.2.tar.gz
$cd tengine-2.1.2
$./configure --prefix=/usr/local/nginx  --with-file-aio --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_ssl_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie' --with-http_stub_status_module --with-http_sysguard_module --with-http_concat_module    
$make && make install
$ln -s /usr/local/nginx/sbin/nginx /usr/bin/  
$nginx  			 #启动

(2)安装php和fpm

我们以安装php5.6版本为例

$yum -y install epel-release
$rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
$yum install php56w php56w-mysql php56w-gd libjpeg* php56w-ldap php56w-odbc php56w-pear php56w-xml php56w-xmlrpc php56w-mbstring php56w-bcmath
$ yum instal php56w-fpm   # 安装fpm

我们把其他用户改下,vim /etc/php-fpm.d/www.conf

user = apache
group = apache

改成
user = www
group = www

然后启动:

systemctl start php-fpm
netstat -ntlp |grep 9000   # 查看是否已经启动

php -m|grep mysql      # 查看启用了哪些扩展
配置nginx能解析php

vim /usr/local/nginx/nginx.conf的配置文件如下(生产环境肯定需要优化ng,这里只做最简单的安装使用)

worker_processes  1;
events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;



## 日志格式
log_format access '[$time_local] $remote_addr $status $request_time $upstream_response_time '   
                  '$body_bytes_sent "$request" "$http_referer" $upstream_addr '          
                  '$http_x_real_ip "$http_x_forwarded_for"  $http_user_agent" $request_filename ';

###
    server {
        listen       80;
        server_name  172.20.128.33;
        root   /home/data/webroot/zabbix/;
        
      location / {
        index   index.php index.html;
        location ~ \.php$ {
                fastcgi_pass   127.0.0.1:9000;
                fastcgi_index  index.php;
                fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include        fastcgi_params;
            }
          }

        access_log /home/data/logs/zabbix/zabbix.access.log access;
    }
}

对网站目录进行授权

chown -R www:www /home/data/logs/
chown -R www:www /home/data/webroot/

写一个测试文件打开看看是否正常,cat /home/data/webroot/zabbix/index.php,

<?php
phpinfo();

访问:

curl http://172.20.128.33/info.php |grep mysql    # 能有内容说明访问页面正常
(3)安装mysql

mysql 的安装方式有很多。mysql的安装方式有以下几种:

扫描二维码关注公众号,回复: 8608683 查看本文章
  • 源码包编译安装(安装时间较长)
  • 二进制包安装(推荐此种方式)
  • yum进行安装

如果生产环境我一般使用二进制包进行安装,这里为了简单起见,直接用yum 安装

开始安装
wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm
yum repolist all | grep mysql  # 查看可用的 mysql 安装文件
yum install mysql-server   # 安装
rpm -qa | grep mysql   # 检查是否安装成功

启动mysql服务

systemctl start mysqld.service 			#启动 mysql
systemctl status mysqld.service         # 查看状态
systemctl restart mysqld.service 		#重启 mysql
systemctl stop mysqld.service 			#停止 mysql
systemctl enable mysqld.service 		#设置 mysql 开机启动

登录mysql,首次登录是不需要密码的,清除掉无效的用户

select user,host from mysql.user   # 查看有哪些用户
>use mysql;
>Delete FROM user Where User='root' and Host='::1';
>Delete FROM user Where User='' and Host='localhost';
>Delete FROM user Where User='' and Host='hostname';
>Delete FROM user Where User='' and Host='vm203';
>Delete FROM user Where User='root' and Host='vm203';  

给root用户设置一个密码,如果生产环境清设置复杂的密码

>use mysql
>SET PASSWORD FOR 'root'@'localhost' = PASSWORD('123456'); 
> flush privileges;

创建一个应用库,并设置密码,让业务能读写这个库

>create database zabbix character set utf8 collate utf8_bin;
> grant all privileges on zabbix.* to zabbix@'localhost' identified by '123456';
>flush privileges;

退出来,再次登录试一试是否正常

mysql -uroot -p

到此,LNMP环境就安装完成

发布了56 篇原创文章 · 获赞 29 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/knight_zhou/article/details/103872767