ubuntu18.04 系统 安装 LNMP环境

2018-05-31  本文已影响0人  小山人

ubuntu18.04安装LNMP

安装nginx

sudo apt-get install nginx

#1:使用dpkg -S nginx来搜索nginx的相关文件
#2:nginx的默认网站目录是/var/www/html/
#3:默认的nginx网站配置文件为/etc/nginx/sites-available/default
#4:日志文件在/var/log/nginx/

启用nginx服务器: sudo /etc/init.d/nginx start或者sudo service nginx start

使用netstat -anp则可以看到80端口已经处于LISTEN状态了。
直接查看80端口可以使用命令:sudo lsof -i:80

查看进程 ps aux | grep nginx

安装mysql

sudo apt-get install mysql-server mysql-client

#在ubuntu18.04中apt安装mysql并没有提示设置密码,用户名默认的不是root,而是debian-sys-maint
#查看的路径在/etc/mysql/debian.cnf 
# Automatically generated for Debian scripts. DO NOT TOUCH!
[client]
host     = localhost
user     = debian-sys-maint
password = l9iU7T2QILPjNzeH
socket   = /var/run/mysqld/mysqld.sock
[mysql_upgrade]
host     = localhost
user     = debian-sys-maint
password = l9iU7T2QILPjNzeH
socket   = /var/run/mysqld/mysqld.sock
#我们可以使用该用户登录mysql,修改root可以直接登录
update mysql.user set authentication_string=password('123456'),plugin='mysql_native_password' where User='root' and Host = 'localhost';
#重启mysql服务
sudo service mysql restart

安装php

sudo apt-get install php7.2-fpm php7.2-mbstring php7.2-xml php7.2-mysql php7.2-common php7.2-gd php7.2-json php7.2-cli php7.2-curl

#查看php版本
php -v

#重启服务
service nginx restart
service mysql restart

浏览器显示错误

#修改/etc/php/7.2/fpm/php.ini
display_errors=on

解析php

修改/etc/nginx/sites-available/default文件

location ~ \.php$ {
                include snippets/fastcgi-php.conf
                fastcgi_pass unix:/run/php/php7.2-fpm.sock;
        }

nginx配置虚拟主机

在/etc/nginx/sites-available目录下,新建lnmptest.com文件

内容为:
server {
        listen 80;
        listen [::]:80;

        server_name lnmptest.com;

        root /var/www/html/test;
        index index.html index.php;

        location / {
                try_files $uri $uri/ =404;
        }

        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/run/php/php7.2-fpm.sock;
        }
}

向sites-enable添加快捷方式

sudo ln -s /etc/nginx/sites-available/lnmptest.com /etc/nginx/sites-enabled/lnmptest.com

修改/etc/hosts文件,添加127.0.0.1 lnmptest.com

#重启服务
service nginx restart
service php7.2-fpm restart

#访问lnmptest.com

添加项目文件

#/var/www/html/下新建test目录,新建index.php
<?php
echo 'hello word';
?>

nginx PATH_INFO配置

修改配置文件

root@jw-server:/home/jw# vim /etc/nginx/sites-available/lnmptest.com 

server {
        listen 80;
        listen [::]:80;

        server_name lnmptest.com;

        root /var/www/html/test;
        index index.html index.php;

        location / {
                try_files $uri $uri/ =404;
        }

        location ~ \.php(.*)$ {  #修改php后的正则验证
                include snippets/fastcgi-php.conf;

                fastcgi_pass unix:/run/php/php7.2-fpm.sock;
                fastcgi_param PATH_INFO $1;  #添加path_info值
        }
}

加载新配置

nginx -s reload

安装laravel

修改composer为国内镜像

composer config -g repo.packagist composer https://packagist.phpcomposer.com

composer 安装laravel

composer create-project --prefer-dist laravel/laravel test

修改配置文件url重写

server {
        listen 80;
        listen [::]:80;

        server_name lnmptest.com;

        root /var/www/html/test/public;
        index index.html index.php;

        location / {
                if (!-e $request_filename){ #判断文件是否存在,如果不存在的,则重写路由
                        rewrite (.*)$ /index.php/$1; #去掉url的index.php
                }
                
        }

        location ~ \.php(.*)$ {
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/run/php/php7.2-fpm.sock;
                fastcgi_param PATH_INFO $1;
        }
}

ps:去掉index.php也可写成 rewrite index.php/(.*)$ $1;

laravel推荐优雅的url重写(效果和之前的url重写相同)推荐

server {
        listen 80;
        listen [::]:80;

        server_name lnmptest.com;

        root /var/www/html/test/public;
        index index.html index.php;

        location / {
                try_files $uri $uri/ /index.php?$query_string;  #url重写
        }

        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/run/php/php7.2-fpm.sock;
        }
}

500错误,修改文件夹权限

chmod -R 777 storage/
chmod -R 777 vendor/
chmod -R 777 bootstrap/

生成.env文件和新的key

.env.example 改名使用命令 copy 修改为 .env
使用命令 php artisan key:generate  获取密码,自动保存到 .env

安装samba

开始需要我们做的是先在我们的ubuntu下安装好samba

#安装samba:
sudo apt-get install samba
#安装smbclient:
sudo apt-get install smbclient

修改配置文件

#/etc/samba/smb.conf 编辑smb.conf 文件
在配置文件的最末尾加上:
[root]
comment = Shared Folder require password
path = /var/www/html/
public = yes
writable = yes
valid users = root
create mask = 0777
directory mask = 0777
force user = root
force group = root
available = yes
browseable = yes
保存后重新启动samba:sudo /etc/init.d/samba restart

设置用户和密码

sudo smbpasswd -a share 
#接着提示输入密码。(前提是添加了此用户:useradd share)
#否则系统在没有此用户的前提下按默认操作是会报如下错的:
#增加samba用户提示Failed to add entry for user
smbpasswd -a share
New SMB password:
Retype new SMB password:
Failed to add entry for user share.
#解决办法:
#这是因为没有加相应的系统账号,所以会提示Failed to add entry for user的错误,只需增加相应的系统账号share就可以了:
groupadd share -g 6000
useradd share -u 6000 -g 6000 -s /sbin/nologin -d /dev/null

#which 服务  查看看安装路径
#cmd中输入       \\ip地址
上一篇下一篇

猜你喜欢

热点阅读