This article has participated in the good article call order activity, click to see: back end, big front end double track submission, 20,000 yuan prize pool for you to challenge!
background
Shortly after I entered the company, I took over an old project, and when I saw the project code, I crashed. Php5.3, the program and database were BOTH GBK code, and handwritten ORM operation. After a period of development, I was uncomfortable with the following points:
1. The framework and many components were packaged by previous developers without documentation.
2, the program and database are GBK code, the operation of colleagues from time to time to ask, I just filled in the things how messed up again… .
3, PHP version is low, many high version methods can not be used, there is no automatic loading, many new component packages can not be introduced, and the latest stable package requires PHP7 version or above.
4. The project was handled by too many developers, the business logic was complex, a lot of useless code was not deleted, and there were obvious traces of simple repair to complete a small function.
After discussing with the technical person in charge and the product manager, I decided to refactor the project for better development because there were many areas to be adjusted and long-term maintenance was required.
To be clear, refactoring does not mean that the previous project was bad. On the contrary, after so many years of product iteration and development by so many people, the architecture is still running and developing stably. I also learned a lot from the functionality encapsulated by many developers.
The refactoring process
1. The server should be equipped with a higher version of PHP, and I configured PHP7.3.
2. Choose Yii2 as the framework (you can also choose other frameworks, as long as it is easy to use by yourself), and configure the connection of database, Redis and other third-party libraries to keep consistent with the old project.
3. Determine the first URL address to be refactored with the product, and rewrite the code for this address in the new framework.
4, server nginx configure the url redirection.
5. Repeat steps 3 or 4 for a long time until most of the core pages have been refacfacted (I haven’t finished refactoring yet because some of them are simple and have very little chance of changing in the future).
6. Changed the database encoding to UTF-8 together with operation and maintenance. Transcoding tests should be repeated on the test server before online operation to ensure that garbled characters will not appear.
Note: actually change the database encoding and reconstruction project code without order, different coding program database connection specified when the corresponding code is good, also want to remind is, the operation according to the actual situation, if the data is constantly updated in real time, data accuracy demand is higher, or advice do not operate, very dangerous.
Nginx configurations coexist with multiple versions of PHP
Since this project is a very old project and the framework is written by the previous developer, the method of optimizing URL is to use the rewrite module of Nginx to achieve URL rewriting. The configuration of Nginx is roughly like this:
server { listen 80; server_name www.test.com; Root /var/www/test.com; index index.php index.html index.htm; Rewrite "^/post/(\d{1,6}).html$" /server/index.php? con=post&ac=detail&id=$1 last; Rewrite "^/article/(\d{1,6}).html$" /server/index.php? con=article&ac=detail&id=$1 last; } location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_split_path_info ^((? U).+\.php)(/? . +) $; Fastcgi_pass Unix: / var/run/PHP/php5.3 - FPM. The sock. }}Copy the code
If you do not do url beautification, the above want to access the details page URL address is:
http://www.test.com/server/index.php?con=post&ac=detail&id=123
http://www.test.com/server/index.php?con=article&ac=detail&id=123
Copy the code
When nginx rewrites the URL, the access details page becomes:
http://www.test.com/post/123.html
http://www.test.com/article/123.html
Copy the code
Obviously, the second type of URL is more attractive to users, but the second type of URL is obviously not written to make users look good, but the second type of URL is more attractive to search engines, and it’s easier to help us to get traffic.
Current frameworks such as Laravel or Yii2 come with url rewriting, so nGINx configurations like the one above are hard to see. Without further explanation, see how to change the above nginx configuration.
Looking at the nginx configuration above, you can see that if you want to rewrite a single URL with a new framework without changing the URL, all you need to do is change the point behind rewrite, for example
# the changed address rewrite "^ / post/(\ d {1, 6}). HTML $"/index. PHP? r=post/detail&id=$1 last; Rewrite "^/article/(\d{1,6}).html$" /server/index.php? con=article&ac=detail&id=$1 last;Copy the code
The new and old code is two projects, and there are two problems:
- How do I make the first address access the code of the new project and the second access the code of the old project
- New project code is php7.3, old project is PHP5.3, how to make it automatically select PHP version according to the project
“PHP” is parsed in PHP by default. Therefore, we only need to add a regex, and add a distinct identifier between the project and the PHP version. According to this identifier, I am the entry file for the change. PHP: index-prod. PHP: index-prod. PHP: index-prod. PHP: index-prod. PHP: index-prod. PHP
# the changed address rewrite "^ / post/(\ d {1, 6}). HTML $"/index - prod. PHP? r=post/detail&id=$1 last; Rewrite "^/article/(\d{1,6}).html$" /server/index.php? con=article&ac=detail&id=$1 last;Copy the code
The full nginx configuration looks like this:
server { listen 80; server_name www.test.com; Root /var/www/test.com; index index.php index.html index.htm index-prod.php; Rewrite "^/post/(\d{1,6}).html$" /index-prod.php? r=post/detail&id=$1 last; Rewrite "^/article/(\d{1,6}).html$" /server/index.php? con=article&ac=detail&id=$1 last; } # match entry documents, determine whether to go to the new code location ~ index \ - prod. PHP ${# change project path root/var/www/new.test.com/frontend/web; include snippets/fastcgi-php.conf; # Fastcgi_pass Unix :/var/run/ PHP /php7.3-fpm.sock; fastcgi_split_path_info ^((? U).+\.php)(/? . +) $; ${include snippets/fastcgi-php.conf; fastcgi_split_path_info ^((? U).+\.php)(/? . +) $; Fastcgi_pass Unix: / var/run/PHP/php5.3 - FPM. The sock. }}Copy the code
conclusion
Through the above methods, I completed the refactoring of the core business bit by bit without delaying the normal business development. Although the refactoring has not been completed yet, it has not affected the new development and the integration of new employees. Some urls have low change rates and stable features, so they can’t be changed, and we can’t refactor for the sake of refactoring, because we have a lot of work to do.
Nginx has a lot of functions, the above only used a few parts, interested friends can in-depth understanding, will give you a lot of surprise. Above have wrong place, also welcome everybody to correct.