Cetetos7.6 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 deltarpm
$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 install php56w-fpm   # 安装fpm

如果是安装php7.2版本,我们用下面的yum的方法

yum install epel-release
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
yum install php72w  php72w-mysqlnd  php72w-gd php72w-xmlrpc  php72w-bcmath php72w-xml php72w-pdo php72w-mbstring php72w-gd php72w-fpm php72w-devel

上面是php5.6和7.2的yum安装,剩下其他配置都一样。

我们把其他用户改下,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,这里只做最简单的安装使用)

user  www;
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 KaTeX parse error: Double superscript at position 47: … '̲body_bytes_sent “ r e q u e s t " " request" " request""http_referer” KaTeX parse error: Double superscript at position 45: … '̲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;
}

}

创建网站目录并对网站目录进行授权

mkdir -p  /home/data/logs/
mkdir -p www /home/data/webroot/zabbix/
chown -R www:www /home/data/logs/
chown -R www:www /home/data/webroot/

对session目录授权,不然会出现500的错误

chown -R www:www /var/lib/php

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

访问:

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

也可用直接在浏览器上面访问http://ip/

(3)安装mysql

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

  • 源码包编译安装(安装时间较长)
  • 二进制包安装(推荐此种方式)
  • yum进行安装

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

开始安装

(1) 使用yum方式安装Mysql5.6

wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm
rpm -ivh 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

(2)使用yum方式安装Mysql5.7

wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
yum  install mysql57-community-release-el7-10.noarch.rpm
yum install mysql-community-server
# 启动
systemctl start  mysqld.service
systemctl status  mysqld.service
systemctl enable  mysqld.service

mysql安装完成之后,在/var/log/mysqld.log文件中给root生成了一个默认密码。通过下面的方式找到root默认密码,然后登录mysql进行修改

grep 'temporary password' /var/log/mysqld.log

用这个密码登录mysql,然后修改root密码

mysq>SET PASSWORD FOR 'root'@'localhost' = PASSWORD('ahoelonG_xx37');

到此,LNMP环境就安装完成

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