Ubuntu下搭建lnmp的过程

前言:萌新瑟瑟发抖,没试过所以就想记录一下

1.Nginx的安装

apt-get update
apt-get install nginx

安装后可以查看版本号码:

nginx -v

这个时候访问ip,就可以看到welcome Nginx的页面了

2.安装mysql

apt-get install mysql-server-5.7 mysql-client-5.7

安装时候会提示输入数据库密码 安装后 可以进入mysql验证

mysql -uroot -p密码

3.安装PHP7.2

apt-get install software-properties-common python-software-properties
add-apt-repository ppa:ondrej/php && apt-get update
apt-get -y install php7.2

安装常用扩展:
apt-get install php7.2-fpm php7.2-mysql php7.2-curl php7.2-json php7.2-mbstring php7.2-xml  php7.2-intl

后面想安装什么扩展,只要参照这个命令:
比如安装gd图片处理扩展
apt-get install php7.2-gd 这样就可以了

4.修改Nginx配置

命令重启Nginx和PHP-fpm

service nginx restart
service php7.2-fpm restart

配置文件路径在 /etc/nginx/nginx.conf 最下面有两行
include /etc/nginx/conf.d/.conf;
include /etc/nginx/sites-enabled/
;
这边是/etc/nginx/sites-enabled/default

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

        # SSL configuration
        #
        # listen 443 ssl default_server;
        # listen [::]:443 ssl default_server;
        #
        # Note: You should disable gzip for SSL traffic.
        # See: https://bugs.debian.org/773332
        #
        # Read up on ssl_ciphers to ensure a secure configuration.
        # See: https://bugs.debian.org/765782
        #
        # Self signed certs generated by the ssl-cert package
        # Don't use them in a production server!
        #
        # include snippets/snakeoil.conf;

        root /var/www/demo1;

        # Add index.php to the list if you are using PHP
        index index.php index.html 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 the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
        #
        #       # With php7.0-cgi alone:
        #       fastcgi_pass 127.0.0.1:9000;
        #       # With php7.0-fpm:
                fastcgi_pass unix:/run/php/php7.2-fpm.sock;
        }

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


# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
#server {
#       listen 80;
#       listen [::]:80;
#
#       server_name example.com;
#
#       root /var/www/example.com;
#       index index.html;
#
#       location / {
#               try_files $uri $uri/ =404;
#       }
#}

注意其中一个是修改root 和index.php
然后注意fastcgi_pass unix:/run/php/php7.2-fpm.sock;
创建www/demo1/index.php 访问 http://106.12.112.85 即可

如果没有域名但又想有多个项目的话,可以location路由匹配或者多个端口。多端口可以再写个demo2.conf来监听81端口,如下配置

server {
       listen 81;
       listen [::]:81;
#
       server_name _;
#
        root /var/www/demo2;
        index index.php index.html;
#
       location / {
               try_files $uri $uri/ =404;
       }

        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
        #
        #       # With php7.0-cgi alone:
        #       fastcgi_pass 127.0.0.1:9000;
        #       # With php7.0-fpm:
                fastcgi_pass unix:/run/php/php7.2-fpm.sock;
        }

}

配置后重启Nginx和PHP-fpm即可访问到81端口的demo2项目 http://106.12.112.85:81/

5.Nginx和PHP-fpm

运行原理

最后修改:2019-10-15 07:08:33
0