安装LNMP笔记

时间:2023-04-24 19:02:20

安装好centos7,主机ssl连不上

网卡设置为桥接模式或NAT模式

防火墙的原因

首先telnet本机22口

先更新yum源

yum update -y

安装网络常用工具:telnet traceroute

yum install telnet -y

yum install traceroute -y

查看内核版本

cat /proc/version

uname -a

开放22端口

firewall-cmd --zone=public --add-port=80/tcp --permanent

重启防火墙

firewall-cmd --reload

安装Nginx

Nginx由C语言编写

判断系统是否安装了Nginx

yum list installed Nginx

默认情况Centos7中无Nginx的源,最近发现Nginx官网提供了Centos的源地址。因此可以如下执行命令添加源:

rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

查看可供安装的nginx版本

yum list nginx

安装nginx

yum install nginx -y

启动nginx

systemctl start nginx

开机自启动

systemctl enable nginx

查看localhost是否能正常访问

curl localhost

安装mysql

centos7默认安装了mariadb,避免冲突,卸载mariadb

查看是否安装了mariadb

rpm -qa | grep mariadb

卸载mariadb

rpm -e --nodeps mariadb-libs-5.5.60-1.el7_5.x86_64  //--nodeps表示如果此软件被其他软件所依赖时可强制删除

开始安装mysql

https://dev.mysql.com/downloads/repo/yum/  此去查看最新的mysql的rpm包

下载源安装包

wget http://dev.mysql.com/get/mysql80-community-release-el7-2.noarch.rpm

安装Mysql源

yum -y install mysql80-community-release-el7-2.noarch.rpm

查看可安装的mysql

yum list mysql

mysql-connectors-community 
mysql-tools-community 
mysql80-community

安装mysql服务器

yum install mysql-community-server -y

启动mysql服务

systemctl start mysqld

查看mysql状态

systemctl status mysqld

找到root初始密码

grep "password" /var/log/mysqld.log

登陆

mysql -uroot -p

修改密码

mysql>alter user 'root'@localhost identified by '********'//不少于8位

授权root远程访问权限

因为安装的是mysql8

create user 'root'@'%' identified by '********'

grant all on *.* to 'root'@'%'

flush privileges;

mysql8语法参考

https://dev.mysql.com/doc/refman/8.0/en/

将mysql设置为自启动

systemctl enable mysqld

安装php7

由于php无官网yun源

所以改为编译安装php7

安装依赖库

yum -y install libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel ncurses curl gdbm-devel db4-devel libXpm-devel libX11-devel gd-devel gmp-devel expat-devel xmlrpc-c xmlrpc-c-devel libicu-devel libmcrypt-devel libmemcached-devel libzip

下载安装包并解压

wget http://php.net/distributions/php-7.3.4.tar.gz

tar -zxvf php-7.3.4.tar.gz
cd php-7.3.4
编译./configure --help可查看编译参数
./configure  --prefix=/usr/local/php --enable-fpm --with-fpm-user=www --with-fpm-group=www --with-config-file-path=/usr/local/php/conf --disable-rpath --enable-soap --with-libxml-dir --with-xmlrpc --with-openssl --with-mhash --with-pcre-regex --with-zlib --enable-bcmath --with-bz2 --enable-calendar --with-curl --enable-exif --with-pcre-dir --enable-ftp --with-gd --with-openssl-dir --with-jpeg-dir --with-png-dir --with-zlib-dir --with-freetype-dir --enable-gd-jis-conv --with-gettext --with-gmp --with-mhash --enable-mbstring --with-onig --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-zlib-dir --with-readline --enable-shmop --enable-sockets --enable-sysvmsg --enable-sysvsem  --enable-sysvshm  --enable-wddx --with-libxml-dir --with-xsl --enable-zip --with-pear export LD_LIBRARY_PATH="/usr/local/lib"

报错configure: error: off_t undefined; check your library configuration

vim /etc/ld.so.conf  #

# 添加搜索路径到配置文件
echo '/usr/local/lib64
/usr/local/lib
/usr/lib
/usr/lib64'>>/etc/ld.so.conf
# 更新配置
ldconfig -v

make && make install

执行完安装命令后php7就已经安装在到了/usr/local/php目录下了。

/usr/local/php/bin/php -v
查看安装是否成功。

为了以后方便,可以编辑 /etc/profile 添加环境变量 ,添加到最后面

PATH=$PATH:/usr/local/php/bin
export PATH
然后更新环境变量。

source /etc/profile

查看环境变量

echo $PATH
查看php版本

php -v
配置php-fpm

cp php.ini-production /usr/local/php/conf/php.ini
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
将php-fpm加入启动服务

cp sapi/fpm/php-fpm.service /usr/lib/systemd/system/php-fpm.service
启动php-fpm

systemctl start php-fpm.service

如果启动不了,应该是没有该用户,直接执行

groupadd www
useradd -g www www
然后再启动php-fpm
关联nginx和php

netstat -anpo | grep 9000

tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 38666/php-fpm: mast off (0.00/0/0)

fastcgi已监听9000端口

只要将web请求发送到这里即可

配置nginx

vim /etc/nginx/conf.d/default.conf

假定根目录为 /webroot 权限设置为777

配置如下

location / {
root /webroot;
index index.php index.html index.htm;
}

location ~ \.php$ {
root /webroot;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /webroot$fastcgi_script_name;
include fastcgi_params;
}

OK

有个遗留问题直接访问根目录时403 权限给够了。。