#从零开始建立一个php应用的教程
建立一个http服务的完整流程,这个流程可以用来指导系统的安装部署。
1. centos7
1.1 安装
在centos官网下载系统镜像并完成安装过程,安装过程根据安装向导进行。
1.2 配置
建立用户 useradd -d /home/username username -s /bin/bash -g wheel
添加用户组
添加密码 passwd username
添加权限 echo "username ALL=(ALL) NOPASSWD: ALL" > /etc/sudoers
ssh服务的配置依据个人习惯;
建议安装ntp服务;
2. php
2.1 安装
添加软件源
sudo yum install epel-release
sudo yum -y install https://rpms.remirepo.net/enterprise/remi-release-7.rpm
安装remi源提供的版本
sudo yum install php74 php74-php-cli php74-php-fpm php74-php-pgsql php74-php-zip php74-php-devel php74-php-gd php74-php-mcrypt php74-php-mbstring php74-php-curl php74-php-xml php74-php-pear php74-php-bcmath php74-php-json
切换最新版本的php为默认版本
scl enable php72 bash
执行检查命令,检查是否安装成功
php -v
php -m
php -r "phpinfo();"
2.2配置
fpm服务需要配置,配置文件如下
/etc/opt/remi/php74/php-fpm.d/www.conf
其中需要修改的内容为
user = nginx
group = nginx
listen = /var/opt/remi/php72/run/php-fpm/php72-fpm.sock
listen.owner = nginx
listen.group = nginx
listen.mode = 0660
修改好之后启动fpm服务,检查服务状态,检查socket文件是否生成,将fpm服务设置为默认启动
systemctl start php74-php-fpm
systemctl status php74-php-fpm
systemctl enable php74-php-fpm
3. nginx
3.1 安装
添加安装源并执行安装
sudo rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
sudo yum install -y nginx
启动服务,检查服务状态
systemctl start nginx
systemctl status nginx
3.2 配置
配置文件的地址为:/etc/nginx/nginx.conf
典型的php网站的配置内容为:
server { listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/buct/public/;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
try_files $uri /index.php$is_args$args;
}
location ~ ^/index\.php(/|$){
fastcgi_pass unix:/var/opt/remi/php74/run/php-fpm/php74-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi.conf;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
另外,如果使用虚拟目录的方式布置网站可以参考如下配置
location /buct {
alias html/buct/public;
try_files $uri @buct;
location ~ \.php {
include fastcgi.conf;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_pass 127.0.0.1:9000;
}
}
location @buct {
rewrite /buct/(.*)$ /buct/index.php?/$1 last;
}
修改配置后检查配置内容,并重新加载nginx服务
nginx -t
nginx -s reload
4. postgresql-12
4.1 安装
添加软件源并安装
sudo yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
yum install postgresql-12
4.4 配置
初始化数据库,并启动服务。
/usr/pgsql-12/bin/postgresql-12-setup initdb
systemctl start postgresql-12
登陆到psql客户端,修改postgre用户的密码
su postgres
psql
CREATE USER test WITH PASSWORD 'test'
ALTER USER postgres WITH PASSWORD 'NewPassword';
修改访问限制,允许指定的ip地址访问,比如,允许192.168.1.10访问
配置文件:
/var/lib/pgsql/12/data/pg_hba.conf
末尾添加:
host all all 192.168.1.10/32 md5
修改后重启服务:
systemctl restart postgresql-12
5. composer
5.1 安装
php -r "copy('https://install.phpcomposer.com/installer', 'composer-setup.php');"
php composer-setup.php
php -r "unlink('composer-setup.php');"
执行以上命令,下载composer应用
5.2 配置
sudo mv composer.phar /usr/local/bin/composer
将二进制文件拷贝到本地应用目录,方便全局使用。
6. symfony
6.1 安装
wget https://get.symfony.com/cli/installer -O - | bash
6.2 配置
sudo mv symfony /usr/local/bin/symfony
7. 建立网站
建立项目
cd /usr/share/nginx/
symfony new --full my_project
修改项目配置文件,添加数据连接信息
配置文件为
my_project/.env
修改链接为
DATABASE_URL="postgresql://postgres:passwd@127.0.0.1:5432/symfonydb?serverVersion=12&charset=utf8"
执行建立网站数据库的命令
php bin/console doctrine:database:create
8. 编码实现json接口
以建立一个书籍数据库为例子,实现getall和add两个http接口。
前一个接口可以获取所有的书籍信息,后一个接口添加书籍信息到数据库。
8.1 建立数据表结构
CREATE SEQUENCE book_id_seq INCREMENT BY 1 MINVALUE 1 START 1
CREATE TABLE book (id INT NOT NULL, mame VARCHAR(255) NOT NULL, public_at TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, PRIMARY KEY(id))
8.2 在控制其中实现可以访问书籍信息和添加书籍信息的接口
BookController.php 文件中包含以下内容。
/**
* @Route("/book")
*/
class BookController extends AbstractController
{
/**
* @Route("/api/getall", name="book_api_getall", methods={"GET"})
*/
public function getall(BookRepository $bookRepository): Response
{
$books = $bookRepository->findAll();
return $this->json($books);
}
/**
* @Route("/api/add", name="book_api_add", methods={"POST"})
*/
public function add(Request $request): Response
{
$content = $request->getContent();
$postarr = json_decode($content,true);
$book = new Book();
$book->setMame($postarr['Name'])
->setPublicAt(new \DateTime($postarr['publicAt']));
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($book);
$entityManager->flush();
return $this->json(array("result"=>"success"));
}
}
9 其他
另外提供一些trouble shooting的方法,用来鉴定问题;
显示所有在运行或者运行过服务模块或者显示所有安装过的服务模块
systemctl list-units
systemctl list-unit-files
删除mac多余的文件
find ./ -name "._*" | xargs rm -rf
postgresql添加用户并赋予权限的方式:查看用户,建立用户,赋予用户权限等
su postgres
psql
\password postgres
\du
CREATE ROLE username WITH LOGIN PASSWORD 'quoted password'
ALTER ROLE patrick CREATEDB;
CREATE DATABASE databasename;
GRANT ALL PRIVILEGES ON DATABASE super_awesome_application TO patrick
\list: lists all the databases in Postgres
\connect: connect to a specific database
\dt: list the tables in the currently connected database
Yum检索缺少模块,查看软件包安装了哪些模块,另外,就是全文搜索
yum provides "module.name"
rpm -qa | grep postgresql
rpm -ql postgresql12-12.3-5PGDG.rhel7.x86_64 | grep bin
Grep -rn ‘keyword’ -C 10
使用symfony中的脚手架有些命令是很常用的
php bin/console make:entity
make:controller
make:curd
make:migration
doctrine:database:create
doctrine:migration:migrate
使用到的软件及版本:
cat /etc/redhat-release
CentOS Linux release 7.8.2003 (Core)
php -v
PHP 7.4.8 (cli) (built: Jul 9 2020 08:57:23) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
nginx -v
nginx version: nginx/1.16.1
psql -V
psql (PostgreSQL) 12.3
composer -V
Composer version 1.10.9 2020-07-16 12:57:00
symfony -V
Symfony CLI version v4.17.2 (2020-07-14T21:01:25+0000 - stable)
git --version
git version 1.8.3.1
软件包的下载资源或安装教程:
Centos7 https://www.centos.org/download/
Php https://blog.csdn.net/qq_27715357/article/details/106734411
Nginx https://www.cnblogs.com/larryzq/p/11009045.html
Postgresql 12 https://cloud.tencent.com/developer/article/1592808
Composer https://pkg.phpcomposer.com/#how-to-install-composer
symfony https://symfony.com/download
##最后把配置脚本写在这里
#setup packages
yum update
yum install epel-release
yum -y install https://rpms.remirepo.net/enterprise/remi-release-7.rpm
yum install -y php72-php-cli php72-php-fpm php72-php-pgsql php72-php-zip php72-php-devel php72-php-gd php72-php-mcrypt php72-php-mbstring php72-php-curl php72-php-xml php72-php-pear php72-php-bcmath php72-php-json
scl enable php72 bash
yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
yum install postgresql12 postgresql12-server
rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
yum install nginx
#nginx
systemctl start nginx
cat <<EOF >/etc/nginx/conf.d/site.conf
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name app.yangfs.top;
root /usr/share/nginx/project/public/;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
try_files \$uri /index.php\$is_args\$args;
}
location ~ ^/index\.php(/|$){
fastcgi_pass unix:/var/opt/remi/php72/run/php-fpm/php72-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
EOF
nginx -s reload
#php-fpm
systemctl start php72-php-fpm
sed -i /etc/opt/remi/php72/php-fpm.d/www.conf \
-e 's/\(user = \)apache/\1nginx/' \
-e 's/\(group = \)apache/\1nginx/' \
-e 's/^listen = .*/listen = \/var\/opt\/remi\/php72\/run\/php-fpm\/php72-fpm.sock/' \
-e 's/^;\(listen.owner = \).*/\1nginx/' \
-e 's/^;\(listen.group = \).*/\1nginx/' \
-e 's/^;\(listen.mode = \).*/\10660/'
chown root:nginx /var/opt/remi/php72/lib/php/session/
systemctl restart php72-php-fpm
/usr/pgsql-12/bin/postgresql-12-setup initdb
systemctl start postgresql-12
su - postgres -c "psql -c \"CREATE USER symfony WITH PASSWORD 'password';\" "
su - postgres -c "psql -c \"Alter user symfony CREATEDB; \" "
sed -i /var/lib/pgsql/12/data/pg_hba.conf \
-e 's/\(^host\s*all\s*all\s*127.0.0.1\/32\).*$/\1\t\tmd5/'
systemctl restart postgresql-12
评论
发表评论