ubuntu18.04安装lamp

Ubuntu18.04安装LNMP环境

  1. 更新apt-get源:
    1. 备份原源列表:sudo mv /etc/apt/sources.list /etc/apt/sources.list.bak
    2. 修改源列表:sudo vi /etc/apt/sources.list
    deb http://mirrors.163.com/ubuntu/ bionic main restricted universe multiverse 
    deb http://mirrors.163.com/ubuntu/ bionic-security main restricted universe multiverse 
    deb http://mirrors.163.com/ubuntu/ bionic-updates main restricted universe multiverse 
    deb http://mirrors.163.com/ubuntu/ bionic-proposed main restricted universe multiverse 
    deb http://mirrors.163.com/ubuntu/ bionic-backports main restricted universe multiverse 
    deb-src http://mirrors.163.com/ubuntu/ bionic main restricted universe multiverse 
    deb-src http://mirrors.163.com/ubuntu/ bionic-security main restricted universe multiverse 
    deb-src http://mirrors.163.com/ubuntu/ bionic-updates main restricted universe multiverse 
    deb-src http://mirrors.163.com/ubuntu/ bionic-proposed main restricted universe multiverse 
    deb-src http://mirrors.163.com/ubuntu/ bionic-backports main restricted universe multiverse
    
    deb http://mirrors.ustc.edu.cn/ubuntu/ xenial main restricted universe multiverse 
    deb http://mirrors.ustc.edu.cn/ubuntu/ xenial-security main restricted universe multiverse 
    deb http://mirrors.ustc.edu.cn/ubuntu/ xenial-updates main restricted universe multiverse 
    deb http://mirrors.ustc.edu.cn/ubuntu/ xenial-proposed main restricted universe multiverse 
    deb http://mirrors.ustc.edu.cn/ubuntu/ xenial-backports main restricted universe multiverse 
    deb-src http://mirrors.ustc.edu.cn/ubuntu/ xenial main restricted universe multiverse 
    deb-src http://mirrors.ustc.edu.cn/ubuntu/ xenial-security main restricted universe multiverse 
    deb-src http://mirrors.ustc.edu.cn/ubuntu/ xenial-updates main restricted universe multiverse 
    deb-src http://mirrors.ustc.edu.cn/ubuntu/ xenial-proposed main restricted universe multiverse 
    deb-src http://mirrors.ustc.edu.cn/ubuntu/ xenial-backports main restricted universe multiverse
    
    deb http://mirrors.aliyun.com/ubuntu/ vivid main restricted universe multiverse
    deb http://mirrors.aliyun.com/ubuntu/ vivid-security main restricted universe multiverse
    deb http://mirrors.aliyun.com/ubuntu/ vivid-updates main restricted universe multiverse
    deb http://mirrors.aliyun.com/ubuntu/ vivid-proposed main restricted universe multiverse
    deb http://mirrors.aliyun.com/ubuntu/ vivid-backports main restricted universe multiverse
    deb-src http://mirrors.aliyun.com/ubuntu/ vivid main restricted universe multiverse
    deb-src http://mirrors.aliyun.com/ubuntu/ vivid-security main restricted universe multiverse
    deb-src http://mirrors.aliyun.com/ubuntu/ vivid-updates main restricted universe multiverse
    deb-src http://mirrors.aliyun.com/ubuntu/ vivid-proposed main restricted universe multiverse
    deb-src http://mirrors.aliyun.com/ubuntu/ vivid-backports main restricted universe multiverse
    
    1. 更新:sudo apt-get updatesudo apt-get upgrade
  2. 安装vimsudo apt-get install -y vim
  3. 设置使用root登陆ssh
    1. 设置root密码(Ubuntu默认root用户是没有密码的):sudo passwd root
    2. 安装openssh serversudo apt-get install openssh-server
    3. 配置openssh serversudo vim /etc/ssh/sshd_confg
    PermitRootLogin no 改为 PermitRootLogin yes
    
    1. 重启:sudo service ssh restart
  4. 安装LNMP
    1. 安装nginxsudo apt-get install nginx
      1. 启动nginxsudo /etc/init.d/nginx startsudo service nginx start
      2. 安装net-toolssudo apt-get install net-tools
      3. 检查是否启动成功:sudo lsof -i:80
      4. 在浏览器中输入:127.0.0.1:80可以看到nginx欢迎页面
    2. 安装phpphp-fpmsudo apt-get install php7.2 php7.2-fpm
      1. 检查是否安装成功:php -v
      2. 说明:
        • php-fpm与nginx通信方式有两种,一种是基于tcp的Internet domain socket方式,一种是UNIX domain socket方式。
        • UNIX domain socket可以使同一台操作系统上的两个或多个进程进行数据通信。UNIX domain socket接口和Internet domain socket很像,但它不是用网络底层协议来通信。
        • 服务器压力不大的情况下,这两种方式性能差别不大,但在压力比较满的时候,用UNIX domain socket方式,效果很好。
        • 网站根目录通常在 /etc/nginx/sites-available/default配置文件中,nginx已经将php和php-fpm的整合准备好了,还需要在这个文件中改以下东西
          在这几行下面加入新内容
        #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;
        #}
        
        新内容:
        location ~ .php$ {
           include snippets/fastcgi-php.conf;
           # With php-fpm (or other unix sockets):
           fastcgi_pass unix:/run/php/php7.2-fpm.sock;
        }
        
      3. 修改php-fpmsudo vim /etc/php/7.2/fpm/pool.d/www.conf
      listen = /run/php/php7.2-fpm.sock
      
      1. 重启nginxsudo /etc/init.d/nginx restartsudo service nginx restart
      2. 重启php-fpmsudo /etc/init.d/php7.2-fpm restart
    3. 安装mysqlsudo apt-get -y install mysql-server mysql-client php7.2-mysql
      如果没有提示输入密码则使用:mysql_secure_installation进行设置
  5. 配置虚拟机可以外部访问(防火墙):
    1. 检查是否已经安装iptableswhereis iptables
    2. 安装iptablessudo vim apt-get install -y iptables
    3. 检查防火墙信息:sudo iptables -L
    4. 添加防火墙信息:sudo vim /etc/iptables.rules
    *filter
    :INPUT DROP [0:0]
    :FORWARD ACCEPT [0:0]
    :OUTPUT ACCEPT [0:0]
    :syn-flood - [0:0]
    -A INPUT -i lo -j ACCEPT
    -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
    -A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
    -A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT
    -A INPUT -p tcp -m state --state NEW -m tcp --dport 443 -j ACCEPT
    -A INPUT -p tcp -m state --state NEW -m tcp --dport 888 -j ACCEPT
    -A INPUT -p tcp -m state --state NEW -m tcp --dport 8888 -j ACCEPT
    -A INPUT -p tcp -m state --state NEW -m tcp --dport 3306 -j ACCEPT
    -A INPUT -p tcp -m state --state NEW -m tcp --dport 6379 -j ACCEPT
    -A INPUT -p icmp -m limit --limit 100/sec --limit-burst 100 -j ACCEPT
    -A INPUT -p icmp -m limit --limit 1/s --limit-burst 10 -j ACCEPT
    -A INPUT -p tcp -m tcp --tcp-flags FIN,SYN,RST,ACK SYN -j syn-flood
    -A INPUT -j REJECT --reject-with icmp-host-prohibited
    -A syn-flood -p tcp -m limit --limit 3/sec --limit-burst 6 -j RETURN
    -A syn-flood -j REJECT --reject-with icmp-port-unreachable
    COMMIT
    
    1. 使防火墙规则生效:iptables-restore < /etc/iptables.rules
    2. 开机自启动防火墙:sudo vim /etc/network/if-pre-up.d/iptables
    #!/bin/bash
    iptables-restore < /etc/iptables.rules
    
    1. 添加执行权限:# chmod +x /etc/network/if-pre-up.d/iptables
    2. 查看规则是否生效:sudo iptables -L -n
    3. 检查当前IP:ifconfig,确定虚拟机网络设置为:网桥模式
    4. 外部访问:虚拟机IP确定是否访问到nginx
  6. 配置nginx
    1. nginx.conf配置:
    #user  nobody;    
    user root root; 
    worker_processes auto; #nginx工作进程数,一般设置为cpu核数 
    #error_log  logs/error.log; 
    #error_log  logs/error.log  notice; 
    #error_log  logs/error.log  info; 
    #pid        logs/nginx.pid; 
    events { 
    	worker_connections 1024; # 最大连接数
    } 
    http { 
    	include mime.types; 
    	default_type application/octet-stream; 
    	#log_format  main  '$remote_addr - $remote_user [$time_local] "$request" ' 
    	#                  '$status $body_bytes_sent "$http_referer" ' 
    	#                  '"$http_user_agent" "$http_x_forwarded_for"'; 
    
    	#access_log  logs/access.log  main; 
    	sendfile on; 
    	#keepalive_timeout  0; 
    	keepalive_timeout 60; # 最大连接时间 
    	client_max_body_size 120M; # 客户端请求体最大体积
    	gzip on; 
    	gzip_min_length 1k; 
    	gzip_buffers 4 16k; 
    	gzip_http_version 1.0; 
    	gzip_comp_level 2; 
    	gzip_types application/json text/plain application/javascript application/x-javascript text/css application/xml; 
    	gzip_vary on; 
    	#gzip  on; #导入外部服务器配置文件存放地址 
    	include /etc/nginx/conf.d/*.conf; 
    }
    
    1. 配置后台api服务器跳转:
    server { 
    	listen 80; 
    	server_name api.service.com; #图片路径拦截,定位到图片静态资源 
    	location ~ /upload/.*$ { 
    		root /www/resources/; 
    		expires 30d; 
    	} 
    	location / { #跳转到指点tomcat服务器 
    		proxy_pass http://localhost:8080; 
    		proxy_set_header Host $host; 
    		proxy_set_header X-Real-IP $remote_addr; 
    		proxy_set_header REMOTE-HOST $remote_addr; 
    		proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
    	} 
    }
    
    1. 配置h5静态页面跳转
    server { 
    	listen 80; 
    	server_name service.com; 
    	#h5页面存放路径 
    	root '/www/orancrabv2-fe/server'; 
    		index index.html; 
    	error_page 404 = /404.html; 
    	location ~ .*\.(js|css|ico|gif|jpg|png|svg|woff|woff2|ttf|eot)$ { 
    		log_not_found off; 
    		access_log off; 
    		expires 7d; 
    	} 
    }
    
    1. nginx集群方式的配置:
    #集群的服务器 
    upstream local_tomcat{ 
    	server localhost:8080; 
    	server localhost:8081; 
    } 
    server { 
    	listen 80; server_name www.tomcat1.com; 
    	location ~ /upload/.*$ { 
    		root /www/resources/; 
    		expires 30d; 
    	} 
    	location / { 
    		proxy_pass http://local_tomcat; 
    		proxy_set_header Host $host; 
    		proxy_set_header X-Real-IP $remote_addr; 
    		proxy_set_header REMOTE-HOST $remote_addr; 
    		proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
    	} 
    }
    
  7. 安装PHP扩展:
    1. sudo apt-get install -y php7.2-dev
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值