Yes you can absolutely apply while he is here. The general start of the process is that you file form I-130 to apply for his permanent stay (start of the green card process), as well as form I-485 to adjust his status. The 485 will allow your spouse to stay with you in the country while he awaits a decision and still maintain a legal status. There is no risk to his legality, as long as the forms and accompanying evidence are received by USCIS before the last day that his visa allows him to be in the country.Entry stamps are usually for no more than 6 months in the country and USCIS generally takes about 2 to 3 weeks to process and send out a receipt. Subtract those days from his date to leave, and use that as your minimum safe timeline. Keep in mind that some forms may come with restrictions in travel and he may be stuck in one place for a while e.g. I-485s may take months to process and can be voided simply by leaving the country without getting special permission in advance.Note also: Messing up the timeline can mean he may have to return to the country of origin to maintain legal status and await a decision. That may literally be a separation of numerous months to several years. In some cases, visitoru2019s visa may be revoked or entry denied once you put in an immigrant petition for him.After you start the process, you may also have to file a myriad of other forms, including an affidavit of support, co-sponsor form, an immigrant visa application, an application for removal of conditions, unlawful presence waiver and so on, all of which come with their own fees. USCIS will usually direct you to what forms are next, once you have completed each stage of the application.The range of forms you may have to file will change depending on the specifics of your case and the complexity will vary based on a number of factors. The simplest of these are the number of years you have been married, your spouses immigration status, but may include any number of things that you have not reported in your very general question.My advice would be to seek the help of an immigration lawyer to navigate the process. It is something you could do by yourself. The process is tedious and requires attention to detail, but is manageable. However, under the latest Trump directive, immigration officers are now simply denying applications for minor errors or insufficient information. In the past, they used to request additional documentation before denying outright, but are no longer required to do so. That would mean an error would require you to resubmit an entirely new application (once allowed), and pay all the hundreds to thousands in cash again. It may even interrupt your spouses legal status and add years to the process.No petitions are guaranteed to be approved, so it is best to be thorough and careful and avoid even the appearance of impropriety. Best of luck to you both!Disclaimer: I am not even close to being an immigration lawyer. I just work with a diverse population and have helped clients navigate this process scores of times.