linux centos7 的 LNMP环境搭建以及composer安装

一、检查是否安装该程序:

which nginx           #查看nginx是否存在
which php             #查看php是否存在
which mysql          #查看mysql是否存在

二、Nginx安装

yum list nginx             #列出nginx
yum install nginx        #安装nginx

看见{Complete! } 安装成功。
安装完nginx之后访问本机ip,结果直接报错,然后去查看var目录下查看nginx错误日志,看到如下错误信息,意思是html下面没有

directory index of "/usr/share/nginx/html/" is forbidden

如果在/usr/share/nginx/html下面没有index.php,index.html的时候,直接访问域名,找不到文件,会报403 forbidden。
解决办法:直接输入以下命令:

#删除index.html文件
rm /usr/share/nginx/html/index.html
#在 /usr/share/nginx/html目录下面新建一个index.php文件,内容是
<?php
      phpinfo(); 
?>
 

还有一个原因就是你的默认的nginx配置文件的location没有指定root路径,我的就是这个问题,这个就直接在默认的 /etc/nginx/nginx.conf 内容里的 server上面加上:

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

如果还是不能正常访问,那就进到nginx的默认配置文件里去找一下,看看有没有如下代码:

location ~* \.php$ {
    fastcgi_index   index.php;
    fastcgi_pass    127.0.0.1:9000;
    include         fastcgi_params;
    fastcgi_param   SCRIPT_FILENAME    $document_root$fastcgi_script_name;
    fastcgi_param   SCRIPT_NAME        $fastcgi_script_name;
}

上面代码的意思是把php文件交给php-fpm处理,php-fpm占用的端口号是9000。

nginx隐藏入库文件

nignx/conf下有vhosts.conf这个文件的话,在这个文件里配置,当有这个文件的时候,在nginx.conf里配置是不起作用的。没有的话就在nginx.conf文件配置.
注意:nginx隐藏入口文件,找到nginxvhost文件,在server 里加入如下:

location / {
   if (!-e $request_filename) {
        rewrite  ^(.*)$  /index.php?s=/$1  last;
    }
}

就Ok了。
配置完以后,记得重启nginx,或者reload一下:
重启nginx:

nginx -s stop
nginx
或者
nginx -s reopen

下面是重新加载nginx的配置文件(推荐这种方式):

service nginx reload

找到nginx的安装目录:

[root@localhost nginx]# which nginx
/usr/sbin/nginx             #这个是yum安装后默认所在位置

查看nginx的命令提示:

[root@localhost nginx]# nginx -h
nginx version: nginx/1.10.2
Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]

Options:
  -?,-h         : this help
  -v            : show version and exit
  -V            : show version and configure options then exit
  -t            : test configuration and exit
  -T            : test configuration, dump it and exit
  -q            : suppress non-error messages during configuration testing
  -s signal     : send signal to a master process: stop, quit, reopen, reload
  -p prefix     : set prefix path (default: /usr/share/nginx/)
  -c filename   : set configuration file (default: /etc/nginx/nginx.conf)
  -g directives : set global directives out of configuration file

启动 Nginx
which nginx #查看nginx是否存在
service nginx start #启动nginx进程方法[1]
/etc/init.d/nginx start #启动nginx进程方法[2]
ps -ef | grep nginx #查看nginx进程
ps -le #查看nginx进程
ifconfig #查看网卡信息
打开浏览器:http://182.92.73.109/
看到 { 欢迎界面 } 说明安装成功!
注意:ip访问,这里的ip是公网ip

三、Php安装

查看php使用的php.ini文件的位置,使用的命令是php -i |grep php.ini(非常好用的一条命令)

yum list php php-fpm              #列出php 和php-fpm 是否存在
yum -y install php php-fpm     #安装php 和php-fpm软件包

看见{Complete! } 安装成功。
启动php-fpm:

service php-fpm start   // 1.启动php-fpm
/bin/systemctl start php-fpm.service  // 2.启动php-fpm

--php扩展安装
如:

yum list php-gd  
yum install php-gd 

redis安装

yum list redis
yum install redis

php扩展安装完成后,重启php-fpm

// 重启php-fpm
/bin/systemctl restart php-fpm.service
// 启动redis
/bin/systemctl restart redis.service
// 查看redis进程
ps -le | grep redis

注意:redis如果需要守护进程启动,需要再redis.conf配置文件128行

daemonize no // 将 no 改成 yes

将 no 改成 yes,然后进入 /usr//bin 系统用户使用的应用程序下,找到 redis-cli 启动

/usr/bin/redis-cli
[root@VM_0_6_centos bin]# redis-cli
127.0.0.1:6379>

出现此则成功了,进行set name xiaochi 设置 ok ,然后 get name,获取成功

四、Mysql安装

yum list mysql mysql-server            #搜索mysql数据包
yum -y install mysql mysql-server    #不需要提示安装

看见{Complete! } 安装成功。如果出现 No package mysql-server available错误,是因为我们本地yum仓库中没有可用的mysql-server rpm包,解决:

// 在Centos 7上使用
rpm -ivh https://repo.mysql.com//mysql57-community-release-el7-11.noarch.rpm
// Centos 6上安装mysql rpm包
rpm -ivh https://repo.mysql.com//mysql57-community-release-el6-11.noarch.rpm

rpm软件包安装好之后,我们就可以使用 yum install mysql-server 命令来安装mysql了。
查看mysql mysql-server是否存在:

which mysql                   #查看mysql是否存在

验证mysql是否启动成功命令,使用netstat命令

    netstat -nap |grep 3306
    tcp   0   0 0.0.0.0:3306   0.0.0.0:*    LISTEN    4571/mysqld

启动mysql

    service mysqld start      #启动mysql

最后需要查看mysql5.7的初始化密码(如果是mysql5.6的话默认密码应该为空,不需要此命令查询)

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

输出类似如下的随机密码:

2017-11-08T16:24:46.311098Z 1 [Note] A temporary password is generated for [root@localhost](mailto:root@localhost): MtPqF0/oN5zoSfdff

MtPqF0/oN5zoSfdff是就是初始密码
然后输入mysql -uroot -p命令登录mysql服务器,并修改mysql初始化密码。

//输入初始化密码登录之后,重新设置mysql密码
mysql> set password for root@localhost = password('123456');
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements

这时会报错,因为为了加强安全性,MySQL5.7为root用户随机生成了一个密码,在error log中,关于error log的位置,如果安装的是RPM包,则默认是/var/log/mysqld.log。一般可通过log_error设置
设置密码的格式,必须有数字,字母,特殊符号,所以需要为了方便设置密码123456,需要进行如下设置

  mysql> set global validate_password_policy=0;
  mysql> set global validate_password_length=1;
  mysql> set global validate_password_mixed_case_count=2;

然后再进行设置密码

  mysql> set password for root@localhost = password('123456');

ok了,quit退出mysql。
然后进行测试,进入到 usr/share/nginx/html 目录下,找到index.php并输入

  $link = mysqli_connect('localhost','root','123456','test');
  if($link){
       echo 'ok';
  }else{
       echo 'error';
  }

注意:先 cd usr/bin 访问系统用户应用程序目录,进入登陆 mysql -uroot -p 进入 mysql 查看 show databases 是否有 test 这个数据库,没有则创建(create database test)
在访问当前腾讯云服务器 公ip,地址栏输入 http://118.24.188.68/,输出ok,表示成功链接Mysql。
Ctrl-C -- exit! #退出终端
*注意:远程连接失败问题,如navicat连接失败
先运行命令

mysql -uroot -p  // 登陆mysql
use mysql; // 选择mysql数据库
select host,user from user;// 查看用户权限

1.运行如下命令,赋与远程连接权限

GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;

2.刷新一下

flush privileges;

如果此时还解决不了,就需要找到mysql配置my.ini文件,运行如下

bind-address = 127.0.0.1 改成 bind-address = 0.0.0.0

然后,重载配置、重启服务,就ok了。

五、phpMyAdmin安装

yum install phpMyAdmin

看见{Complete! } 安装成功。接着 cd /usr/share/ 目录下,将 phpMyAdmin 目录复制一份到 nginx 服务器 web 目录下,

cp -r phpMyAdmin ./nginx/html/

我这里 web 目录是 /usr/share/nginx/html/ ,然后找到 cd /etc/phpMyAdmin/ 目录下的config.inc.php 进行如下编辑

$cfg['blowfish_secret'] = 'xiaochi',// 随便设置,只是一个短语密码
$cfg['Servers'][$i]['host'] = 'localhost'; // MySQL hostname or IP address
$cfg['Servers'][$i]['port'] = '3306'; 
$cfg['Servers'][$i]['auth_type'] = 'cookie';// cookie表示每次都要输入用户密码,linux下不能用 config,不然谁都能直接访问
$cfg['Servers'][$i]['user'] = 'root';
$cfg['Servers'][$i]['password'] = '*************';    //数据库密码
$cfg['Servers'][$i]['extension'] = 'mysql';

然后重启 php-fpm、mysqld

 service php-fpm reload
 service mysqld reload

然后 地址栏输入:http://公网ip/phpMyAdmin/
此时,访问时可能会报错 mb_string is missing。 然后安装 php-mbstring扩展:

yum install php-mbstring

然后重启 php-fpm、mysqld。就ok了。

六、composer安装

下载composer

curl -sS https://getcomposer.org/installer | php
//将composer.phar文件移动到bin目录以便全局使用composer命令
mv composer.phar /usr/local/bin/composer
//切换国内源
#全局设置
composer config -g repo.packagist composer https://packagist.phpcomposer.com

#中国镜像
composer config -g repos.packagist composer https://php.cnpkg.org

 

linux部署 lnmp 常用命令

yum list | grep php*(#查看所以php安装包)
yum remove 名称(卸载,如:yum remove nginx)
ps -le / ps -e(#查看所有进程)
ps -le | grep 应用程序名称(如:mysqld、redis) (#查看具体进程)
-------如:查看 mysql 进程
-------------ps -le | grep mysqld ;// ps -e | grep mysqld也可以

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值