ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

lamp架构之nginx,mysql,php的安装编译

2021-07-07 19:30:23  阅读:201  来源: 互联网

标签:server1 nginx lamp usr mysql php root


L: Linux系统
A: apache服务器(也可使用nginx代替)
M:多指mysql或mariadb数据库
P: 指PHP、python、perl等编程语言。
广义的lamp架构可以指:Linux/unix/windows +apache/nginx…+mysql/pgsql+php/python/perl/golang
 

一 nginx

1.下载相关资源包

[root@server1 ~]# ls
nginx-1.20.1.tar.gz
[root@server1 ~]# tar zxf nginx-1.18.0.tar.gz    %解压
[root@server1 ~]# ls
nginx-1.20.1  nginx-1.20.1.tar.gz
[root@server1 ~]# cd nginx-1.20.1/
[root@server1 nginx-1.20.1]# ls         %源码安装时解压目录中的初始文件
auto     CHANGES.ru  configure  html     man     src
CHANGES  conf        contrib    LICENSE  README

安装相关工具:
[root@server1 nginx-1.20.1]# yum install gcc -y            %编译需要的包
[root@server1 nginx-1.20.1]# yum install pcre-devel -y     %http重写功能需要的包
[root@server1 nginx-1.20.1]# yum install -y openssl-devel  %ssl功能需要的包

2.编译、安装

[root@server1 nginx-1.20.1]# vim auto/cc/gcc   %注释掉debug选项,可以使安装最小化

[root@server1 nginx-1.20.1]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module     %编译,编译参数可自由选择 

[root@server1 nginx-1.20.1]# make && make install      %安装

3.添加环境变量并开启服务

[root@server1 sbin]# pwd
/usr/local/nginx/sbin
[root@server1 sbin]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@server1 ~]# vim .bash_profile      %注意路径(/root/下)
[root@server1 ~]# source .bash_profile   %使上述更改生效
[root@server1 ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/root/bin:/usr/local/nginx/sbin
[root@server1 ~]# which nginx
/usr/local/nginx/sbin/nginx
[root@server1 ~]# nginx     %运行nginx服务,nginx stop为关闭nginx服务

4.测试

编写测试页面并访问进行测试
[root@server1 ~]# curl localhost                 %使用curl命令进行测试,观察是否能够访问通畅
[root@server1 ~]# cd /usr/local/nginx/
[root@server1 nginx]# ls
client_body_temp  fastcgi_temp  logs        sbin       uwsgi_temp
conf              html          proxy_temp  scgi_temp
[root@server1 nginx]# cd html/
[root@server1 html]# ls
50x.html  index.html
[root@server1 html]# vim test.html
[root@server1 html]# curl localhost:/test.html   %编辑测试页,进行访问,观察是否能够访问到
www.westos.org

二 mysql编译安装

1下载安装相关资源包

tar zxf mysql-boost-5.7.31.tar.gz   %解压下载好的tar包
yum install -y cmake    %编译器
yum install -y ncurses-devel
yum install -y gcc-c++
yum install -y bison

2指定参数编译

cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/usr/local/mysql/data -DMYSQL_UNIX_ADDR=/usr/local/mysql/data/mysql.sock -DWITH_INNOBASE_STORAGE_ENGINE=1 -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DEXTRA_CHARSETS=all -DWITH_BOOST=/root/mysql-5.7.31/boost/boost_1_59_0/     %编译参数可选

cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \        	%安装目录
 -DMYSQL_DATADIR=/usr/local/mysql/data \               	%数据库存放目录
 -DMYSQL_UNIX_ADDR=/usr/local/mysql/data/mysql.sock \  	%Unix socket 文件路径
 -DWITH_MYISAM_STORAGE_ENGINE=1 \                      	%安装 myisam 存储引擎
 -DWITH_INNOBASE_STORAGE_ENGINE=1 \ 					%安装 innodb 存储引擎
 -DWITH_ARCHIVE_STORAGE_ENGINE=1 \ 						%安装 archive 存储引擎
 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \ 					%安装 blackhole 存储引擎
 -DWITH_PARTITION_STORAGE_ENGINE=1 \ 					%安装数据库分区
 -DENABLED_LOCAL_INFILE=1 \ 							%允许从本地导入数据
 -DWITH_READLINE=1 \ 									%快捷键功能
 -DWITH_SSL=yes \ 										%支持 SSL
 -DDEFAULT_CHARSET=utf8 \ 								%使用 utf8 字符
 -DDEFAULT_COLLATION=utf8_general_ci \					%校验字符
 -DEXTRA_CHARSETS=all \ 								%安装所有扩展字符集
 -DMYSQL_TCP_PORT=3306 \ 								%MySQL 监听端口

 

3.安装

make
make install

[root@server1 mysql-5.7.31]# cd /usr/local/mysql/
[root@server1 mysql]# ls
bin   include  LICENSE  mysql-test  README-test  support-files
docs  lib      man      README      share
[root@server1 mysql]# du -sh      
1.9G	.    %占用空间比较大

4相关配置

[root@server1 mysql]# cd support-files/
[root@server1 support-files]# ls
magic  mysqld_multi.server  mysql-log-rotate  mysql.server
[root@server1 support-files]# cp mysql.server /etc/init.d/mysqld    %根据你的主机内存复制 mysql 配置文件
[root@server1 support-files]# ll /etc/init.d/mysqld
-rwxr-xr-x 1 root root 10576 Mar 28 15:50 /etc/init.d/mysqld

[root@server1 mysql-5.7.31]# cd mysql-test/
[root@server1 mysql-test]# cd include/
[root@server1 include]# vim /etc/my.cnf   %编辑文件,设置基本配置
[root@server1 ~]# vim .bash_profile       %添加环境变量(注意路径)
[root@server1 ~]# source .bash_profile    %使环境变量生效
[root@server1 ~]# cd /usr/local/mysql/
[root@server1 mysql]# useradd -u 1001 -M -d /usr/local/mysql/data -s /sbin/nologin mysql  %添加数据库用户
[root@server1 mysql]# mysqld --initialize --user=mysql    %初始化,产生初始登陆密码

[root@server1 mysql]# /etc/init.d/mysqld start    %开启mysqld 
[root@server1 mysql]# mysql_secure_installation   %按提示完成 mysql 安全设置
Enter password for user root:      %输入上述步骤产生的初始登陆密码
New password:       %创建新密码
Re-enter new password:
Press y|Y for Yes, any other key for No:
Change the password for root ? ((Press y|Y for Yes, any other key for No) : 
Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success.
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
Success.
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
 - Dropping test database...
Success.
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.
All done!

5登录查看

[root@server1 mysql]# mysql -p
Enter password:                        %用上面设置的新密码登陆
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.7.31 Source distribution

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)
mysql> 

三 php编译 安装

1 资源准备

yum install bzip2 -y    %安装解压工具
tar jxf php-7.4.12.tar.bz2 
cd php-7.4.12/
yum search systemd
yum install -y systemd-devel.x86_64
yum search libxml2
yum install -y libxml2-devel.x86_64
yum search sqlite
yum install -y sqlite-devel.x86_64
yum install -y oniguruma-devel-6.8.2-1.el7.x86_64.rpm oniguruma-6.8.2-1.el7.x86_64.rpm
yum install -y libevent-devel-1.4.13-4.el6.x86_64.rpm libevent-doc-1.4.13-4.el6.noarch.rpm libevent-headers-1.4.13-4.el6.noarch.rpm
yum install -y libpng-devel.x86_64

上面这些rpm包都是在编译的时候根据报错提示安装的

2编译安装

为了能够保证nginx与php的正常通信,需要在编译php之前对nginx做相关配置

useradd -M -d /usr/local/nginx/ -s /sbin/nologin nginx
cd /usr/local/nginx/
cd conf/
ls
vim nginx.conf      %编辑配置文件
文件开头加上 user nginx nginx
nginx -s reload
ps -aux             %查看进程用户信息

3执行configure文件

 ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --enable-mysqlnd --with-pdo-mysql --with-mysqli --with-openssl-dir --enable-gd --with-zlib-dir --with-curl --with-pear --enable-inline-optimization --enable-soap --enable-sockets --enable-mbstring --enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx --with-fpm-systemd

4make && make install

执行configure文件后执行(这个过程需要一些时间):
make
make install   

四 设置nginx、php启动方式,二者结合测试

1 Linux系统中服务启动方式

以启动php-fpm为例:一种常见的服务启动方式是: /etc/init.d/php-fpm start
Linux中的/etc/init.d目录包含许多系统各种服务的启动和停止脚本,上述方式通过调用脚本的方式来启动服务,当然,事先要赋予该脚本可执行权限

第二种服务启动方式是利用systemd来启动:systemctl start php-fpm,相比于第一种传统的方式,这种启动服务的方式更加便捷、快速,且能够设置开机自启。使用了 systemd,就不需要再用init了。systemd 取代了initd,成为系统的第一个进程(PID 等于 1),其他进程都是它的子进程。
 

2脚本启动

cd /root/php-7.4.12/sapi/fpm
cp init.d.php-fpm /etc/init.d/php-fpm      %复制服务脚本到/etc/init.d目录下
chmod +x /etc/init.d/php-fpm            %为该脚本添加可执行权限
cd /usr/local/php/etc
cp php-fpm.conf.default php-fpm.conf   %复制php-fpm服务的配置文件模板,创建新的配置文件并根据需求做相应修改
vim php-fpm.conf
打开global中下面一行的注释:
pid=run/php-fpm.pid       %指定pid的存放目录

cd /usr/local/php/etc/php-fpm.d
cp www.conf.default www.conf       %复制另外一个相关配置文件模板并编辑
vim www.conf                %可以不用改,可能需要改的地方是启动的进程数,具体多少要根据生产环境来确定
cd /root/php-7.4.12
cp php.ini-production /usr/local/php/etc/php.ini    %复制php主配置文件到php的配置目录下,用于开发和生产,注意配置文件的命名方式必须要是php-ini

/etc/init.d/php-fpm start    %启动
ps -aux         %查看后台是否有相关进程
netstat -antulp    %查看是否开启相关端口

3使用systemd启动

chkconfig --level 35 php-fpm on       %设置开机自启的另外一种方式
chkconfig --list php-fpm         %查看chkconfig的一些参数

 

cd /root/php-7.4.12/
/etc/init.d/php-fpm stop     %使用脚本停止之前的php-fpm,后续测试使用systemctl方式启动
cd /root/php-7.4.12/sapi/fpm/ 
cp php-fpm.service /usr/lib/systemd/system
cd /usr/lib/systemd/system
vim php-fpm.service          %编辑配置文件,注释掉保护机制
systemctl daemon-reload 
systemctl start php-fpm.service   %启动服务

4Nginx启动方式设定

cd /usr/local/nginx/conf/          
vim nginx.conf                   	%编辑主配置文件
diff fastcgi.conf fastcgi_params    %查看文件的不同
nginx                            	%启动nginx
cd /etc/systemd/system/
cd /usr/lib/systemd/system
vim nginx.service                	%编辑配置文件
systemctl daemon-reload          	%重载daemon,让systemd后台能够识别到更改
nginx -s stop                    	%停止nginx
systemctl start nginx       	 	%systemctl方式启动nginx
systemctl enable nginx      	 	%设置开机自启

 

 

 

 

 systemd 默认从目录/etc/systemd/system/读取配置文件。但是,里面存放的大部分文件都是符号链接,指向目录/usr/lib/systemd/system/,真正的配置文件存放在那个目录。 systemctl enable 命令用于在上面两个目录之间,建立符号链接关系。
 

 

 

 

 

 

 5 nginx与php结合测试

cd /usr/local/nginx/html/      %进入到nginx的默认发布目录,编写php测试页面
 vim index.php
<?php
phpinfo()
?>
cd /usr/local/php/etc/
vim php.ini                    %编辑php的配置文件做访问测试
systemctl reload php-fpm.service   %reload php-fpm服务

 

标签:server1,nginx,lamp,usr,mysql,php,root
来源: https://blog.csdn.net/weixin_56993834/article/details/118552620

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有