LNMP环境搭建教程及linux常用命令及问题
Posted 年华流失
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LNMP环境搭建教程及linux常用命令及问题相关的知识,希望对你有一定的参考价值。
1. 常用命令
gedit index.php 编辑文件
grep -Rn "^listen" `find / -name www.conf` //find查找的文件,再用grep搜索
grep -Rn "^listen.*sock" `find / -name www.conf`
1.使用YUM查找软件包
命令:yum search
2.列出所有可安装的软件包
命令:yum list
3.列出所有可更新的软件包
命令:yum list updates
4.列出所有已安装的软件包
命令:yum list installed
5.列出所有已安装但不在 Yum Repository 内的软件包
命令:yum list extras
6.列出所指定的软件包
命令:yum list
7.使用YUM获取软件包信息
命令:yum info
8.列出所有软件包的信息
命令:yum info
9.列出所有可更新的软件包信息
命令:yum info updates
10.列出所有已安装的软件包信息
命令:yum info installed
11.列出所有已安装但不在 Yum Repository 内的软件包信息
命令:yum info extras
12.列出软件包提供哪些文件
命令:yum provides
1. 查看ip
ifconfig -a
hostname -I
2. 查看yum仓库
yum repolist
3. 以某个yum源安装
yum install --enablerepo=epel
4.yum基本命令
1.使用YUM查找软件包
命令:yum search
2.列出所有可安装的软件包
命令:yum list
3.列出所有可更新的软件包
命令:yum list updates
4.列出所有已安装的软件包
命令:yum list installed
5.列出所有已安装但不在 Yum Repository 内的软件包
命令:yum list extras
6.使用YUM获取软件包信息
命令:yum info
7.列出所有可更新的软件包信息
命令:yum info updates
8.列出所有已安装的软件包信息
命令:yum info installed
9.列出所有已安装但不在 Yum Repository 内的软件包信息
命令:yum info extras
10.列出软件包提供哪些文件
命令:yum provides
5.安装remi和epel源
安装remi源
cd /etc/yum.repos.d
wget http://rpms.remirepo.net/enterprise/remi-release-8.rpm
yum localinstall remi-release-8.rpm
安装epel源
yum -y install epel-release
查看仓库
yum repolist
当前仓库搜索
yum list --enablerepo=remi-safe | grep php74
清理缓存并生成新的缓存。把软件包信息下载到本地
yum clean all
yum -y makecache
6. root方式直接登录系统
vi /etc/gdm/custom.conf
在[daemon]下增加
AutomaticLoginEnable=True
AutomaticLogin=root
7. 防火墙
firewalld ,端口开启后,记得重启
firewall-cmd --reload //重启防火墙
开启,重启,关闭
systemctl start firewalld
systemctl status firewalld
systemctl stop firewalld
查看端口是否开放
firewall-cmd --query-port=80/tcp
开放80端口(-----记得重启防火墙生效-----)
firewall-cmd --permanent --add-port=80/tcp
移除端口
firewall-cmd --permanent --remove-port=8080/tcp
重启防火墙(修改配置后要重启防火墙)
firewall-cmd --reload
查看所有永久开放的端口(默认为空)
# firewall-cmd --list-ports --permanent
参数解释
1、firwall-cmd:是Linux提供的操作firewall的一个工具;
2、--permanent:表示设置为持久;
3、--add-port:标识添加的端口
iptables
systemctl enable iptables #注册iptables服务 相当于以前的chkconfig iptables on
systemctl start iptables #开启服务
systemctl status iptables #查看状态
iptables -A INPUT -i lo -j ACCEPT #允许来自于lo接口的数据包(本地访问)
iptables -A INPUT -p tcp --dport 22 -j ACCEPT #开放22端口
iptables -A INPUT -p tcp --dport 21 -j ACCEPT #开放21端口(FTP)
iptables -A INPUT -p tcp --dport 80 -j ACCEPT #开放80端口(HTTP)
iptables -A INPUT -p tcp --dport 443 -j ACCEPT #开放443端口(HTTPS)
iptables -A INPUT -p icmp --icmp-type 8 -j ACCEPT #允许ping
8. 服务目录
我们常用的systemctl restart nginx 可启动 脚本/etc/init.d/nginx
/etc/init.d是指向/etc/rc.d/init.d的软连接
9. 服务
这是nginx.service目录的文件
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/run/nginx.pid
# Nginx will fail to start if /run/nginx.pid already exists but has the wrong
# SELinux context. This might happen when running `nginx -t` from the cmdline.
# https://bugzilla.redhat.com/show_bug.cgi?id=1268621
ExecStartPre=/usr/bin/rm -f /run/nginx.pid #启动服务之前执行的命令
ExecStartPre=/usr/sbin/nginx -t #启动服务之前执行的命令
ExecStart=/usr/sbin/nginx # 启动
ExecReload=/bin/kill -s HUP $MAINPID #重启
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=mixed
PrivateTmp=true
[Install]
WantedBy=multi-user.target
2. 环境搭建
-
nginx配置文件,nginx.conf
-
php 配置 www.conf
-
mysql配置my.cnf
用某个yum源安装
yum install mysql --enablerepo=epel
1 安装nginx
nginx的配置文件时httpd.conf
//安装
yum install nginx
//设置开机启动
systemctl enable nginx
//查看状态
systemctl status nginx
//启动
systemctl start nginx
//重启
systemctl restart nginx
nginx.conf配置
user root; # 这里修改root,修改为其他用户可能会因文件夹权限不足报错
worker_processes auto;
error_log /var/log/nginx/error.log; #错误日志文件,当服务器启动不了,可以查看错误信息
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
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 /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name localhost;
root /www;
index index.php index.html index.htm;
//允许请求头
# add_header \'Access-Control-Allow-Headers\' *;
# laravel 隐藏index.php
# location / {
# try_files $uri $uri/ /index.php?$query_string;
# }
# Load configuration files for the default server block.
# include /etc/nginx/default.d/*.conf; # 注释这个,通过外部加载配置,在手动添加:::2
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
# :::2
location ~ \\.php$ {
root /www;
fastcgi_pass unix:/run/php-fpm/www.sock; # sock方式
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;#这个可能不支持,用上面那个
include fastcgi_params;
}
}
# Settings for a TLS enabled server.
#
# server {
# listen 443 ssl http2 default_server;
# listen [::]:443 ssl http2 default_server;
# server_name _;
# root /usr/share/nginx/html;
#
# ssl_certificate "/etc/pki/nginx/server.crt";
# ssl_certificate_key "/etc/pki/nginx/private/server.key";
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 10m;
# ssl_ciphers PROFILE=SYSTEM;
# ssl_prefer_server_ciphers on;
#
# # Load configuration files for the default server block.
# include /etc/nginx/default.d/*.conf;
#
# location / {
# }
#
# error_page 404 /404.html;
# location = /40x.html {
# }
#
# error_page 500 502 503 504 /50x.html;
# location = /50x.html {
# }
# }
}
让nginx支持put,patch,delete请求
//如果非文件就交给index.php处理
if (!-e $request_filename) {
rewrite ^/(.*)$ /index.php/$1;
}
//如果DELETE|PUT|PATCH方法就交给index.php处理
if ($request_method ~* "(DELETE|PUT|PATCH)") {
rewrite ^/(.*)$ /index.php/$1;
}
//所有url交给index.phpc
location / {
index index.php index.html index.htm;
try_files $uri $uri/ /index.php?$query_string;
}
安装apache
yum install httpd -y
systemctl enable httpd
重启一下,就ok了
配置文件,httpd.conf
配置文件目录 Document_Root
php需要设置成路径形式
2. php74安装
php重启
systemctl start php-fpm
仓库目录
/etc/yum.repos.d
//查看所有的仓库
yum repolist
安装
第一步:
yum install epel-release
https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm
第三步:安装php74
yum --enablerepo=remi install php74-php
//查找php.ini文件路径
1. whereis php.ini
2. php --ini
3. find / -name php.ini
安装php及扩展
yum -y --enablerepo=remi-safe install php74-php php74-php-gd php74-php-xml php74-php-sockets php74-php-session php74-php-snmp php74-php-mysql php74-php-phpiredis php74-php-pecl-redis5
重启
php74
systemctl restart php74-php-fpm
systemctl enable php74-php-fpm
#运行并查看版本
php74 -v
#重启命令php-fpm systemctl restart php74-php-fpm
#添加自动启动 systemctl enable php74-php-fpm
#查看php7.4的安装路径
whereis php #链接php文件 ln -s /opt/remi/php74/root/usr/bin/php /usr/bin/php
修改配置文件
php的配置文件是 www.conf
1.搜索listen
有两种方式,一种路径,一种通过端口
listen = 127.0.0.1:9000
listen = unix:/run/php-fpm/www.sock
配置nginx支持php
location ~ \\.php$ {
root /var/www; #指定php的根目录
fastcgi_pass 127.0.0.1:9000;#php-fpm的默认端口是9000
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
4.1、查看当前selinux的状态。
/usr/sbin/sestatus
4.2、将SELINUX=enforcing 修改为 SELINUX=disabled 状态。
vi /etc/selinux/config
#SELINUX=enforcing
SELINUX=disabled
重启生效。reboot。
#SELINUX=enforcing
SELINUX=disabled
也可以使用命令修改
sed -i \'s/SELINUX=enforcing/SELINUX=disabled/\' /etc/selinux/config
配置完成后,当代码有错误时,直接显示HTTP ERROR 500错误
不显示错误信息,可以在代码上加上这个,
获取
ini_set("display_errors","On");
修改为On即可
3. 配置nginx与php工作
1. 修改php配置文件
-
查看php的连接方式,ip端口还是,文件
-
php配置文件为www.conf
使用命令修改
显示php错误信息
sudo sed -i \'s/display_errors = .*/display_errors = On/\' `find / -name php.ini`
sudo sed -i \'s/;date.timezone.*/date.timezone = PRC/\' `find / -name php.ini`
sudo sed -i \'s/post_max_size = .*/post_max_size = 50M/\' `find / -name php.ini`
sudo sed -i \'s/listen =.*/listen = 127.0.0.1:9000/\' `find / -name www`
2. 修改nginx配置文件
如果遇到配置不成功,可以查看nginx错误文件
报错
-
启动nginx,报下面的错误
主配置文件第69行定义调用脚本文件路径:
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
而Nginx识别不到/scripts路径,所以phpinfo验证信息就无法正常通过,需要修改如下:
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
这个错误,还有一种可能,就是, /home/qin500 这个文件夹的权限不足
还可以将nginx.conf文件里面的user 修改为root,也可以解决
chown -R root:root qin500
更改文件夹权限
3. nginx报错403四种解决方案
is forbidden (13: Permission denied
- nginx.conf 里面的user是否为root
- root 文件夹是否具有权限
- 在nginx.conf server{ 添加 index index.php index.html index.htm; }
- 是否开启 SELinux
4. 安装mysql
1、先检查系统是否安装有mysql
[root@localhost ~] #yum list installed mysql*
[root@localhost ~] #rpm –qa|grep mysql*
2、查看有没有安装包
[root@localhost ~] #yum list mysql*
3、安装mysql客户端,服务端
yum -y install mysql mysql-server mysql-devel
启动
systemctl restart mysqld
5、在/etc/my.cnf 文件中加入默认字符集
[root@localhost ~]#vim /etc/my.cnf
6 、创建root管理员
mysqladmin -u root password 密码
mysqladmin -u root password 1234
格式:mysqladmin -u用户名 -p旧密码 password 新密码
例子:mysqladmin -uroot -p123456 password 123
7 进入mysql容器中
mysql -u root -p
8、设置允许远程访问
开放防火墙的端口号
mysql增加权限:mysql库中的user表新增一条记录host为“%”,user为“root”。
use mysql;
UPDATE user SET `Host` = \'%\' WHERE `User` = \'root\' LIMIT 1;
5. 完全卸载mysql
查看yum是否安装过mysql
yum list installed mysql*
yum卸载
根据列表上的名字
yum remove mysql-community-client mysql-community-common mysql-community-libs mysql-community-libs-compat mysql-community-server mysql57-community-release
rm -rf /var/lib/mysql
rm /etc/my.cnf
rpm查看安装
rpm -qa | grep -i mysql
rpm 卸载
rpm -e mysql57-community-release-el7-9.noarch
rpm -e mysql-community-server-5.7.17-1.el7.x86_64
rpm -e mysql-community-libs-5.7.17-1.el7.x86_64
rpm -e mysql-community-libs-compat-5.7.17-1.el7.x86_64
rpm -e mysql-community-common-5.7.17-1.el7.x86_64
rpm -e mysql-community-client-5.7.17-1.el7.x86_64
cd /var/lib/
rm -rf mysql/
清除余项
whereis mysql
mysql: /usr/bin/mysql /usr/lib64/mysql /usr/local/mysql /usr/share/mysql /usr/share/man/man1/mysql.1.gz
#删除上面的文件夹
rm -rf /usr/bin/mysql
rm -rf /var/log/mysql
6. 安装redis
yum -y --enablerepo=remi-safe install redis &&
systemctl restart redis
配置文件 redis.conf
//取消这个注释就设置了密码,重新一下生效
requirepass
登录
redis-cli -p 6379 -a 1234
7.测试脚本
<?php
$redis = new Redis();
$redis->connect(\'127.0.0.1\', 6379);//serverip port
$redis->auth(\'\');//my redis password
$redis ->set( "test" , "Hello World");
echo "Redis:" . $redis ->get( "test");
phpinfo();
try{
$pdo=new PDO("mysql:dbname=demo;host=localhost","root","1234");
$stmt=$pdo->prepare("select * from demo");
$stmt->execute();
print_r($stmt->fetch(PDO::FETCH_ASSOC));
}catch(\\Exception $e){
echo $e->getMessage();
}
被转义
echo \'$redis = new Redis();$redis->connect("127.0.0.1", 6379);$redis->auth(""); $redis->set("test", "Hello World");echo "Redis:" . $redis->get("test");phpinfo();try {$pdo = new PDO("mysql:dbname=demo;host=localhost", "root", "1234");$stmt = $pdo->prepare("select * from demo");$stmt->execute();print_r($stmt->fetch(PDO::FETCH_ASSOC));} catch (\\Exception $e) {echo $e->getMessage();}\';
8. 一键安装命令
自动安装安装nginx,mysql,php,redis,和关闭selinx,显示php错误,关闭防火墙80端口,修改php以端口方式连接ip:port
sed -i \'s/SELINUX=enforcing/SELINUX=disabled/\' /etc/selinux/config ; wget http://rpms.remirepo.net/enterprise/remi-release-8.rpm && yum -y localinstall remi-release-8.rpm && yum -y clean all && rm -rf remi-release-8.rpm && yum -y makecache && firewall-cmd --permanent --add-port=80/tcp && firewall-cmd --reload && yum -y install nginx && systemctl enable nginx && systemctl restart nginx && yum -y --enablerepo=remi-safe install php74-php php74-php-gd php74-php-xml php74-php-sockets php74-php-session php74-php-snmp php74-php-mysql && systemctl restart php74-php-fpm && sudo sed -i \'s/listen =.*/listen = 127.0.0.1:9000/\' `find / -name www.conf` && systemctl enable php74-php-fpm && sudo sed -i \'s/display_errors = .*/display_errors = On/\' `find / -name php.ini` ; sudo sed -i \'s/;date.timezone.*/date.timezone = PRC/\' `find / -name php.ini`; yum -y install mysql mysql-server mysql-devel && systemctl restart mysqld && systemctl enable mysqld && mysqladmin -u root password 1234 && yum -y --enablerepo=remi-safe install redis php74-php-pecl-redis5 ; systemctl restart redis && systemctl enable redis ; rm -rf /www && mkdir /www ; chmod -R 777 /www ; echo \'<?php echo "<h1 style=\\"font-size:55px;color:red;text-align:center;\\">年华流失提示您:恭喜你成功搭建LNMP环境</h1>"; $redis = new Redis();$redis->connect("127.0.0.1", 6379);$redis->auth(""); $redis->set("test", "Hello World");echo "Redis:" . $redis->get("test");phpinfo();try {$pdo = new PDO("mysql:dbname=mysql;host=localhost", "root", "1234");$stmt = $pdo->prepare("select * from user");$stmt->execute();print_r($stmt->fetch(PDO::FETCH_ASSOC));} catch (\\Exception $e) {echo $e->getMessage(); } \' >> /www/index.php && setenforce 0 && iptables -A INPUT -p tcp --dport 80 -j ACCEPT ; history -c && reboot
----------------------------- 一键安装,并修改服务器配置 -----------------------------
sed -i \'s/SELINUX=enforcing/SELINUX=disabled/\' /etc/selinux/config ; wget http://rpms.remirepo.net/enterprise/remi-release-8.rpm && yum -y localinstall remi-release-8.rpm && yum -y clean all && rm -rf remi-release-8.rpm && yum -y makecache && firewall-cmd --permanent --add-port=80/tcp && firewall-cmd --reload && yum -y install nginx && systemctl enable nginx && systemctl restart nginx && yum -y --enablerepo=remi-safe install php74-php php74-php-gd php74-php-xml php74-php-sockets php74-php-session php74-php-snmp php74-php-mysql && systemctl restart php74-php-fpm && sudo sed -i \'s/listen =.*/listen = 127.0.0.1:9000/\' `find / -name www.conf` && systemctl enable php74-php-fpm && sudo sed -i \'s/display_errors = .*/display_errors = On/\' `find / -name php.ini` ; sudo sed -i \'s/;date.timezone.*/date.timezone = PRC/\' `find / -name php.ini`; yum -y install mysql mysql-server mysql-devel && systemctl restart mysqld && systemctl enable mysqld && mysqladmin -u root password 1234 && yum -y --enablerepo=remi-safe install redis php74-php-pecl-redis5 ; systemctl restart redis && systemctl enable redis ; rm -rf /www && mkdir /www ; chmod -R 777 /www ; echo \'<?php echo "<h1 style=\\"font-size:55px;color:red;text-align:center;\\">年华流失提示您:恭喜你成功搭建LNMP环境</h1>"; $redis = new Redis();$redis->connect("127.0.0.1", 6379);$redis->auth(""); $redis->set("test", "Hello World");echo "Redis:" . $redis->get("test");phpinfo();try {$pdo = new PDO("mysql:dbname=mysql;host=localhost", "root", "1234");$stmt = $pdo->prepare("select * from user");$stmt->execute();print_r($stmt->fetch(PDO::FETCH_ASSOC));} catch (\\Exception $e) {echo $e->getMessage(); } \' >> /www/index.php ; setenforce 0 && iptables -A INPUT -p tcp --dport 80 -j ACCEPT ; mv `find / -name nginx.conf` `find / -name nginx.conf`.bak && url=`find / -name nginx.conf.bak` && echo "user root;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
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 /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name localhost;
root /www;
index index.php index.html
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
location ~ \\\\.php$ {
root /www;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME \\$document_root\\$fastcgi_script_name;
include fastcgi_params;
}
}
}" >> ${url%.*} && history -c && reboot
10 镜像资源
阿里云镜像
3. ubuntu安装
设置root密码
sudo su -
输入你的账户的密码
passwd
输入新的root密码
再次输入新的root密码
1. 安装nginx
# 切换至root用户
sudo su root
apt-get install nginx
查看nginx是否安装成功
nginx -v
启动nginx
service nginx start
卸载apt-get安装的nginx
# 彻底卸载nginx
apt-get --purge autoremove nginx
#查看nginx的版本号
nginx -v
2. 安装apache
第1步:更新系统存储库
您可以通过首先更新Ubuntu存储库的本地包索引来下载最新版本的软件。打开终端并输入以下命令以执行此操作:
sudo apt update
使用apt命令安装Apache 2
sudo apt install apache2
验证Apache安装
apache2 -version
配置文件
apache2.conf
以上是关于LNMP环境搭建教程及linux常用命令及问题的主要内容,如果未能解决你的问题,请参考以下文章