记得当初第一次接触Linux部署php的项目,也是很头疼网上的资料也并不是那么全面,导致踩了不少的坑太难了。
简单介绍下lnmp
lnmp 其实是Linux+Nginx+Mysql+Php是缩写,与 lamp 其实就相差使用nginx 还是apache,目前lnmp环境的应用比较多。
搭建好之后的环境
php版本:7.1.33
mysql版本:5.7.29
nginx版本:1.16.1
准备好开始搭建
yum安装nginx
yum update //更新yum
安装nginx最新源
yum localinstall http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
yum repolist enabled | grep "nginx*"
安装nginx
yum -y install nginx
启动nginx
service nginx start
设置nginx服务器开机自启动
systemctl enable nginx.service
检查开机自动是否设置成功
systemctl list-dependencies | grep nginx
浏览器中输入公网ip,检测是否安装成功
如果出现无法访问,那么需要检查服务器是否开启80端口
1. 阿里——云服务器:添加安全组规则
2. 阿里——轻量服务器:防火墙里添加规则
使用yum安装mysql5.7
安装mysql源
yum -y localinstall http://dev.mysql.com/get/mysql57-community-release-el7-7.noarch.rpm
yum repolist enabled | grep "mysql.*-community.*"
安装mysql
yum -y install mysql-community-server install mysql-community-devel
启动mysql
service mysqld start
检查mysql启动是否正常
service mysqld status 或者 ps -ef | grep mysql
设置mysqld服务开机自启动
systemctl enable mysqld.service
检查mysqld开机自启动是否设置成功
systemctl list-dependencies | grep mysqld
mysql5.7以后的争强了安全机制, 所以使用yum安装,启动会系统会自动生成一个随机的密码,修改mysql密码
查看mysql的随机密码
grep 'temporary password' /var/log/mysqld.log
使用查询得到的随机密码在终端登录
mysql -u root -p 更改密码(mysql文档规定,密码必须包括大小写字母数字加特殊符号>8位)
ALTER USER 'root'@'localhost' IDENTIFIED BY 'Yourpassword';
退出mysql客户端,用刚才修改的密码登录确保密码修改成功
exit;
mysql -u root -p
//授权navicat 并且需要开启服务器3306端口
grant all privileges on *.* to 'root'@'%' identified by '123ZYjie.' with grant option;
设置完之前记得设置sql_mode:如果不设置会出现(分组、字段问题)
select @@session.sql_mode; //查看当前数据库开启的模式
永久修改的方法编辑my.cnf文件:vim /etc/my.cnf
[mysqld]中添加下面代码:
sql_mode = NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
安装php7.1
安装php源
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
检查源是否安装成功
yum repolist enabled | grep "webtatic*"
安装php扩展源
yum -y install php71w php71w-fpm
yum -y install php71w-mbstring php71w-common php71w-gd php71w-mcrypt
yum -y install php71w-mysql php71w-xml php71w-cli php71w-devel
yum -y install php71w-pecl-memcached php71w-pecl-redis php71w-opcache
验证php7.1.x和扩展是否安装成功
验证php是否安装成功
php -v
验证对应的扩展是否安装成功
php -m
设置php-fpm并检测php-fpm的运行状态
启动php-fpm
service php-fpm start
检查启动是否成功
service php-fpm status
设置开机自启动
systemctl enable php-fpm.service
检查开机自启动是否设置成功
systemctl list-dependencies | grep php-fpm
ps -ef | grep php-fpm
在执行完以上命令之后lnmp环境就算已经都安装好了,接下来就是开始配置nginx,让项目运行起来了,使用yum安装的nginx配置文件是在 /etc/nginx/conf.d 目录下,随便创建一个文件黏贴nginx配置即可。之所以这样做是因为nginx 在运行的时候会将 conf.d下的所有文件合并到 /etc/nginx/nginx.conf 文件中
nginx配置如下:
server{
listen 80;
server_name vswxx.top; #这是我的域名
index index.html index.php;
root /home/public/CRMEB; #这是我项目放的位置
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
#访问路径的文件不存在则重写URL转交给ThinkPHP处理
#!-e用来判断是否存在文件或目录
if (!-e $request_filename){
rewrite ^(.*)$ /index.php?s=$1 last;
break;
}
index index.html index.php;
try_files $uri $uri/ /index.php?$query_string;
}
error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ .php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
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;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /.ht {
deny all;
}
}
到这里LNMP环境就已经算是搭建完毕了。