Linux centos7部署LNMP


转载参考于:
nginx:https://www.cnblogs.com/wyd168/p/6636529.html
php:https://blog.csdn.net/uisoul/article/details/79024923
php7:https://blog.csdn.net/mangojo/article/details/86475554

一、安装基础使用包

安装make:

yum -y install gcc automake autoconf libtool make

安装g++:

yum -y install gcc gcc-c++

必须要分开安装!!!!

二、安装PCRE库

ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/ 下载最新的 PCRE 源码包,使用下面命令下载编译和安装 PCRE 包:(本文参照下载文件版本:pcre-8.37.tar.gz 经过验证未发现这个版本,若想下载最新版本请打开上面网址。本文选择pcre-8.39.tar.gz)

cd /usr/local/src
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.39.tar.gz 
tar -zxvf pcre-8.37.tar.gz
cd pcre-8.34
./configure
make
make install

**make的时候可能会报错:警告:检测到时钟错误。您的创建可能是不完整的。

原因:一般是虚拟机可能回滚了快照导致时间跟当前时间不同步
解决方法:重启系统,时间就会同步

三、安装zlib库

http://zlib.net/zlib-1.2.11.tar.gz 下载最新的 zlib 源码包,使用下面命令下载编译和安装 zlib包:(本文参照下载文件版本:zlib-1.2.8.tar.gz 经过验证未发现这个版本,若想下载最新版本请打开上面网址。本文选择zlib-1.2.11.tar.gz )

cd /usr/local/src
 
wget http://zlib.net/zlib-1.2.11.tar.gz
tar -zxvf zlib-1.2.11.tar.gz
cd zlib-1.2.11
./configure
make
make install

四、安装openssl

yum -y install openssl openssl-devel

五、安装nginx

Nginx 一般有两个版本,分别是稳定版和开发版,您可以根据您的目的来选择这两个版本的其中一个,下面是把 Nginx 安装到 /usr/local/nginx 目录下的详细步骤:

cd /usr/local/src
wget http://nginx.org/download/nginx-1.1.10.tar.gz
tar -zxvf nginx-1.1.10.tar.gz
cd nginx-1.1.10
./configure --with-http_ssl_module
make
make install

六、使用firewalld打开关闭防火墙与端口

使用以下方法把80(nginx)、3306(mysql)、9000(php-fpm)、21这几个端口开放出来

1、firewalld的基本使用
启动: systemctl start firewalld
关闭: systemctl stop firewalld
查看状态: systemctl status firewalld 
开机禁用  : systemctl disable firewalld
开机启用  : systemctl enable firewalld

2.systemctl是CentOS7的服务管理工具中主要的工具,它融合之前service和chkconfig的功能于一体。
启动一个服务:systemctl start firewalld.service
关闭一个服务:systemctl stop firewalld.service
重启一个服务:systemctl restart firewalld.service
显示一个服务的状态:systemctl status firewalld.service
在开机时启用一个服务:systemctl enable firewalld.service
在开机时禁用一个服务:systemctl disable firewalld.service
查看服务是否开机启动:systemctl is-enabled firewalld.service
查看已启动的服务列表:systemctl list-unit-files|grep enabled
查看启动失败的服务列表:systemctl --failed

3.配置firewalld-cmd
查看版本: firewall-cmd --version
查看帮助: firewall-cmd --help
显示状态: firewall-cmd --state
查看所有打开的端口: firewall-cmd --zone=public --list-ports
更新防火墙规则: firewall-cmd --reload
查看区域信息:  firewall-cmd --get-active-zones
查看指定接口所属区域: firewall-cmd --get-zone-of-interface=eth0
拒绝所有包:firewall-cmd --panic-on
取消拒绝状态: firewall-cmd --panic-off
查看是否拒绝: firewall-cmd --query-panic
 
那怎么开启一个端口呢
添加
firewall-cmd --zone=public --add-port=80/tcp --permanent    (--permanent永久生效,没有此参数重启后失效)
添加完记得重新载入!!!!
firewall-cmd --reload
查看
firewall-cmd --zone= public --query-port=80/tcp
删除
firewall-cmd --zone= public --remove-port=80/tcp --permanent

七、nginx使用方法

验证nginx配置文件是否正确
方法一:进入nginx安装目录sbin下,输入命令./nginx -t

看到显示

nginx.conf syntax is ok
nginx.conf test is successful

说明配置文件正确!

启动

/usr/local/nginx/sbin/nginx
或者
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

查看进程号

ps -ef|grep nginx

例子
如上图得到nginx主进程号是31908
nginx的停止有三种方式:

1、从容停止

kill -QUIT 31908

2、快速停止

kill -TERM 31908
或者
kill -INT 31908

3、强制停止

pkill -9 nginx

八、yum安装最新版mariadb

由于centos7 默认使用yum安装MySQL的话就会安装mariadb,只是安装的版本停留在mariadb5.x,版本比较低。如果我们需要安装mariadb10这里就需要删除mariadb-libs-5.5.52-1.el7.x86_64,这是系统默认安装的。

rpm -qa mariadb-libs
yum remove mariadb-libs

配置mariadb的yum仓库,这里我们选择在/etc/yum.repos.d/下直接创建仓库文件,版本地址到http://mirrors.ustc.edu.cn/mariadb/yum/查找相对应版本号

vi /etc/yum.repos.d/Mariadb.repo
#添加内容如下
# MariaDB 10.1 CentOS repository list - created 2017-02-23 08:53 UTC
# http://downloads.mariadb.org/mariadb/repositories/
[mariadb]
name = MariaDB
baseurl = http://mirrors.ustc.edu.cn/mariadb/yum/10.3/centos7-amd64
gpgkey=https://mirrors.ustc.edu.cn/mariadb/yum/RPM-GPG-KEY-MariaDB
gpgcheck=1

使用yum安装

yum install mariadb-server mariadb-devel

启动mysql

[root@localhost //]# systemctl start mariadb

这里提示我们需要为root用户设置密码以及进行初始化

[root@localhost mysql]# mysql_secure_installation

> 以下提示:

Enter current password for root (enter for none):     输入当前root密码(没有输入)

Set root password? [Y/n]     设置root密码?(是/否)

New password:    输入新root密码

Re-enter new password:        确认输入root密码

Password updated successfully!         密码更新成功

By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

默认情况下,MariaDB安装有一个匿名用户,
允许任何人登录MariaDB而他们无需创建用户帐户。
这个目的是只用于测试,安装去更平缓一些。
你应该进入前删除它们生产环境。

Remove anonymous users? [Y/n]         删除匿名用户?(是/否)

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

通常情况下,root只应允许从localhost连接。
这确保其他用户无法从网络猜测root密码。

Disallow root login remotely? [Y/n]     不允许root登录远程?(是/否)

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

默认情况下,MariaDB提供了一个名为“测试”的数据库,任何人都可以访问。
这也只用于测试,在进入生产环境之前应该被删除。

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

重新加载权限表将确保所有到目前为止所做的更改将立即生效。

Reload privilege tables now? [Y/n]      现在重新加载权限表(是/否)

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

全部完成!如果你已经完成了以上步骤,MariaDB安装现在应该安全。

Thanks for using MariaDB!

感谢使用MariaDB!

进入MariaDB终端

  [root@localhost ~]# mysql

如果设置了密码则加入参数

-u: 用户名
-p: 密码
-P: 端口号
-h: 主机
更多参数具体含义请参数 mysql --help 进行查看!

[root@localhost ~]# mysql -u u_setting_username -p

通过用户名密码登入mariadb

九、安装php5.6.40/第十点安装php7

  1. 相关依赖包安装
libmcrypt
libmcrypt-devel
mhash
mhash-devel

mcrypt扩展库可以实现加密解密功能,就是既能将明文加密,也可以密文还原。
mhash是基于离散数学原理的不可逆向的php加密方式扩展库,其在默认情况下不开启。
mhash的可以用于创建校验数值,消息摘要,消息认证码,以及无需原文的关键信息保存(如密码)等。
centos源不能安装libmcrypt-devel,由于版权的原因没有自带mcrypt的包,唯有安装yum扩展包
libevent是一个异步事件通知库文件,其API提供了在某文件描述上发生某事件时或其超时时执行回调函数的机制 ,它主要用来替换事件驱动的网络服务器上的event loop机制。 目前来说, libevent支持/dev/poll、kqueue、select、poll、epoll及Solaris的event ports。
bzip2 是一个基于Burrows-Wheeler 变换的无损压缩软件能够高效的完成文件数据的压缩
libcurl主要功能就是用不同的协议连接和沟通不同的服务器,也就是相当封装了的sockPHP
libcurl允许你用不同的协议连接和沟通不同的服务器
图形相关的rpm包 通常对应的错误提示:JIS-mapped Japanese font support in GD

yum  install epel-release  //扩展包更新包
yum install php-mcrypt libmcrypt libmcrypt-devel mhash mhash-devel
yum install libevent libevent-devel
yum install libxml2 libxml2-devel bzip2-devel libcurl-devel
yum install libjpeg-devel libpng-devel freetype-devel
yum -y install readline-devel

安装

tar -xzvf php-5.6.25.tar.gz
cd php-5.6.25
./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --enable-inline-optimization --disable-debug --disable-rpath --enable-shared --enable-opcache --enable-fpm --with-mysql --with-mysqli --with-pdo-mysql --with-gettext --enable-mbstring --with-iconv --with-mcrypt --with-mhash --with-openssl --enable-bcmath --enable-soap --with-libxml-dir --enable-pcntl --enable-shmop --enable-sysvmsg --enable-sysvsem --enable-sysvshm --enable-sockets --with-curl --with-zlib --enable-zip --with-bz2 --with-gd --with-freetype-dir --with-jpeg-dir --with-png-dir --with-readline

php配置

php.ini是php运行核心配置文件
php-fpm.conf是php-fpm进程服务的配置文件

# cp php.ini-production /usr/local/php/etc/php.ini
# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
# chmod +x /etc/init.d/php-fpm

# chkconfig --add php-fpm
# chkconfig php-fpm on
# service php-fpm start
    Starting php-fpm done
# ps -ef|grep php
    root 45690 1 0 20:07 ? 00:00:00 php-fpm: master process(/usr/local/php/etc/php-fpm.conf)                                  
    nobody 45691 45690 0 20:07 ?   00:00:00 php-fpm: pool www                                                      
    nobody 45692 45690 0 20:07 ?   00:00:00 php-fpm: pool www                                                      
    root  45694  3441 0 20:07 pts/4 00:00:00 grep php

# netstat -nltp|grep 9000
    tcp 0  0 127.0.0.1:9000  0.0.0.0:*  LISTEN  45690/php-fpm 

启动php-fpm:

/usr/local/php/sbin/php-fpm

重启php-fpm:

kill -USR2 42891

master进程可以理解以下信号

INT, TERM 立刻终止
QUIT 平滑终止
USR1 重新打开日志文件
USR2 平滑重载所有worker进程并重新载入配置和二进制模块

**

十、编译安装php7

**

方法一、yum安装

yum install libmcrypt libmcrypt-devel mcrypt mhash libxml2-devel

以下libzip扩展必须编译安装

#由于libzip1.4版本以上要用cmake安装,所以先编译安装cmake
wget https://cmake.org/files/v3.6/cmake-3.6.2.tar.gz
tar -zxvf cmake-3.6.2.tar.gz
cd cd cmake-3.6.2/
./bootstrap
gmake
gmake install

#新建cmake软连接
ln -s /usr/local/bin/cmake /usr/bin/
#查看版本
cmake --version

#卸载原有libzip
yum remove libzip -y

#开始安装libzip
wget https://libzip.org/download/libzip-1.5.2.tar.gz
tar -zxvf libzip-1.5.2.tar.gz
cd cd libzip-1.5.2/

##连贯操作编译
mkdir build && cd build && cmake .. && make && make install

#继续安装其他扩展
yum -y install curl-devel libjpeg-devel libpng-devel freetype-devel

下载php安装包
官网下载php-7.3.2.tar.gz

安装

tar -zxvf php-7.3.2.tar.gz
cd php-7.3.2
./configure   --prefix=/usr/local/php   --with-config-file-path=/usr/local/php/etc  --with-png-dir=/usr/local/libpng   --with-jpeg-dir=/usr/local/jpeg   --with-freetype-dir=/usr/local/freetype   --with-zlib-dir=/usr/local/zlib   --with-libxml-dir=/usr/local/libxml2/   --with-iconv-dir=/usr/local/libiconv   --enable-libxml   --enable-xml   --enable-bcmath   --enable-shmop   --enable-sysvsem   --enable-inline-optimization   --enable-opcache   --enable-mbregex   --enable-fpm   --enable-mbstring=all   --with-openssl   --enable-pcntl   --enable-sockets   --with-xmlrpc   --enable-zip   --enable-soap   --without-pear   --with-gettext   --enable-session   --with-curl   --enable-ctype   --enable-shared   --with-gd   --with-pdo-mysql --with-mysql-sock=/var/mysql/mysql.sock
make clean
make
make install

###如果编译过程出现以下报错:

configure: error: off_t undefined; check your library configuration

使用以下方法解决:

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

注意php7.2不支持–with-mcrypt=/usr/local/libmcrypt,–enable-gd-native-ttf这两个选项。官方建议用openssl_*系列函数代替Mcrypt_*系列的函数

将源码中的php.ini-development复制到给php.ini指定路径下

cp php.ini-development /usr/local/php/etc/php.ini

复制一份php-fpm配置文件,并命名为php-fpm.conf

cd /usr/local/php/etc
cp php-fpm.conf.default php-fpm.conf

如果文件不存在,则阻止 Nginx 将请求发送到后端的 PHP-FPM 模块, 以避免遭受恶意脚本注入的攻击

vim /usr/local/php/etc/php.ini
cgi.fix_pathinfo=0

添加用户组

useradd www

修改用户和组

cd  /usr/local/php/etc/php-fpm.d
cp www.conf.default  www.conf
vim www.conf
  1. 开启php服务

    /usr/local/php/sbin/php-fpm

将php命令加入到环境变量中

vim /etc/profile
sh /etc/profile

如果想在命令行下运行php,需要将可执行文件复制到环境变量下:

cp /usr/local/php/bin/php  /usr/local/bin/php
php -m

添加端口

firewall-cmd --zone=public --add-port=80/tcp --permanent
firewall-cmd --zone=public --add-port=3306/tcp --permanent
firewall-cmd --zone=public --add-port=9000/tcp --permanent
firewall-cmd --reload
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好!要在CentOS 7上部署LNMPLinux + Nginx + MySQL + PHP),您可以按照以下步骤进行操作: 1. 安装和配置Nginx: - 执行以下命令安装Nginx: ``` sudo yum install nginx ``` - 配置Nginx的虚拟主机(网站): - 打开Nginx配置文件:`sudo vi /etc/nginx/nginx.conf` - 在`http`块中添加以下内容来配置虚拟主机: ``` server { listen 80; server_name example.com; root /var/www/html; index index.php index.html index.htm; location / { try_files $uri $uri/ /index.php?$query_string; } location ~ \.php$ { fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } } ``` - 保存并关闭文件。 - 启动Nginx服务:`sudo systemctl start nginx` - 开启开机自启:`sudo systemctl enable nginx` 2. 安装和配置MySQL: - 执行以下命令安装MySQL: ``` sudo yum install mysql-server ``` - 启动MySQL服务:`sudo systemctl start mysqld` - 运行安全脚本来设置MySQL的密码等相关配置:`sudo mysql_secure_installation` 3. 安装PHP-FPM和相关扩展: - 执行以下命令安装PHP-FPM和常用扩展: ``` sudo yum install php-fpm php-mysqlnd php-gd php-xml php-mbstring ``` - 打开PHP-FPM配置文件:`sudo vi /etc/php-fpm.d/www.conf` - 将以下行的注释符号去掉: ``` ;listen.owner = nobody ;listen.group = nobody ;listen.mode = 0666 ``` - 保存并关闭文件。 - 启动PHP-FPM服务:`sudo systemctl start php-fpm` - 开启开机自启:`sudo systemctl enable php-fpm` 4. 配置防火墙: - 如果防火墙开启了,需要允许HTTP和HTTPS流量通过: ``` sudo firewall-cmd --permanent --zone=public --add-service=http sudo firewall-cmd --permanent --zone=public --add-service=https sudo firewall-cmd --reload ``` 现在,您应该已经成功在CentOS 7上部署LNMP。您可以将您的网站文件放置在`/var/www/html`目录下,然后访问您的服务器的IP地址或域名来查看您的网站。 请注意,以上步骤仅供参考,实际情况可能因您的系统配置和需求而有所不同。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值