Centos7 安装LNMP环境

  • 修改yum
    1
    2
    3
     rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
    rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
    rpm -Uvh http://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm
  • 安装php、mysql、nginx
    1
    2
    3
    yum -y install nginx
    yum -y install mysql-community-server
    yum -y install php70w-devel php70w.x86_64 php70w-cli.x86_64 php70w-common.x86_64 php70w-gd.x86_64 php70w-ldap.x86_64 php70w-mbstring.x86_64 php70w-mcrypt.x86_64 php70w-pdo.x86_64 php70w-mysqlnd php70w-fpm php70w-opcache php70w-pecl-redis php70w-pecl-mongo
  • 配置mysql
    • MySQL 安装完成之后,在 /var/log/mysqld.log 文件中给 root 生成了一个默认密码

    • 通过下面的方式找到root 默认密码,然后登录 MySQL 进行修改

      1
      2
      3
       systemctl start mysqld    # 启动 MySQL
      grep 'temporary password' /var/log/mysqld.log # 查找默认密码
      2017-10-310T02:58:16.806931Z 1 [Note] A temporary password is generated for root@localhost: iacFXpWt-6gJ
    • 登录mysql

        
      1
      mysql -uroot -p'iacFXpWt-6gJ'
    • 修改密码

      1
      mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyPass1!';

      或者

      1
      mysql> set password for 'root'@'localhost'=password('123abc');
    • 配置默认编码为 utf8:

      1
      2
      3
      4
      5
       vim /etc/my.cnf
      [mysqld] ### 增加下面两行
      character_set_server=utf8
      init_connect='SET NAMES utf8'
      systemctl restart mysqld # 重启 MySQL
    • 设置开机启动

      1
      systemctl enable mysqld
    • 默认配置文件路径

      1
      2
      3
      4
      配置文件:/etc/my.cnf
      日志文件:/var/log/mysqld.log
      服务启动脚本:/usr/lib/systemd/system/mysqld.service
      socket 文件:/var/run/mysqld/mysqld.pid
  • 配置nginx
    1
    2
    3
    4
    5
    6
    7
    systemctl status firewalld ### 防火墙 如果active (running) 则修改添加nginx规则
    vi /etc/firewalld/zones/public.xml
    <zone>
    ...
    <service name="nginx"/>
    <zone>
    systemctl reload firewalld ### 重启防火墙
  • 配置nginx。con
    1
    cp /etc/nginx/nginx.conf  /etc/nginx/nginx.conf_bak
  • vi /etc/nginx/nginx.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34

user nobody;
worker_processes 1;

error_log /data/logs/nginx/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;

events {
worker_connections 1024;
}

http {
include mime.types;
default_type application/octet-stream;

log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';

access_log /data/logs/nginx/access.log main;

sendfile on;
#tcp_nopush on;

#keepalive_timeout 0;
keepalive_timeout 65;

gzip on;
gzip_types text/plain application/javascript application/x-javascript text/css application/xml;
include /etc/nginx/conf.d/*.conf;
}

  • vi /etc/nginx/conf.d/default.conf

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    #
    # The default server
    #
    server {
    listen 80;
    server_name _;

    access_log /data/log/nginx/defalut.access.log main;

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

    location / {
    root /data/www;
    index index.php index.html index.htm;
    }

    error_page 404 /404.html;
    location = /404.html {
    root /data/www/info_58;
    }

    # redirect server error pages to the static page /50x.html
    #
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
    root /usr/share/nginx/html;
    }

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #

    location ~ \.php$ {
    root /data/www;
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME /data/www$fastcgi_script_name;
    include fastcgi_params;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    location ~ /\.ht {
    deny all;
    }
    }
  • 启动nginx

    1
    2
    systemctl start nginx
    systemctl enable nginx ### 开机启动
  • 启动fpm

    1
    2
    systemctl enable php-fpm
    systemctl start php-fpm
  • 访问测试

    1
    2
    3
    vi /data/www/test.php
    <?php
    phpinfo();