铭哥和佩佩的博客

铭哥和佩佩的博客分享Python、PHP、JavaScript、HTML5、CSS3等各种知识

ubuntu18.04安装LNMP

1、安装Nginx

安装

sudo apt-get install nginx

启动nginx服务

sudo systemctl start nginx.service

设置开机启动

sudo systemctl enable nginx.service

可以用浏览器通过IP地址访问,如果是本地且安装了图形界面可以通过 127.0.0.1访问到

2、安装MySql

安装

sudo apt-get install mysql-server mysql-client 

启动服务

sudo service mysql start

修改登录密码

sudo mysql      # 登入mysql
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'YourPassword';

注意:如果安装的是mysql8.0以上,修改密码用

alter user 'root'@'localhost' IDENTIFIED BY 'YourPassword';

3、安装PHP

sudo apt-get install php7.2 php7.2-fpm php7.2-mysql

4、配置Nginx

打开nginx配置文件

sudo vim /etc/nginx/sites-available/default

修改内容如下

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

        root /home/ubuntu/www;

        index index.html index.htm index.php index.nginx-debian.html;

        server_name _;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404;
        }

        # pass PHP scripts to FastCGI server
        #
        location ~ \.php$ {
                include snippets/fastcgi-php.conf;

                # With php-fpm (or other unix sockets):
                # fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
                # With php-cgi (or other tcp sockets):
                fastcgi_pass 127.0.0.1:9000;
        }
}

注意配置文件中root指定的目录就是你的web根目录了,html文件、php文件放在这里就可以了

5、配置PHP

打开php-pfm配置文件

sudo vim /etc/php/7.2/fpm/pool.d/www.conf

加入下面一行代码

listen = 127.0.0.1:9000

重启php-fpm服务

sudo service php7.2-fpm restart

6、验证是否成功

在web根目录 /home/ubuntu/www 下创建文件 phpinfo.php, 里面写入代码

phpinfo()

访问即可

none

添加新评论