Ubuntu16.04搭建LNMP环境

2018-12-13  本文已影响0人  在牛魔角上狂码

1、准备

安装language-pack-en-base包,解决不同语言之间可能发生的冲突

apt update

apt install language-pack-en-base

locale-gen en_US.UTF-8

2、配置环境

安装php7.1

root@iZwz956snfyrvah6yq8sa4Z:~# apt-get install php7.1
Reading package lists... Done
Building dependency tree       
Reading state information... Done
E: Unable to locate package php7.1
E: Couldn't find any package by glob 'php7.1'
E: Couldn't find any package by regex 'php7.1'

直接运行安装php7.1出错,报没有php7.1这个安装包

使用ppa方式安装php7.1

apt-get install software-properties-common

LC_ALL=en_US.UTF-8 add-apt-repository ppa:ondrej/php

完成后,更新安装包

apt update

apt-cache search php7.1

检验是否安装成功,成功了就能安装php7.1了

apt-get isntall php7.1-fpm

安装完成后

php -v

显示

root@iZwz956snfyrvah6yq8sa4Z:~# php -v
PHP 7.1.15-1+ubuntu16.04.1+deb.sury.org+2 (cli) (built: Mar  6 2018 11:10:13) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.1.15-1+ubuntu16.04.1+deb.sury.org+2, Copyright (c) 1999-2018, by Zend Technologies

安装php7.1的模块

apt-get -y install php7.1-mysql

apt-get -y install php7.1-mysql

apt-get install php7.1-curl php7.1-xml php7.1-mcrypt php7.1-json php7.1-gd php7.1-mbstring

安装mysql

apt-get install mysql-server mysql-client

过程需要输入密码

安装完成后

root@iZwz956snfyrvah6yq8sa4Z:~# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 5.7.21-0ubuntu0.16.04.1 (Ubuntu)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> exit

如果安装后,发现MySQL版本过低的话,需要升级下

cd ~
wget https://dev.mysql.com/get/mysql-apt-config_0.8.1-1_all.deb
sudo dpkg -i mysql-apt-config_0.8.1-1_all.deb

安装Nginx

卸载Apache2

由于阿里云系统自动安装了Apache2,如果不卸载Apache2的话可能会出现各种奇怪的错误,所以先卸载Apache2

sudo apt-get --purge remove apache-common 

sudo apt-get --purge remove apache-common 

找到没有删除掉的配置文件,一一删除

sudo find /etc -name "*apache*" |xargs  rm -rf  

sudo rm -rf /var/www 

sudo rm -rf /etc/libapache2-mod-jk 

sudo rm -rf /etc/init.d/apache2 

sudo rm -rf /etc/apache2 

安装Nginx

直接使用sudo apt install nginx 安装的Nginx版本比较低

现在是ppa的方式安装

sudo apt-get install software-properties-common

sudo add-apt-repository ppa:nginx/stable 

sudo apt-get update

sudo apt-get install nginx

安装完成后,使用服务器IP访问,出现

Welcome to nginx!
If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.

Thank you for using nginx.

安装成功,如果一直在加载,则登录阿里云,到安全组配置里看看是否添加了http(80)

配置nginx

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

    root /home/chase/test;

    index index.php index.html index.htm;

    server_name api.test;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        try_files $uri /index.php =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/run/php/php7.1-fpm.sock; #对应/etc/php/7.1/fpm/pool.d/www.conf中的listen = /run/php/php7.1-fpm.sock
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

卸载Nginx

罗列与Nginx相关的软件

dpkg --get-selections|grep nginx

显示

nginx                       install
nginx-common                    install
nginx-core                  install

删除与Nginx相关的软件

sudo apt-get --purge remove nginx

sudo apt-get --purge remove nginx-common

sudo apt-get --purge remove nginx-core

自动移除全部不使用的软件包

sudo apt-get autoremove
上一篇下一篇

猜你喜欢

热点阅读