个人linux学习总结(四)搭建Lnmp环境

熟悉LNMP使用场景
安装nginx
安装mysql或mariadb扩展
安装php7.X
配置nginx虚拟主机【基于端口、ip地址、域名】
实现反向代理和负载均衡
发布应用程序进行测试

**

Systemctl stop firewalld
Sentenforce 0
yum -y install nginx
nginx -v
rpm -Uvh  http://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm
yum -y install mysql-community-server
mysql -V
systemctl start mysqld
systemctl enable mysqld
systemctl daemon-reload
yum install \
https://repo.ius.io/ius-release-el7.rpm \
https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
yum -y install php70w-devel php70w.x86_64 php70w-cli.x86_64 php70w-common.x86_64 php70w-gd.x86_64 php70w-ldap.x86_64 php70w-mbstring.x86_64 php70w-mcrypt.x86_64  php70w-pdo.x86_64   php70w-mysqlnd  php70w-fpm php70w-opcache php70w-pecl-redis php70w-pecl-mongodb
php -v
cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak
vim /etc/nginx/nginx.conf
location / {
            index index.php index.html index.htm;
        }
 location ~ .php$ {
            root /usr/share/nginx/html;    #将/usr/share/nginx/html替换为您的网站根目录,本教程使用/usr/share/nginx/html作为网站根目录。
            fastcgi_pass 127.0.0.1:9000;   #Nginx通过本机的9000端口将PHP请求转发给PHP-FPM进行处理。
            fastcgi_index index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include fastcgi_params;   #Nginx调用fastcgi接口处理PHP请求。
        }                
systemctl start nginx 
systemctl enable nginx
grep 'temporary password' /var/log/mysqld.log
mysql_secure_installation
vim /usr/share/nginx/html/phpinfo.php
<?php echo phpinfo(); ?>
systemctl start php-fpm
systemctl enable php-fpm

**

基于端口配置

server
listen 81;#修改监听端口
server_name www.wjw.com;
root
/usr/share/nginx/html/wjw1;

Load configuration files for the default server block.

include /etc/nginx/default.d/*.conf; location/(
index index.php index.html index.htm;
location-.php$(
root /usr/share/nginx/html/wjw1;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME
$document_root$fastcgi_script_name; include fastcgi_params;
error_page 404 /404.html;
location =/404.html(
error_page 500 502 503 504 /50x.html;
基于域名配置
server
listen 80;
server_name www.wjw.cn;
root
/usr/share/nginx/html/wjw2;

Load configuration files for the default server block.

include /etc/nginx/default.d/*.conf; location/(
index index.php index.html index.htm;
location-.php$(
root /usr/share/nginx/html/wjw2;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME
$document_root$fastcgi_script_name; include fastcgi_params;
error_page 404 /404.html;
location =/404.html(
error_page 500 502 503 504 /50x.html;

基于ip配置

server
listen 192.168.128.130:80;
server_name www.wjw.cn;
root
/usr/share/nginx/html/wjw2;
~~# Load configuration files for the default server block.~~  
include /etc/nginx/default.d/*.conf; location/(
index index.php index.html index.htm;
location-.php$(
root /usr/share/nginx/html/wjw2;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME
$document_root$fastcgi_script_name; include fastcgi_params;
error_page 404 /404.html;
location =/404.html(
error_page 500 502 503 504 /50x.html;
虚拟路径
server
listen 80;
server_name www.wjw1.cn;
root
/usr/share/nginx/html/wjw;

Load configuration files for the default server block.

include /etc/nginx/default.d/*.conf; location/(
index index.php index.html index.htm;
location-.php$(
root /usr/share/nginx/html/wjw;
Rewrite /wjw/(.*\.php) /$1 break;#重命名wjw
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME
$document_root$fastcgi_script_name; include fastcgi_params;
error_page 404 /404.html;
location =/404.html(
error_page 500 502 503 504 /50x.html;

反向代理

server {
	listen       80;
	server_name  192.168.128.129;
	location / {
		root   html;
		index  index.html index.htm;
		proxy_pass  http://127.0.0.1:8080
	}

}
负载均衡

http {
    upstream tomcats {
        server 192.168.128.130:8080;
        server 192.168.128.131:8080;
        server www.wjw.com:8080;
    }

    server {
        listen 80;
        location / {
            proxy_pass http://tomcats;
        }
    }
}
Cd /var/www/html/wjw1
Cd /var/www/html/wjw2

Touch index.php

<?Php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {    die("连接失败: " . $conn->connect_error);}  $sql = "SELECT id, firstname, lastname FROM MyGuests";$result = $conn->query($sql); if ($result->num_rows > 0) {       while($row = $result->fetch_assoc()) {        echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";    }} else {    echo "0 结果";}$conn->close();?>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

neverう

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值